WPF DataTrigger to display and hide grid column XAML

前端 未结 1 1517
眼角桃花
眼角桃花 2021-02-07 22:03

I have an WPF application that contains a grid. The grid is split into 3 columns with the 3rd grid having zero width upon loading.

I have two datagrids in the other two

相关标签:
1条回答
  • 2021-02-07 22:31

    Using the correct DataTrigger, this is totally possible. First, add the Trigger to the UI element that you want to change... the ColumnDefinition, not the Grid:

    <ColumnDefinition>
        <ColumnDefinition.Style>
            <Style TargetType="{x:Type ColumnDefinition}">
                <Setter Property="Width" Value="10" />
                <Style.Triggers>
                    <DataTrigger Binding="{Binding OrderSelected.Name}" Value="Mark">
                        <Setter Property="Width" Value="2*" />
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </ColumnDefinition.Style>
    </ColumnDefinition>
    

    Next, don't set the Width on the ColumnDefinition element, but in the Style instead. Otherwise the Width on the ColumnDefinition element will override the value set from the DataTrigger.

    0 讨论(0)
提交回复
热议问题