【问题标题】:Identify auto generated TextBox识别自动生成的文本框
【发布时间】:2015-07-21 14:43:58
【问题描述】:

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

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

  • name1property1,property2
  • name2property1,property2
  • ...

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

我已准备好接受任何提示或解决方案...也许“自动生成数据网格”不是最好的决定?

编辑我的代码在后面。在这里你可以看到填充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; }
    }

【问题讨论】:

  • 您在寻找属性网格吗?如果是的话,看看here
  • DataGrid 没有可靠的方法来做到这一点。如果您发布您绑定的课程,我可以进一步帮助您。也许带有 HierarchicalDataTemplate 的 TreeView 会是一个不错的选择
  • @Muds 谢谢,我会看看这个!
  • @Dominik 看看我的编辑...也许对你有帮助?

标签: c# wpf datagrid textchanged


【解决方案1】:

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

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

【讨论】:

  • 它对我有用,但看起来有点冒险......但由于我是 C# 和 WPF 的新手,我无法评估这个解决方案:)......我会使用它!谢谢;)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-01-01
  • 2017-05-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多