Search code examples
c#wpfdatagridtextchanged

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
  • name2: property1,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 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.

I'm ready for any hint or solution... maybe the 'auto gen datagrid' isn't the best decision here?

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

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

Solution

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

    <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=.}".

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