简体   繁体   English

从DataTemplate中使用其标识名称访问控件

[英]Access a control from within a DataTemplate with its identifying name

In my WPF application I have a ComboBox control that is located inside a Grid Control. 在我的WPF应用程序中,我有一个位于网格控件内的ComboBox控件。 In XAML I am assigning a name to the ComboBox: 在XAML中,我为ComboBox分配了一个名称:

<DataGridTemplateColumn Header="Status">
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <TextBlock VerticalAlignment="Center" Text="{Binding name_ru}" Width="Auto" />
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
    <DataGridTemplateColumn.CellEditingTemplate>
        <DataTemplate>
            <ComboBox Name="stcom" Style="{DynamicResource ComboBoxStyle}" SelectionChanged="status_SelectionChanged" Height="auto" Width="Auto">
                 <ComboBox.BorderBrush>
                     <SolidColorBrush Color="{DynamicResource Color1}"/>
                 </ComboBox.BorderBrush>
            </ComboBox>
        </DataTemplate>
    </DataGridTemplateColumn.CellEditingTemplate>
</DataGridTemplateColumn>

With the method FindName(string) I am trying to refer to the ComboBox with its associated name: 我试图使用FindName(string)方法来引用具有相关名称的ComboBox:

ComboBox stcom
        {
            get
            {
                return (ComboBox)FindName("stcom");
            }
        }


 if (stcom != null)
            {
                stcom.ItemsSource = list;
            }

But obviously the control can not be found because the reference stcom remains null. 但是显然找不到控件,因为参考stcom保持为空。

The question now is how to refer to my ComboBox using its name property ? 现在的问题是如何使用其name属性引用我的ComboBox?

The answer is: 答案是:

<Style x:Key="CheckBoxStyle1" TargetType="{x:Type CheckBox}">
  <Setter Property="Template">
   <Setter.Value>
    <ControlTemplate TargetType="{x:Type CheckBox}">
     <StackPanel Orientation="Horizontal">
      <Grid>
       <TextBlock Name="tbUserIcon" Text="t1" />
       <TextBlock Name="tbCheck"  Text="✓" />
      </Grid>
     </StackPanel>
    </ControlTemplate>
   </Setter.Value>
  </Setter>
 </Style>

and C#: 和C#:

checkBox.ApplyTemplate();
var tbUserIcon= (TextBlock)checkBox.Template.FindName("tbUserIcon", checkBox);

don't forget the checkBox.ApplyTemplate() be fore Template.FindName() it's important! 不要忘记checkBox.ApplyTemplate()在Template.FindName()之前,这一点很重要!

First you have to get access to the control template which it has been applied to, then you can find an element of the template by name. 首先,您必须有权访问已应用到该控件模板的控件,然后才能按名称查找模板的元素。 Have a look at the MSDN knowledge base : 看一下MSDN知识库:

You can't access controls that are part of a DataTemplate with their name. 您无法使用名称访问属于DataTemplate的控件。

You can try to read about some workarounds for example 您可以尝试阅读一些解决方法,例如

You can also have a look at the dozens of posts here on SO issuing this topic for example 您也可以在此处查看有关发布此主题的数十篇文章

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM