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

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

问题描述

我的wpf应用程序中有DataGrid。它有几个事件。我需要单击编辑datagrid单元格。当我双击单元格时,它正在改变。我已经刺伤了一些东西。但是,这对我来说并不奏效。行和列不一致。动态地,我将为Datagrid创建列。



这是我的代码..



Xaml:



$ _ code>< DataGrid Name =dgTestAutoGenerateColumns =TrueCanUserResizeColumns =FalseCanUserAddRows =False
ItemsSource ={Binding NotifyOnSourceUpdated = True}
Horizo​​ntalAlignment =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屋!

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