WPF DataTrigger to show and hide XAML column column

I have a WPF application containing a grid. The grid is split into 3 columns, and the 3rd grid has zero width when loading.

I have two datagrids in two other columns. When a selected item in one of the datagrid data changes other changes in the datagrid, it displays the values, i.e. Main part template. All of this works great.

There is one value in the datagrid which, if selected, I want this third column to change its width from zero to 2 *. I do not know how to do that?

I want to achieve this through XAML. I watched data triggers and value converters. I quickly wrote the code below to check. I read that setting the column to width = 0 is probably higher on the priority list of dependency properties. Anyway, to do this or will I need to use the code?

<Grid> <Grid.ColumnDefinitions> <ColumnDefinition/> <ColumnDefinition Width="2*"/> <ColumnDefinition Width="0"/> </Grid.ColumnDefinitions> <DataGrid Grid.Column="0" ItemsSource="{Binding OrderList}" SelectedItem="{Binding OrderSelected}" AutoGenerateColumns="True"> </DataGrid> <TextBox Grid.Column="1" Text="{Binding OrderSelected.Name}"> </TextBox> <Grid x:Name="columnHideSeek" Grid.Column="2" Background="Blue"> <Grid.Style> <Style> <Style.Triggers> <DataTrigger Binding="{Binding OrderSelected.Name}" Value="Mark"> <Setter Property="Grid.Width" Value="10"/> </DataTrigger> </Style.Triggers> </Style> </Grid.Style> </Grid> </Grid> 
+6
source share
1 answer

Using the correct DataTrigger , this is entirely possible. First add the Trigger element to the user interface element you want to change ... ColumnDefinition , not 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> 

Then do not set Width in the ColumnDefinition element, but in Style . Otherwise, the Width element in the ColumnDefinition element ColumnDefinition override the value set in the DataTrigger .

+18
source

All Articles