从 DataGridCell 获取控件 [英] Getting a control from a DataGridCell

查看:28
本文介绍了从 DataGridCell 获取控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我在 DataGridTemplateColumn 中有一个任意控件,我想知道如何获取控件,因为我已经检索到包含该控件的 DataGridCell.

Assuming that I have an arbitrary control inside a DataGridTemplateColumn, I wish to know how to get the control, given that I have retrieved the DataGridCell which contains that control.

我的包含 DataGrid 的 XAML 文件如下:

My XAML file containing the DataGrid is as follows:

    <DataGrid Name="dgMovement">
...    
    <DataGridTemplateColumn Header="Target %">
       <DataGridTemplateColumn.CellTemplate>
          <DataTemplate>
            <vi:PercentageEditor Value="{Binding TargetPercentage, Mode=TwoWay,
                      UpdateSourceTrigger=PropertyChanged}" Width="100px"  
                      cal:Message.Attach="[Event PreviewLostKeyboardFocus] = [Action ChangeTargetPercentage];[Event PreviewGotKeyboardFocus] = [Action OnFocus]" 
                      Name="aa" />
          </DataTemplate>
       </DataGridTemplateColumn.CellTemplate>
    </DataGridTemplateColumn>...

我使用扩展方法检索了 DataGridCell,如下所示:

I retrieved the DataGridCell using extension methods as follows:

DataGridCell cell = view.dgMovement2.GetCell(index, 6);

包含在静态类中的扩展方法位于 这里

The extension methods, contained in a static class is found here

问题是,一旦我获得了 DataGridCell,如何检索PercentageEditor"?有谁能够帮我?任何帮助将不胜感激.谢谢!

The question is, how to I retrieve the "PercentageEditor", once I got the DataGridCell? Can anybody help me? Any help would be greatly appreciated. Thanks!

推荐答案

您可以使用控件的名称在模板中找到它,例如

You can use the name of the control to find it in the template, e.g.

<DataGridTemplateColumn>
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <uc:Bogus x:Name="root" ItemsSource="{Binding Machines}"/>
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>

var cell = dataGrid.GetCell(5, 0);
var cp = (ContentPresenter)cell.Content;
var bogus = (Bogus)cp.ContentTemplate.FindName("root", cp);

<小时>

但是请注意,这通常不需要,因为在大多数情况下修改模板化控件可以单独使用数据绑定、附加属性和事件来完成.一般来说,我会通过代码限制对自定义控件的模板访问(通常有 指定部分).


Note however that this usually should not be necessary as modifying templated controls for the most part can be done using data binding, attached properties and events alone. In general i would restrict template access via code to custom controls (which often have designated parts).

这篇关于从 DataGridCell 获取控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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