DataGrid单元格单击编辑模式在wpf c#? [英] DataGrid cell single click edit mode in wpf c#?
问题描述
这是我的代码..
Xaml:
$ _ code>< DataGrid Name =dgTestAutoGenerateColumns =TrueCanUserResizeColumns =FalseCanUserAddRows =False
ItemsSource ={Binding NotifyOnSourceUpdated = True}
HorizontalAlignment =Left
SelectionMode =Single
SelectionUnit =Cell
IsSynchronizedWithCurrentItem =True
CellStyle ={StaticResource DataGridBorder}
CurrentCellChanged =dgTest_CurrentCellChanged
CellEditEnding =dgTest_CellEditEnding
GotFocus =dgTest_GotFocusLostFocus =dgTest_LostFocus
GotKeyboardFocus =TextBoxGotKeyboardFocusLostKeyboardFocus =TextBoxLostKeyboardFocus
AutoGeneratingColumn = dgTest_AutoGeneratingColumn/>
我已经去添加一些代码到GotFocus事件。但它不适合我。任何帮助将是真正的价值。
CS:
private void dgTest_GotFocus(object sender,RoutedEventArgs e)
{
//查找源为DataGridCell
if(e.OriginalSource.GetType()== typeof(DataGridCell))
{
//开始行上的编辑;
DataGrid grd =(DataGrid)sender;
grd.BeginEdit(e);
}
}
尝试这样:
挂接DataGrid上的PreviewMouseLeftButtonDown事件:
< DataGrid.Resources>
< Style TargetType ={x:Type DataGridCell}>
< EventSetter
Event =PreviewMouseLeftButtonDown
Handeler =DataGridCell_PreviewMouseLeftButtonDown/>
< / Style>
< /DataGrid.Resources>
然后在后面的代码中:
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元素)其中T:UIElement
{
UIElement parent = element;
while(parent!= null)
{
T correctTyped =父作为T;
if(correctTyped!= null)
{
return correctTyped;
}
parent = VisualTreeHelper.GetParent(parent)as UIElement;
}
返回null;
}
I have DataGrid in my wpf application. It is having a few events. I need to edit the datagrid cell in single click. Presently it is altering when I double click the cell. I have taken a stab at something. However, it is not working for me. The rows and columns are not consistent. 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 had a go at adding some code to the "GotFocus" event. yet its not working for me. Any help would be truly valued.
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;
}
这篇关于DataGrid单元格单击编辑模式在wpf c#?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!