简体   繁体   English

识别自动生成的TextBox

[英]Identify auto generated TextBox

I want to build a simple properties view where one can change each value. 我想构建一个简单的属性视图,在其中可以更改每个值。

The properties are grouped by one name like this: 这些属性按如下名称进行分组:

  • name1 : property1,property2 名称1属性1,属性2
  • name2 : property1,property2 name2property1,property2
  • ... ...

So I created a DataGrid with a template to fill the grid (note that I removed every style property etc. and the text values are also just examples): 因此,我创建了一个带有模板的DataGrid来填充网格(请注意,我删除了所有样式属性等,并且文本值也只是示例):

<DataGrid Name="propertyGrid">
    <DataGrid.Columns>
        <DataGridTemplateColumn Header="Property Group Name">
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding propertyGroupName}" />
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>
        <DataGridTemplateColumn Header="Property 1">
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <TextBox Text="{Binding property1}" />
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>
        <DataGridTemplateColumn Header="Property 2">
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <TextBox Text="{Binding property2}" TextChanged="TextBox_TextChanged" />
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>
    </DataGrid.Columns>
</DataGrid>

As you can see I'm now trying to add a TextChanged event and there is my problem: Where do I get the propertyGroupName information from, since I only need to change the property2 from a specific propertyGroup . 正如你所看到的,现在我想添加一个TextChanged事件,并有我的问题:我在哪里得到propertyGroupName从信息,因为我只需要改变property2从特定propertyGroup

I'm ready for any hint or solution... maybe the 'auto gen datagrid' isn't the best decision here? 我已经准备好提出任何提示或解决方案...也许“ auto gen datagrid”不是这里的最佳决定?

Edit My code behind. 在后面编辑我的代码。 Here you can see the page filling the DataGrid and the class I am binding to (note that the method GetPropertyX is just reading my property file): 在这里,您可以看到填充DataGrid和我要绑定到的类的页面(请注意,方法GetPropertyX只是在读取我的属性文件):

    public PropertiesPage()
    {
        InitializeComponent();

        List<PropertyGroup> properties = new List<PropertyGroup>();
        properties.Add(GetPropertyGroup("propertyGroup1"));
        properties.Add(GetPropertyGroup("propertyGroup2"));
        properties.Add(GetPropertyGroup("propertyGroup3"));

        propertyGrid.ItemsSource = properties;

    }

    private PropertyGroup GetPropertyGroup(string propertyGroupName)
    {
        return new CarrierConfig()
        {
            PropertyGroupName = propertyGroupName,
            Property1 = GetProperty1(propertyGroupName),
            Property2 = GetProperty2(propertyGroupName)
        };
    }

    public class PropertyGroup
    {
        public string PropertyGroupName { get; set; }
        public string Property1 { get; set; }
        public string Property2 { get; set; }
    }

You can bind the PropertyGroup to the TextBox as Tag , then you can read it in the event handler and check the property group name: 您可以将PropertyGroup作为Tag绑定到TextBox ,然后可以在事件处理程序中读取它并检查属性组名称:

<DataGrid Name="propertyGrid">
<DataGrid.Columns>
    <DataGridTemplateColumn Header="Property Group Name">
        <DataGridTemplateColumn.CellTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding propertyGroupName}" />
            </DataTemplate>
        </DataGridTemplateColumn.CellTemplate>
    </DataGridTemplateColumn>
    <DataGridTemplateColumn Header="Property 1">
        <DataGridTemplateColumn.CellTemplate>
            <DataTemplate>
                <TextBox Text="{Binding property1}" />
            </DataTemplate>
        </DataGridTemplateColumn.CellTemplate>
    </DataGridTemplateColumn>
    <DataGridTemplateColumn Header="Property 2">
        <DataGridTemplateColumn.CellTemplate>
            <DataTemplate>
                <TextBox Text="{Binding property2}" Tag="{Binding Path=.}" TextChanged="TextBox_TextChanged" />
            </DataTemplate>
        </DataGridTemplateColumn.CellTemplate>
    </DataGridTemplateColumn>
</DataGrid.Columns>

The only difference is Tag="{Binding Path=.}" . 唯一的区别是Tag="{Binding Path=.}"

Event handler: 事件处理程序:

private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
    {
        var textbox = (sender as TextBox);
        if ((textbox.Tag as PropertyGroup).PropertyGroupName == "the name you want")
        {
            //do stuff
        }
    }

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

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