0

I am relatively new in WPF my XAML file is still more complex and confusing because some code is often repeated. Is there some easy way how to make code looks better and shorter? For example I have a DataGrid where DataGridColumnTemplate is always the same and only it's data source and header name are different.

  <DataGridTemplateColumn Header="Web">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <Grid>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="auto" />
                                <ColumnDefinition Width="auto" />
                            </Grid.ColumnDefinitions>
                            <CheckBox Grid.Column="0" Margin="4,0" IsChecked="{Binding Webs.IsSelected,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}">
                                <CheckBox.Style>
                                    <Style TargetType="CheckBox">
                                        <Style.Triggers>
                                            <DataTrigger Binding="{Binding Webs}" Value="{x:Null}">
                                                <Setter Property="IsEnabled" Value="false"></Setter>
                                            </DataTrigger>
                                        </Style.Triggers>
                                    </Style>
                                </CheckBox.Style>
                            </CheckBox>
                            <Button Grid.Column="1" Content="Detail" Margin="3,1" />
                        </Grid>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>

            <DataGridTemplateColumn Header="Flange">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <Grid>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="auto" />
                                <ColumnDefinition Width="auto" />
                            </Grid.ColumnDefinitions>
                            <CheckBox Grid.Column="0" Margin="4,0" IsChecked="{Binding Flanges.IsSelected,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}">
                                <CheckBox.Style>
                                    <Style TargetType="CheckBox">
                                        <Style.Triggers>
                                            <DataTrigger Binding="{Binding Flanges}" Value="{x:Null}">
                                                <Setter Property="IsEnabled" Value="false"></Setter>
                                            </DataTrigger>
                                        </Style.Triggers>
                                    </Style>
                                </CheckBox.Style>
                            </CheckBox>
                            <Button Grid.Column="1" Content="Detail" Margin="3,1" />
                        </Grid>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
Josef
  • 43
  • 7

2 Answers2

0

in your exact situation, when null value should disable control, there is a rude hack to bind IsEnabled directly and rely on TargetNullValue to replace nulls

<CheckBox Grid.Column="0" Margin="4,0" 
          IsChecked="{Binding Flanges.IsSelected,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
          IsEnabled="{Binding Path=Flanges, TargetNullValue=false}"/>

however this produces some binding errors when property is not null

"Value produced by BindingExpression is not valid for target property. target element is 'CheckBox'; target property is 'IsEnabled' (type 'Boolean')"

it makes sence to have a specialized value conveter to check null and use it in binding. it is much shorter than writing a DataTrigger

IsEnabled="{Binding Path=Flanges, Converter={StaticResource IsNullConverter}}"

see also related question: Null To Boolean IValueConverter not working

Community
  • 1
  • 1
ASh
  • 34,632
  • 9
  • 60
  • 82
0

Defining Styles is a common method to shorten your code and reuse styles repeatedly.
You can follow the links below to learn about WPF Styles:
Using WPF styles
Styling and Templating
Walkthrough: Styling WPF Content

Milad H.
  • 122
  • 6