在数据网格单元内获得控制权 [英] Getting control inside a datagrid cell

查看:36
本文介绍了在数据网格单元内获得控制权的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的事情:

<DataGridTemplateColumn Header="Occurences" Width="Auto">
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding Path=Occurences, Converter={StaticResource ListConverter}, Mode=TwoWay}" Margin="5,5,10,5"/>
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
    <DataGridTemplateColumn.CellEditingTemplate>
        <DataTemplate>
            <myCustomControls:OccurencesManualEntry/>
        </DataTemplate>
    </DataGridTemplateColumn.CellEditingTemplate>
</DataGridTemplateColumn>

对于当前行,当我在此网格的另一列中时,是否可以获取 <myCustomControls:OccurencesManualEntry/> 以便我可以设置它的一些属性?

For the current row, When I am in ANOTHER column of this grid, Is it possible to get the <myCustomControls:OccurencesManualEntry/> so I can set some of its properties?

推荐答案

是否可以获得 <myCustomControls:OccurencesManualEntry/> 以便我可以设置它的一些属性?

Is it possible to get the <myCustomControls:OccurencesManualEntry/> so I can set some of its properties?

不,不是因为 OccurencesManualEntry 控件仅在Occurences"单元格处于编辑模式时才存在.

No, it's not since the OccurencesManualEntry control only exists when the "Occurences" cell is in edit mode.

您可以将依赖属性添加到 OccurencesManualEntry 控件并将其绑定到数据对象的源属性:

What you could is to add a dependency property to the OccurencesManualEntry control and bind this one to a source property of your data object:

<myCustomControls:OccurencesManualEntry SomeProperty="{Binding SourceProperty}"/>

然后您可以从另一个单元格设置此源属性:

You could then set this source property from another cell:

<DataGridTemplateColumn.CellEditingTemplate>
    <DataTemplate>
        <TextBox TextChanged="TextBox_TextChanged" />
    </DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>

<小时>

private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
{
    TextBox txtBox = sender as TextBox;
    var yourDataObject = txtBox.DataContext as YourDataClass;
    yourDataObject.SourceProperty = "value...";
}

确保数据类实现了 INotifyPropertyChanged 接口并在源属性的 setter 中引发 PropertyChanged 事件.

Make sure that the data class implements the INotifyPropertyChanged interface and raises the PropertyChanged event in the setter of the source property.

这篇关于在数据网格单元内获得控制权的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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