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

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

问题描述

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



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

  < DataGrid Name =dgMovement> 
...
< DataGridTemplateColumn标题=目标%>
< 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,如下所示:

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

发现静态类中包含的扩展方法这里



问题是,如何获取百分比编辑器,一旦我得到了DataGridCell?有谁能够帮助我?任何帮助将不胜感激。谢谢!

解决方案

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

 < DataGridTemplateColumn> 
< DataGridTemplateColumn.CellTemplate>
< DataTemplate>
< uc:Bogus x:Name =rootItemsSource ={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);






请注意,这通常不应该需要修改模板控件大部分可以使用数据绑定,附加属性和事件单独完成。一般来说,我将通过代码限制模板访问到自定义控件(通常有指定部分)。


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.

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>...

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

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天全站免登陆