数据网格元素名称上的样式触发器

时间:2016-09-12 07:27:54

标签: c# wpf

我想在window.resources标签内设置我的样式 我需要在DatagridTextColumn上设置maxlength属性,但我找不到可行的解决方案。

Windows资源:

<Window.Resources>
    <Style x:Key="CellEditStyle" TargetType="{x:Type TextBox}">
        <Setter Property="BorderThickness" Value="0"/>
        <Setter Property="Padding" Value="0"/>
        <Style.Triggers>
            <DataTrigger Binding="{??? ElementName=RefFourn ???}">
                <Setter Property="MaxLength" Value="19"/>
            </DataTrigger>
            <Trigger Property="Validation.HasError" Value="true">
                <Setter Property="ToolTip" Value="{Binding RelativeSource={RelativeSource Self}, Path=(Validation.Errors)[0].ErrorContent}"/>
            </Trigger>
        </Style.Triggers>
    </Style>
</Window.Resources>

xaml datagrid:

<DataGrid>
<DataGrid.Columns>
<DataGridTextColumn x:Name="RefFourn" Header="Ref Fourn" EditingElementStyle="{StaticResource CellEditStyle}" Binding="{Binding Path=RefFourn, ValidatesOnDataErrors=True, NotifyOnValidationError=True}">
</Datagrid.Columns
</Datagrid>

1 个答案:

答案 0 :(得分:-2)

如下所示更改您的风格:

  <Style x:Key="CellEditStyle" TargetType="{x:Type TextBox}">
        <Setter Property="BorderThickness" Value="0"/>
        <Setter Property="MaxLength" Value="19"/>
        <Setter Property="Padding" Value="0"/>
        <Style.Triggers>               
            <Trigger Property="Validation.HasError" Value="true">
                <Setter Property="ToolTip" Value="{Binding RelativeSource={RelativeSource Self}, Path=(Validation.Errors)[0].ErrorContent}"/>
            </Trigger>
        </Style.Triggers>
    </Style>