在WPF C#DataGrid单元格单一的点击编辑模式? [英] DataGrid cell single click edit mode in wpf c#?

查看:2134
本文介绍了在WPF C#DataGrid单元格单一的点击编辑模式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的DataGrid在我的WPF应用程序。它有一些事件。我想编辑单一的点击数据网格单元。现在,当我加倍编辑单击该单元格。我曾尝试一些东西。但它不是为我工作。行和列是不恒定的。动态我会为Datagrid中创建列。
这是我的代码..



XAML中:

 < DataGrid的名称=dgTest的AutoGenerateColumns =真CanUserResizeColumns =FALSECanUserAddRows =FALSE
的ItemsSource ={结合NotifyOnSourceUpdated = TRUE}
的Horizo​​ntalAlignment =左
的SelectionMode =单
SelectionUnit =单元格
IsSynchronizedWithCurrentItem =真
CellStyle ={StaticResource的DataGridBorder}
CurrentCellChanged =dgTest_CurrentCellChanged
CellEditEnding =dgTest_CellEditEnding
的GotFocus =dgTest_GotFocus引发LostFocus =dgTest_LostFocus
GotKeyboardFocus =TextBoxGotKeyboardFocusLostKeyboardFocus =TextBoxLostKeyboardFocus
AutoGeneratingColumn =dgTest_AutoGeneratingColumn/>



我尝试添加一些代码为的GotFocus事件..但还是它不是为我工作。任何帮助将非常感激。



CS:

 私人无效dgTest_GotFocus(对象发件人,RoutedEventArgsê )
{
//查询的源为DataGridCell
如果(e.OriginalSource.GetType()== typeof运算(DataGridCell))
{
//开始于该行的编辑;
DataGrid的GRD =(DataGrid中)发送;
grd.BeginEdit(E);
}

}


解决方案

试试这个:



勾上你的DataGrid中的PreviewMouseLeftButtonDown事件:

 < DataGrid.Resources> 
<风格的TargetType ={X:类型DataGridCell}>
< EventSetter
事件=的PreviewMouseLeftButtonDown
Handeler =DataGridCell_PreviewMouseLeftButtonDown/>
< /样式和GT;
< /DataGrid.Resources>

然后在后面的代码:

 私人无效DataGridCell_PreviewMouseLeftButtonDown(对象发件人,MouseButtonEventArgs E)
{
DataGridCell细胞=(DataGridCell)发送;
如果(电池= NULL&放大器;!&安培;!cell.IsEditing和放大器;&安培;!cell.IsReadOnly)
{
如果(!cell.IsFocused)
{
cell.Focus();
}
如果(grdData.SelectionUnit!= DataGridSelectionUnit.FullRow)
{
如果(!cell.IsSelected)
{
cell.IsSelected = TRUE ;
}
}
,否则
{
DataGridRow行= FindVisualParent< DataGridRow>(小区);
如果(行= NULL&放大器;!&安培;!row.IsSelected)
{
row.IsSelected = TRUE;
}
}

}
}
静态牛逼FindVisualParent< T>(的UIElement元素),其中T:的UIElement
{
的UIElement父为元素;
,而(父!= NULL)
{$ B $(B T)= correctlyTyped父母为T;
如果(correctlyTyped!= NULL)
{
返回correctlyTyped;
}
父= VisualTreeHelper.GetParent(父)为的UIElement;
}
返回NULL;
}


i have DataGrid in my wpf application. its having some events. i want to edit the datagrid cell in single click. now its editing when i double click the cell. i have tried something. but its not working for me. The rows and columns are not constant. Dynamically i will create column for Datagrid. this is my code..

Xaml:

 <DataGrid Name="dgTest" AutoGenerateColumns="True" CanUserResizeColumns="False" CanUserAddRows="False"
                  ItemsSource="{Binding NotifyOnSourceUpdated=True}" 
                  HorizontalAlignment="Left" 
                  SelectionMode="Single"
                  SelectionUnit="Cell"
                  IsSynchronizedWithCurrentItem="True"
                  CellStyle="{StaticResource DataGridBorder}"
                  CurrentCellChanged="dgTest_CurrentCellChanged"
                  CellEditEnding="dgTest_CellEditEnding"   
                  GotFocus="dgTest_GotFocus" LostFocus="dgTest_LostFocus"
                  GotKeyboardFocus="TextBoxGotKeyboardFocus" LostKeyboardFocus="TextBoxLostKeyboardFocus"
                  AutoGeneratingColumn="dgTest_AutoGeneratingColumn"/>

i tried adding some code to the "GotFocus" event.. but still its not working for me. Any help would be really appreciated.

CS:

 private void dgTest_GotFocus(object sender, RoutedEventArgs e)
    {
        // Lookup for the source to be DataGridCell
        if (e.OriginalSource.GetType() == typeof(DataGridCell))
        {
            // Starts the Edit on the row;
            DataGrid grd = (DataGrid)sender;
            grd.BeginEdit(e);
        }

    }     

解决方案

Try this:

Hook up the PreviewMouseLeftButtonDown event on your DataGrid:

    <DataGrid.Resources>
        <Style TargetType="{x:Type DataGridCell}">
            <EventSetter
                Event="PreviewMouseLeftButtonDown"
                Handeler="DataGridCell_PreviewMouseLeftButtonDown"/>
        </Style>
    </DataGrid.Resources>

Then in the code behind:

    private void DataGridCell_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
        DataGridCell cell = (DataGridCell) sender;
        if (cell != null && !cell.IsEditing && !cell.IsReadOnly)
        {
            if (!cell.IsFocused)
            {
                cell.Focus(); 
            }
            if (grdData.SelectionUnit != DataGridSelectionUnit.FullRow)
            {
                if (!cell.IsSelected)
                {
                    cell.IsSelected = true;
                }
            }
            else
            {
                DataGridRow row = FindVisualParent<DataGridRow>(cell);
                if (row != null && !row.IsSelected)
                {
                    row.IsSelected = true;
                }
            }

        }
    }
    static T FindVisualParent<T>(UIElement element) where T : UIElement
    {
        UIElement parent = element;
        while (parent != null)
        {
            T correctlyTyped = parent as T;
            if (correctlyTyped != null)
            {
                return correctlyTyped;
            }
            parent = VisualTreeHelper.GetParent(parent) as UIElement;
        }
        return null;
    } 

这篇关于在WPF C#DataGrid单元格单一的点击编辑模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆