简体   繁体   English

如何在用户控制代码中获取模板中使用的控件?

[英]How to get control used in template in user control code?

How could I get a DataGrid control used in xaml style template to use it in code? 如何在xaml样式模板中使用DataGrid控件在代码中使用它?

<UserControl>
<UserControl.Resources>
    <Style x:Key="MyComboBoxStyle" TargetType="{x:Type ComboBox}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type ComboBox}">
                    <Grid x:Name="MainGrid" SnapsToDevicePixels="true">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="*" />
                            <ColumnDefinition Width="0" MinWidth="{DynamicResource {x:Static SystemParameters.VerticalScrollBarWidthKey}}" />
                        </Grid.ColumnDefinitions>
                        <Popup x:Name="PART_Popup"
                               Grid.ColumnSpan="2">
                            <Microsoft_Windows_Themes:SystemDropShadowChrome x:Name="Shdw">

                                <Border x:Name="DropDownBorder">

                                    <DataGrid x:Name="PART_PopupDataGrid" />

                                </Border>
                            </Microsoft_Windows_Themes:SystemDropShadowChrome>
                        </Popup>
                        <ToggleButton Grid.ColumnSpan="2"
                                      Background="{TemplateBinding Background}"
                                      BorderBrush="{TemplateBinding BorderBrush}"
                                      IsChecked="{Binding Path=IsDropDownOpen,
                                                          Mode=TwoWay,
                                                          RelativeSource={RelativeSource TemplatedParent}}"
                                      Style="{StaticResource ComboBoxReadonlyToggleButton}" />
                        <ContentPresenter Margin="{TemplateBinding Padding}"
                                          HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                                          VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                                          Content="{TemplateBinding SelectionBoxItem}"
                                          ContentTemplate="{TemplateBinding SelectionBoxItemTemplate}"
                                          ContentTemplateSelector="{TemplateBinding ItemTemplateSelector}"
                                          IsHitTestVisible="false"
                                          SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</UserControl.Resources>

    <Grid>
        <ComboBox Name="ComboGrid" Style="{DynamicResource DataGridComboBoxStyle}" />
    </Grid>
</UserControl>

There is how I tried to get DataGrid control in user control, but not successfully: 我试图在用户控件中尝试获取DataGrid控件,但没有成功:

var v1 = this.FindName("PART_PopupDataGrid");
var v2 = this.Template.FindName("PART_PopupDataGrid", this);
var v3 = ComboGrid.FindName("PART_PopupDataGrid");

How could I get this control in code? 我怎么能在代码中得到这个控件?

This is a very common requirement. 这是一个非常常见的要求。 In fact, it's so common that Microsoft even have a page on MSDN specifically for this: 事实上,微软甚至在MSDN上专门为此设置了一个页面:

How to: Find ControlTemplate-Generated Elements 如何:查找ControlTemplate生成的元素

Basically, if you have a reference to the ComboBox that the ControlTemplate is applied to (let's call it ComboBox ), then you should be able to do this: 基本上,如果您对应用ControlTemplateComboBox有一个引用(让我们称之为ComboBox ),那么您应该能够这样做:

DataGrid dataGrid = 
    ComboBox.Template.FindName("PART_PopupDataGrid", ComboBox) as DataGrid;

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

相关问题 如何在代码中设置控制模板? - How to set Control Template in code? 如何在列表框数据模板项中获取用户控件 - How to get User Control in listbox data template item 如何获得对用户控件中存在的表Layoutpanel的每个单元格的控制,以及如何在另一个winform中使用该用户控件? - How to get control of each cell of a table Layoutpanel present in a usercontrol and that user control is used in another winform? 用户控件用作类 - User control used as a class 在用作模板时在属性中捕获 WPF 用户控件的数据 - Capturing a WPF user control's data in a property when used as a template 如何使注册用户控件在C#中进行编码? - How to get the registered user control to code behind in c#? 后台代码中使用的控件模板可动态添加到StackPanel - Control Template used in Code Behind to add dynamically to a StackPanel 创建一个Web用户控件并在Web窗体上使用?,以及如何在首页上获取ddl,日历(在ascx上使用)的值 - create a web user control and used on web form?, and How to get value of ddl, calendar(used on ascx) on main page WPF中的用户控件与控件模板 - User control vs Control Template in WPF 用户控件项模板未绑定的FlowLayout控件 - FlowLayout Control With User Control Item Template Not Binding
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM