识别自动生成的文本框

Identify auto generated TextBox

我想构建一个可以更改每个值的简单属性视图。

属性按一个名称分组,如下所示:

所以我创建了一个带有模板的 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>

如您所见,我现在正在尝试添加一个 TextChanged 事件,但我的问题是:我从哪里获取 propertyGroupName 信息,因为我只需要更改 property2 来自特定的 propertyGroup.

我已准备好接受 任何 提示或解决方案...也许 'auto gen datagrid' 不是最好的决定?

编辑 我的代码在后面。在这里你可以看到页面填充 DataGrid 和我绑定的 class (注意方法 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; }
    }

您可以将 PropertyGroup 绑定到 TextBox 作为 Tag,然后您可以在事件处理程序中读取它并检查 属性 组名:

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

唯一的区别是 Tag="{Binding Path=.}"

事件处理程序:

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