How to use targetname with usercontrol and trigger - c#

I made UserControl of DataGrid. I placed this new component into page1.xaml. I would like to use some template and setting based on value in Data1.
Could you help me with this code how to avoid the error message?
<my:MyDataGrid Grid.Column="1" Grid.Row="1" HorizontalAlignment="Left" Margin="29,295,0,0" Name="myDataGrid1"
VerticalAlignment="Top" Height="151" Width="176" SelectionChanged="myDataGrid1_SelectionChanged">
<my:MyDataGrid.Columns>
<DataGridTemplateColumn Header="Col1" >
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Data1}" x:Name="mytext" />
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding Data1}" Value="1">
<Setter TargetName="mytext" Property="Foreground" Value="Red" />
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</my:MyDataGrid.Columns>
</my:MyDataGrid>
I got error message:
Cannot set Name attribute value 'mytext' on element 'TextBlock'.
'TextBlock' is under the scope of element 'MyDataGrid', which already
had a name registered when it was defined in another scope.

You can add you data trigger to a style attached to a TextBlock
<my:MyDataGrid Grid.Column="1" Grid.Row="1" ...>
<DataGrid.Columns>
<DataGridTemplateColumn Header="Col1" >
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Data1}" x:Name="mytext">
<TextBlock.Style>
<Style TargetType="TextBlock">
<Style.Triggers>
<DataTrigger Binding="{Binding Data1}" Value="1">
<Setter Property="Foreground" Value="Red" />
</DataTrigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
</TextBlock>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</my:MyDataGrid>

Related

Appearance of Textbox within a view column shall be identical to the appearance of list rows

My ListView contains a textbox per column to change data. Via Style my Listview rows are colored in different colors depending on the alternation index.
I want my textboxes within the listview to look like the entire line.
Actually its like this:
The lines should cover the textboxes.
The Code to color the ListView Rows:
<Style x:Key="DifAlternationColorsLV" TargetType="{x:Type ListViewItem}">
<Style.Triggers>
<Trigger Property="ItemsControl.AlternationIndex" Value="0">
<Setter Property="Background" Value="#FFB1E4EF"></Setter>
</Trigger>
<Trigger Property="ItemsControl.AlternationIndex" Value="1">
<Setter Property="Background" Value="#FFD4EBFF"></Setter>
</Trigger>
</Style.Triggers>
</Style>
The code in the XAML of the View:
<Border Grid.Row="1" Grid.Column="0"
Margin="10,10,10,10"
BorderBrush="#FF474A57"
CornerRadius="10,10,10,10"
BorderThickness="2,2,2,2"
Width="300"
MaxHeight="200"
Background="White">
<StackPanel Margin="0,0,0,20" Orientation="Vertical">
<StackPanel Grid.Column="0" Grid.RowSpan="1"
Grid.Row="1"
VerticalAlignment="Top">
<Label HorizontalAlignment="Center" FontWeight="Bold">
Schichten
</Label>
<ListView x:Name="Shift" MinHeight="150" MaxHeight="150" MinWidth="250" HorizontalContentAlignment="Stretch" HorizontalAlignment="Center"
IsSynchronizedWithCurrentItem="True"
ItemContainerStyle="{StaticResource DifAlternationColorsLV}"
AlternationCount="2"
ItemsSource="{Binding UiShiftHModelList, UpdateSourceTrigger=PropertyChanged}"
SelectedItem="{Binding SelectedLine, UpdateSourceTrigger=PropertyChanged}" d:ItemsSource="{d:SampleData ItemCount=3}">
<ListView.View>
<GridView>
<GridView.Columns>
<GridViewColumn Header="Bezeichnung" Width="Auto">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBox x:Name="Bezeichnung" MinWidth="100"
Style="{StaticResource TBoxInListV}"
Text="{Binding Bezeichnung, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
BorderThickness="0">
</TextBox>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="Tagmuster" Width="80">
<GridViewColumn.CellTemplate>
<DataTemplate>
<!---->
<TextBox x:Name="Tagmuster" MinWidth="80"
Background="{Binding ElementName=Shift}"
Style="{StaticResource TBoxInListV}"
Text="{Binding TagmusterID, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
BorderThickness="0">
</TextBox>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="Edit" Width="40">
<GridViewColumn.CellTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" >
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Button x:Name="SaveShiftH"
Style="{StaticResource BtnListSave}"
Grid.Column="0">
</Button>
<Button x:Name="UpdateShiftH"
Style="{StaticResource BtnListUpdate}"
Grid.Column="0">
</Button>
<Button x:Name="DeleteShiftH"
Style="{StaticResource BtnListDelete}"
Grid.Column="1">
</Button>
<Button x:Name="ReopenShiftH"
Style="{StaticResource BtnListReopen}"
Grid.Column="0">
</Button>
</Grid>
</StackPanel>
<DataTemplate.Triggers>
<!--Visibility of the Buttons-->
<DataTrigger Binding="{Binding EditModus}" Value="0">
<Setter TargetName="SaveShiftH" Property="Visibility" Value="Visible"/>
</DataTrigger>
<DataTrigger Binding="{Binding EditModus}" Value="2">
<Setter TargetName="UpdateShiftH" Property="Visibility" Value="Visible"/>
</DataTrigger>
<DataTrigger Binding="{Binding EditModus}" Value="1">
<Setter TargetName="UpdateShiftH" Property="Visibility" Value="Collapsed"/>
</DataTrigger>
<DataTrigger Binding="{Binding EditModus}" Value="2">
<Setter TargetName="DeleteShiftH" Property="Visibility" Value="Visible"/>
</DataTrigger>
<DataTrigger Binding="{Binding EditModus}" Value="1">
<Setter TargetName="DeleteShiftH" Property="Visibility" Value="Collapsed"/>
</DataTrigger>
<!--<DataTrigger Binding="{Binding EditModus}" Value="0">
<Setter TargetName="Delete" Property="Visibility" Value="Collapsed"/>
</DataTrigger>-->
<DataTrigger Binding="{Binding EditModus}" Value="1">
<Setter TargetName="ReopenShiftH" Property="Visibility" Value="Visible"/>
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView.Columns>
</GridView>
</ListView.View>
</ListView>
</StackPanel>
</StackPanel>
</Border>
If I change the Background Binding from the Textbox to:
Background="{Binding ElementName=Shift}"
The Line appears exactly I want it to but leads to a lot binding errors.
Any ideas?
You can make the background of the TextBox wholly or partially transparent; that will make the background of the row visible:
<TextBox x:Name="Bezeichnung" MinWidth="100" Background="#60FFFFFF"
Style="{StaticResource TBoxInListV}"
Text="{Binding Bezeichnung, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
BorderThickness="0">
</TextBox>

Add Custom Rows at the top of DataGrid - WPF

I am new in WPF and I have a project that needs to have this output below
How can I do this? I just know a basic DataGrid code like this below:
<DataGrid AutoGenerateColumns="False" Name="myGrid" Margin="10">
<DataGrid.RowHeaderTemplate>
<DataTemplate>
<TextBlock Text="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type DataGridRow}}, Path=Item.Header}"/>
</DataTemplate>
</DataGrid.RowHeaderTemplate>
<DataGrid.Columns>
<DataGridTextColumn Header="Case Title" Binding="{Binding Path=Name}" Width="160" />
<DataGridComboBoxColumn Width="100" x:Name="dtgCbxColUnit" SelectedValueBinding="{Binding Unit, Mode=TwoWay}" DisplayMemberPath="{Binding Unit}" />
<DataGridTextColumn Header="Case 1" Binding="{Binding Path=Case1}" Width="80"/>
<DataGridTextColumn Header="Case 2" Binding="{Binding Path=Case2}" Width="80" />
<DataGridTextColumn Header="Case 3" Binding="{Binding Path=Case3}" Width="80" />
<DataGridTextColumn Header="Case 4" Binding="{Binding Path=Case4}" Width="80" />
<DataGridTextColumn Header="Case 5" Binding="{Binding Path=Case5}" Width="80" />
<DataGridTextColumn Header="Case 6" Binding="{Binding Path=Case6}" Width="80" />
</DataGrid.Columns>
</DataGrid>
How can I resolve this?
The DataGrid cell templating is done vertically (by columns) not horizontally (by rows). It means you can't make an exception for a few rows. If you want to have custom rows you have to apply it to all rows using DataGridTemplateColumn
<DataGrid.Columns>
<DataGridTemplateColumn Header="People">
<DataGridTemplateColumn.CellStyle>
<Style TargetType="DataGridCell">
<!-- normal template -->
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<TextBlock Text="{Binding A}" Background="Green"/>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<DataTrigger Binding="{Binding RowType}" Value="0">
<Setter Property="Template">
<Setter.Value>
<!-- first extra template -->
<ControlTemplate>
<TextBlock Text="{Binding A}"/>
</ControlTemplate>
</Setter.Value>
</Setter>
</DataTrigger>
<DataTrigger Binding="{Binding RowType}" Value="1">
<Setter Property="Template">
<Setter.Value>
<!-- second extra template -->
<ControlTemplate>
<CheckBox Content="{Binding A}"/>
</ControlTemplate>
</Setter.Value>
</Setter>
</DataTrigger>
</Style.Triggers>
</Style>
</DataGridTemplateColumn.CellStyle>
</DataGridTemplateColumn>
</DataGrid.Columns>
On the other hand DataGrid "styling" is done the opposite way using RowStyle. So you can write a trigger to customize one row completely with the cost of ignoring all cells (or columns) in that row:
<DataGrid.RowStyle>
<Style TargetType="DataGridRow">
<Style.Triggers>
<DataTrigger Binding="{Binding RowType}" Value="0">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<Button Content="{Binding A}"/>
</ControlTemplate>
</Setter.Value>
</Setter>
</DataTrigger>
</Style.Triggers>
</Style>
</DataGrid.RowStyle>
combined results:
Normally I would suggest to use your own implementation of the general ItemsControl if you think these customizations will grow in the future.
There is another workaround where you create a gap at the top of the grid and overlay stuff onto it using a Canvas or any control with ClipToBounds=False explained here

WPF MVVM DataGrid contents don't fit to row height change

I need to make my row height variable so that I can allow for some rows to add additional information. Setting the RowHeight value doesn't seem to make any difference. There is no value to set for height at the level of DataGridTextColumn as all the contents are bound (MVVM).
<Border Grid.Row="1"
Grid.Column="1"
HorizontalAlignment="Right" Margin="9" Width="auto" Visibility="{Binding LogVisibility}" VerticalAlignment="Stretch">
<DataGrid AutoGenerateColumns="False" VerticalContentAlignment="Center" ItemsSource="{Binding EventLog}" RowHeight="100" Background="White" CellStyle="{StaticResource cellStyle}" ColumnHeaderStyle="{StaticResource headerStyle}" CanUserAddRows="False">
<DataGrid.Columns>
<DataGridTemplateColumn Header="Type" SortMemberPath="CategoryDescription">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Image Source="{Binding Image}" MaxHeight="15" MaxWidth="15" VerticalAlignment="Center"/>
<TextBlock Text=" "/>
<TextBlock Text="{Binding CategoryDescription}" TextWrapping="Wrap"/>
</StackPanel>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<!--<DataGridTextColumn Header="Type" Binding="{Binding CategoryDescription}"></DataGridTextColumn>-->
<DataGridTextColumn Header="Date" Binding="{Binding Date}"/>
<DataGridTextColumn Header="Details" Binding="{Binding TypeDescription}" MaxWidth="400"/>
</DataGrid.Columns>
</DataGrid>
</Border>
Having set the value of RowHeight="{x:Static sys:Double.NaN}" doesn't change anything and instead I see truncated text as seen here:
If I set an arbitrary fixed height of RowHeight="100" (although not ideal), the rows content doesnt expand either, and show an ugly outline:
I added vertical scrolling, but I don't need horizontal scrolling, so I was hoping to have variable height so that longer text would wrap and fit, how can I achieve this?
Update (solution) - Thanks to Nomad developer
There was an offender style at the top of my XAML that applied to all the cells and was restricting them from expanding:
<Style TargetType="DataGridCell" x:Key="cellStyle" >
<Setter Property="FontFamily" Value="Segoe UI" />
<Setter Property="FontSize" Value="14"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type DataGridCell}">
<Grid Background="{TemplateBinding Background}">
<ContentPresenter VerticalAlignment="Center" Margin="5,5,5,5" />
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
<Setter Property="TextBlock.VerticalAlignment" Value="Center"/>
<Setter Property="Margin" Value="0" />
<Setter Property="BorderThickness" Value="0"/>
<Setter Property="Height" Value="35"/>
<Setter Property="VerticalContentAlignment" Value="Center"/>
</Style>
I have removed this style now, and the final Datagrid is (using <DataGridTextColumn.ElementStyle>):
<DataGrid AutoGenerateColumns="False"
VerticalContentAlignment="Center"
ItemsSource="{Binding EventLog}"
MinRowHeight="30"
Background="White"
ColumnHeaderStyle="{StaticResource headerStyle}"
CanUserAddRows="False">
<DataGrid.Columns>
<DataGridTemplateColumn Header="Type" SortMemberPath="CategoryDescription">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Image Source="{Binding Image}" MaxHeight="15" MaxWidth="15" VerticalAlignment="Center" Margin="5,0,5,0"/>
<TextBlock Text="{Binding CategoryDescription}" TextWrapping="Wrap" VerticalAlignment="Center" Margin="0,0,5,0"/>
</StackPanel>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTextColumn Header="Date" Binding="{Binding Date}">
<DataGridTextColumn.ElementStyle>
<Style>
<Setter Property="TextBlock.TextWrapping" Value="Wrap"/>
<Setter Property="TextBlock.TextAlignment" Value="Justify" />
<Setter Property="TextBlock.VerticalAlignment" Value="Center"/>
<Setter Property="TextBlock.Margin" Value="5"/>
</Style>
</DataGridTextColumn.ElementStyle>
</DataGridTextColumn>
<DataGridTextColumn Header="Details" Binding="{Binding TypeDescription}" MaxWidth="400">
<DataGridTextColumn.ElementStyle>
<Style>
<Setter Property="TextBlock.TextWrapping" Value="Wrap"/>
<Setter Property="TextBlock.TextAlignment" Value="Justify" />
<Setter Property="TextBlock.VerticalAlignment" Value="Center"/>
<Setter Property="TextBlock.Margin" Value="5"/>
</Style>
</DataGridTextColumn.ElementStyle>
</DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>
Which helped me achieve this:
Yes that's possible if you add custom style with textblock wrapping for your DataGridTextColumn
<DataGridTextColumn.ElementStyle>
<Style>
<Setter Property="TextBlock.TextWrapping" Value="Wrap" />
</Style>
</DataGridTextColumn.ElementStyle>
All you need is add TextWrapping as Wrap or WrapWithOverflow You can check out the difference here.
Also to make it work, you need to remove your RowHeight or you can change it from
RowHeight="100"
to
MinRowHeight="100"
It ensures that your rows height will at least be 100, and if text doesnt fit - can go longer in size for that specific row, but with original rowheight it cant change size and has fixed 100 height for all rows. Btw, 100 seems to be too high, may be 20-ish would be neat.
Give a try to this code, I also added TextAlignment to Justify just in case you might find it useful.
<Border Grid.Row="1"
Grid.Column="1"
Width="auto"
Margin="9"
HorizontalAlignment="Right"
VerticalAlignment="Stretch"
Visibility="{Binding LogVisibility}">
<DataGrid VerticalContentAlignment="Center"
AutoGenerateColumns="False"
Background="White"
CanUserAddRows="False"
CellStyle="{StaticResource cellStyle}"
ColumnHeaderStyle="{StaticResource headerStyle}"
ItemsSource="{Binding EventLog}"
MinRowHeight="20">
<DataGrid.Columns>
<DataGridTemplateColumn Header="Type"
SortMemberPath="CategoryDescription">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Image MaxWidth="15"
MaxHeight="15"
VerticalAlignment="Center"
Source="{Binding Image}" />
<TextBlock Text=" " />
<TextBlock Text="{Binding CategoryDescription}"
TextWrapping="Wrap" />
</StackPanel>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<!--<DataGridTextColumn Header="Type" Binding="{Binding CategoryDescription}"></DataGridTextColumn>-->
<DataGridTextColumn Binding="{Binding Date}"
Header="Date" />
<DataGridTextColumn MaxWidth="400"
Binding="{Binding TypeDescription}"
Header="Details">
<DataGridTextColumn.ElementStyle>
<Style>
<Setter Property="TextBlock.TextWrapping" Value="Wrap" />
<Setter Property="TextBlock.TextAlignment" Value="Justify" />
</Style>
</DataGridTextColumn.ElementStyle>
</DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>
</Border>

WPF DataGrid trigger on other cell value

I have DataGrid:
<DataGrid x:Name="PART_DataGrid"
ItemsSource="{TemplateBinding Items}"
AutoGenerateColumns="False"
Margin="2,25,2,2">
<DataGrid.Columns>
<DataGridTemplateColumn Header="Language" Width="SizeToCells" IsReadOnly="False">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox SelectedItem="{Binding Path=Language, Mode=OneWay}"
ItemsSource="{Binding LanguagesSource, RelativeSource={RelativeSource AncestorType=UserControl}}"
FontSize="16"
IsEnabled="False">
<ComboBox.Style>
<Style TargetType="ComboBox">
<Style.Triggers>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGridRow}}, Path=test }" Value="">
<Setter Property="IsEnabled" Value="True"/>
</DataTrigger>
</Style.Triggers>
</Style>
</ComboBox.Style>
</ComboBox>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="Translation" Width="SizeToCells" IsReadOnly="False">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBox x:Name="TranslationValueTB"
Text="{Binding Path=Value, UpdateSourceTrigger=PropertyChanged}"
BorderThickness="0"
FontSize="16" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
And I want to set that first column ComboBox will be enabled when second TextBox.Text is empty, I have tried different binding methods but none of them have worked. So how can I do that?
Bind to the Value source property that the TextBox in the second column is bound to:
<DataGridTemplateColumn Header="Language" Width="SizeToCells" IsReadOnly="False">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox SelectedItem="{Binding Path=Language, Mode=OneWay}"
ItemsSource="{Binding LanguagesSource, RelativeSource={RelativeSource AncestorType=UserControl}}"
FontSize="16"
IsEnabled="False">
<ComboBox.Style>
<Style TargetType="ComboBox">
<Style.Triggers>
<DataTrigger Binding="{Binding Value}" Value="{x:Null}">
<Setter Property="IsEnabled" Value="True"/>
</DataTrigger>
<DataTrigger Binding="{Binding Value}" Value="">
<Setter Property="IsEnabled" Value="True"/>
</DataTrigger>
</Style.Triggers>
</Style>
</ComboBox.Style>
</ComboBox>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
Also make sure that the class where the Value property is defined implements the INotifyPropertyChanged interface and raises the PropertyChanged event in the setter of the Value property.
Remove the IsEnabled = false from combobox and do the same in the setter as follows, and do the binding as like follows,
<ComboBox.Style>
<Style TargetType="ComboBox">
<Setter Property="IsEnabled" Value="False"/>
<Style.Triggers>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGridRow}}, Path=Item.Name }" Value="{x:Static sys:String.Empty}">
<Setter Property="IsEnabled" Value="True"/>
</DataTrigger>
</Style.Triggers>
</Style>
</ComboBox.Style>
Item.Name - > this is the property you need to check

Style DataTrigger not evaluating on initial load

Title says it all
<Page.Resources>
<local:BoolToVisibilityConverter x:Key="BoolToVisibilityConverter"/>
<DataTemplate x:Key="level1">
<StackPanel Orientation="Vertical">
<TextBlock x:Name="GroupTitle" FontSize="16" FontWeight="Bold" Foreground="#FF000532" Text="{Binding name}"/>
<TextBlock x:Name="GroupDescription" Text="{Binding description}"/>
<TextBlock x:Name="Depth" Text="{Binding childDepth}"/>
<ItemsControl ItemsSource="{Binding settings}">
</ItemsControl>
</StackPanel>
</DataTemplate>
<DataTemplate x:Key="level2">
<StackPanel Orientation="Vertical">
<TextBlock x:Name="GroupTitle" FontSize="16" FontWeight="Bold" Foreground="#FF000532" Text="{Binding name}"/>
<TextBlock x:Name="GroupDescription" Text="{Binding description}"/>
<TextBlock x:Name="Depth" Text="{Binding childDepth}"/>
<ItemsControl ItemsSource="{Binding settings}">
</ItemsControl>
<TabControl ItemsSource="{Binding groups}">
<TabControl.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding name}" />
</DataTemplate>
</TabControl.ItemTemplate>
<TabControl.ContentTemplate>
<DataTemplate>
<ContentPresenter Content="{Binding}" ContentTemplate="{StaticResource level1}"/>
</DataTemplate>
</TabControl.ContentTemplate>
</TabControl>
</StackPanel>
</DataTemplate>
</Page.Resources>
<Grid>
<ContentControl x:Name="SettingsDisplayer" Content="{Binding settingGroup, ElementName=page}">
<ContentControl.Resources>
<local:BoolToVisibilityConverter x:Key="BoolToVisibilityConverter"/>
</ContentControl.Resources>
<ContentControl.Style>
<Style TargetType="{x:Type ContentControl}">
<Setter Property="ContentTemplate" Value="{StaticResource level1}" />
<Style.Triggers>
<DataTrigger Binding="{Binding Path=childDepth}" Value="1">
<Setter Property="ContentTemplate" Value="{StaticResource level1}" />
</DataTrigger>
<DataTrigger Binding="{Binding Path=childDepth}" Value="2">
<Setter Property="ContentTemplate" Value="{StaticResource level2}" />
</DataTrigger>
<DataTrigger Binding="{Binding Path=childDepth}" Value="3">
<Setter Property="ContentTemplate" Value="{StaticResource level3}" />
</DataTrigger>
<DataTrigger Binding="{Binding Path=childDepth}" Value="4">
<Setter Property="ContentTemplate" Value="{StaticResource level4}" />
</DataTrigger>
</Style.Triggers>
</Style>
</ContentControl.Style>
</ContentControl>
</Grid>
The getter for childDepth is not even being accessed because I put a console trace in there to know when it is. The property is populated as it shows the proper amount when seen in the Depth TextBlock in the DataTemplates.
The relevant property in my class:
public int childDepth { get { Console.WriteLine(this.getDepth()); return this.getDepth(); } }
No mater what the childDepth is it alwayse skipps the triggers and just uses the level1 template.
I feel silly, So apparently binding the ContentControl's Content is not the equivalent of binding data to the ContentControl, thus I needed to target my DataTrigger Bindings more specifically at my original DP since it doesn't just take the parent containers binding.
This worked:
<DataTrigger Binding="{Binding settingGroup.childDepth, ElementName=page}" Value="1">
<Setter Property="ContentTemplate" Value="{StaticResource level1}" />
</DataTrigger>

Categories