简体   繁体   English

如何访问模板生成的元素WPF

[英]How to get access to template generated element wpf

I want to get access to TextBox named NameTextBox from my code below (element generated by template) in my xaml.cs file. 我想从我的xaml.cs文件中的以下代码(由模板生成的元素)中访问名为NameTextBox的TextBox。 Especially I want to get Text property. 特别是我想获得Text属性。 I want this property because want to identify the group after generation. 我需要此属性,因为要在生成后识别组。

<DataGrid x:Name="PersonDataGrid">
            <DataGrid.GroupStyle>
                <GroupStyle>
                    <GroupStyle.ContainerStyle>
                        <Style TargetType="{x:Type GroupItem}">
                            <Setter Property="Template">
                                <Setter.Value>
                                    <ControlTemplate TargetType="{x:Type GroupItem}">
                                        <Expander IsExpanded="False">
                                            <Expander.Header>
                                                <StackPanel Orientation="Horizontal">
                                                    <CheckBox x:Name="SelectGroupCheckBox" Checked="SelectGroupCheckBox_Checked"/>
                                                    <TextBlock x:Name="NameTextBox" Text="{Binding Name}"/>
                                                </StackPanel>
                                            </Expander.Header>
                                            <Expander.Content>
                                                <ItemsPresenter/>
                                            </Expander.Content>
                                        </Expander>
                                    </ControlTemplate>
                                </Setter.Value>
                            </Setter>
                        </Style>
                    </GroupStyle.ContainerStyle>
                </GroupStyle>

Or If you guys have other idea how to identify specific generated group. 或者,如果您还有其他想法,如何识别特定的生成组。 After generation it looks like this . 生成后看起来像这样 I want it because i want to if i check the group header checkbox has checked children checkboxes. 我想要它,因为我想如果我选中组标题复选框已选中子复选框。

In your xaml.cs file, you just need to use: 在您的xaml.cs文件中,您只需要使用:

NameTextBox.Text

But it seem's that you are using MVVM mode, in the ViewModel you just need to use the variable that you are binding: 但是似乎您正在使用MVVM模式,在ViewModel中,您只需要使用要绑定的变量:

Name

I change CheckBox code 我更改CheckBox代码

<CheckBox x:Name="SelectGroupCheckBox" 
              Content="{Binding Name}"
              Checked="SelectGroupCheckBox_Checked"
              Unchecked="SelectGroupCheckBox_Unchecked"/>

And in code behind 并在后面的代码中

private void SelectGroupCheckBox_Unchecked(object sender, RoutedEventArgs e)
{
            CheckBox checkBox = sender as CheckBox;
            checkBox.Content.ToString();
}

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

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