简体   繁体   中英

EditingElementStyle in a DataGridTextColumn

I need to be able to distinguish from which column in a DataGrid , a TextBox has been edited:

<DataGridTextColumn Header="No" Binding="{Binding NumberOfItems}">
    <DataGridTextColumn.EditingElementStyle>
        <Style TargetType="{x:Type TextBox}"  x:Name="NumOfItems">   
            <Setter Property="MaxLength" Value="2"/>
         </Style>
    </DataGridTextColumn.EditingElementStyle>
</DataGridTextColumn>

<DataGridTextColumn Header="Role" Binding="{Binding Role}" Width="0.75*">
    <DataGridTextColumn.EditingElementStyle>
        <Style TargetType="{x:Type TextBox}"  x:Name="Role">
            <Setter Property="MaxLength" Value="30"/>
         </Style>
    </DataGridTextColumn.EditingElementStyle>
</DataGridTextColumn>

so that I can update a database with the correct information. I have tried to use x:Name but, with the code below, the name is always empty

private void dgItems_CellEditEnding(object sender, 
DataGridCellEditEndingEventArgs e)
    {
        if(e.EditingElement is TextBox)
        {
             TextBox t = e.EditingElement as TextBox;
             ..........
             if (t.Name == "Role")
             //do this
             else if (t.NumOfItems)
             //do this
    }

Thanks for any help

You could set the Tag property in the Style :

<Style TargetType="{x:Type TextBox}">
    <Setter Property="MaxLength" Value="30"/>
    <Setter Property="Tag" Value="Role"/>
</Style>

if (t.Tag?.ToString() == "Role")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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