I have a RadTreeView and trying to use stye triggers for disabling few nodes:
<telerikNavigation:RadTreeView ItemTemplate="{StaticResource HierarchyItemsTreeItemTemplate}">
<telerikNavigation:RadTreeView.Style>
<Style TargetType="telerikNavigation:RadTreeView">
<Style.Triggers>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=TreeViewItem}, Converter={StaticResource converter}}" Value="true">
<Setter Property="IsEnabled" Value="false"/>
</DataTrigger>
</Style.Triggers>
</Style>
</telerikNavigation:RadTreeView.Style>
</telerikNavigation:RadTreeView >
But this gives me error:
The attachable property Triggers was not found in Style
You don't seem to specify the property your Style should apply to which is the telerikNavigation:RadTreeView.Style. As of now you got a Style as the Content of the TreeView
Try:
<telerikNavigation:RadTreeView ItemTemplate="{StaticResource HierarchyItemsTreeItemTemplate}">
<telerikNavigation:RadTreeView.Style> <!-- Missing Line from original Code -->
<Style TargetType="telerikNavigation:RadTreeView">
<Style.Triggers>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource FindAncestor,
AncestorType=TreeViewItem},
Converter={StaticResource converter}}"
Value="true">
<Setter Property="IsEnabled"
Value="false" />
</DataTrigger>
</Style.Triggers>
</Style>
</telerikNavigation:RadTreeView.Style> <!-- Missing Line from original Code -->
</telerikNavigation:RadTreeView>
Related
Good Evening Everyone,
I am trying to change a WPF ListView ContextMenu header depending on a column value from said ListView. Specifically I want to change it from Enable to Disable (and vice-versa) when a column binded to the value of STATUS = "Y"(hold) or "N"(not on hold). I have tried the following:
<ContextMenu.Resources>
<Style TargetType="{x:Type MenuItem}">
<Style.Triggers>
<DataTrigger Binding="{Binding STATUS}" Value="Y">
<Setter Property="Header" Value="Enable" />
</DataTrigger>
<DataTrigger Binding="{Binding STATUS}" Value="N">
<Setter Property="Header" Value="Disable" />
</DataTrigger>
</Style.Triggers>
</Style>
</ContextMenu.Resources>
2ND Attempt:
<ListView.ContextMenu>
<ContextMenu x:Name="cmlv">
<MenuItem x:Name="cmdisableenable">
<MenuItem.Resources>
<Style TargetType="{x:Type MenuItem}">
<Style.Triggers>
<DataTrigger Binding="{Binding STATUS}" Value="Y">
<Setter Property="Header" Value="Enable" />
</DataTrigger>
<DataTrigger Binding="{Binding STATUS}" Value="N">
<Setter Property="Header" Value="Disable" />
</DataTrigger>
</Style.Triggers>
</Style>
</MenuItem.Resources>
</MenuItem>
</ContextMenu>
</ListView.ContextMenu>
<MenuItem>
<MenuItem.Resources>
<Style TargetType="{x:Type MenuItem}">
<Style.Triggers>
<DataTrigger Binding="{Binding STATUS}" Value="Y">
<Setter Property="Header" Value="Enable" />
</DataTrigger>
<DataTrigger Binding="{Binding STATUS}" Value="N">
<Setter Property="Header" Value="Disable" />
</DataTrigger>
</Style.Triggers>
</Style>
</MenuItem.Resources>
</MenuItem>
You have apply style on MenuItem so depends on status the header will be Disable or Enable and make sure u dont assign any value to header
I am a noob at Xaml triggers.
Do you see any obvious reason why this trigger does not work:
<TextBox Text="{Binding TiretNom}" Margin="1">
<TextBox.Style>
<Style>
<Style.Triggers>
<DataTrigger Binding="{Binding TiretNom}" Value="">
<Setter Property="TextBox.Background" Value="Tomato"/>
</DataTrigger>
<DataTrigger Binding="{Binding TiretNom}" Value="a">
<Setter Property="TextBox.BorderBrush" Value="Green"/>
</DataTrigger>
</Style.Triggers>
</Style>
</TextBox.Style>
</TextBox>
Neither does the backround change when the property TiretNom = "",
nor does the borderbrush gets red if TiretNom = "a"
Thx in advance.
I have a DataGrid with data binding to DataTable with columns set to auto generate.
The first column has boolean data type replaced with DataGridTemplateColumn with CheckBox from DataTemplate. It all works fine as it is.
However, now I want to make the DataGridCell background to red when the CheckBox is not checked.
The problem is, I have no idea how to set CheckBox's parent DataGridCell style with IsChecked trigger.
WPF:
<Window.Resources>
<DataGridTemplateColumn x:Key="colSelect">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<CheckBox Name="chkBxSelect"
HorizontalAlignment="Center"
VerticalAlignment="Center"
IsChecked="{Binding Path=Select, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
Click="chkBxSelect_Click">
</CheckBox>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
<DataGridTemplateColumn.HeaderTemplate>
<DataTemplate>
<CheckBox x:Name="chkBxSelectAll"
Content="Select"
HorizontalAlignment="Center"
VerticalAlignment="Center"
IsThreeState="True"
Click="chkBxSelectAll_Click"
IsChecked="{Binding RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}, Path=DataContext.SelectAll}">
</CheckBox>
</DataTemplate>
</DataGridTemplateColumn.HeaderTemplate>
<DataGridTemplateColumn.CellStyle>
<Style TargetType="{x:Type DataGridCell}">
<Style.Triggers>
<Trigger Property="IsSelected" Value="true">
<Setter Property="Foreground" Value="White"></Setter>
<Setter Property="Background" Value="DarkGray"></Setter>
<Setter Property="BorderBrush" Value="Red"></Setter>
<Setter Property="BorderThickness" Value="1"></Setter>
</Trigger>
</Style.Triggers>
</Style>
</DataGridTemplateColumn.CellStyle>
</DataGridTemplateColumn>
</Window.Resources>
C# while DataGrid Column AutoGenerating:
DataGridTemplateColumn col = (DataGridTemplateColumn)Resources["colSelect"];
e.Column = col;
e.Column.IsReadOnly = false;
Update:
So far I have figured out it could be done using RelativeSource and AncestorType in Binding. However, still trying to make it work.
Well after struggling a lot and not even trying the most obvious solution. I found it. It was relatively very simple and easy.
Just added DataTrigger to DataGridCell style and all done, WPF is magic.
<DataGridTemplateColumn.CellStyle>
<Style TargetType="{x:Type DataGridCell}">
<Style.Triggers>
<Trigger Property="IsSelected" Value="true">
<Setter Property="Foreground" Value="White"></Setter>
<Setter Property="Background" Value="DarkGray"></Setter>
<Setter Property="BorderBrush" Value="Red"></Setter>
<Setter Property="BorderThickness" Value="1"></Setter>
</Trigger>
<DataTrigger Binding="{Binding Path=Select, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Value="False">
<Setter Property="Background" Value="Red"/>
</DataTrigger>
</Style.Triggers>
</Style>
</DataGridTemplateColumn.CellStyle>
I have a dialog that can show information in different way. To achive that I use a ContentTemplate and DataTemplates. But I am getting some error when I try to define what DataTemplate should be used. I am doing the following:
<ContentControl Content="{Binding}" >
<ContentControl.Style>
<Style TargetType="ContentControl">
<Style.Triggers>
<DataTrigger Binding="{Binding UseImageTemplate}" Value="True">
<Setter Property="ContentTemplate" Value="{StaticResource ImageTemplate}"></Setter>
</DataTrigger>
<DataTrigger Binding="{Binding UseMessageTemplate}" Value="True">
<Setter Property="ContentTemplate" Value="{StaticResource MessageTemplate}"></Setter>
</DataTrigger>
<DataTrigger Binding="{Binding UseImageMessageTemplate}" Value="True">
<Setter Property="ContentTemplate" Value="{StaticResource ImageMessageTemplate}"></Setter>
</DataTrigger>
</Style.Triggers>
</Style>
</ContentControl.Style>
</ContentControl>
And the definition of DataTemplates are like this:
<DataTemplate x:Key="ImageTemplate">
<Grid>
<Image Source="{Binding Image}" Stretch="Uniform" Width="48" Height="48"></Image>
</Grid>
</DataTemplate>
Have I forget anything? When I execute this code, I am getting an InvalidOperationException. If I don't bind the Content of the ContentControl to the DataContext, I don't get any Exception but the information of my ViewModel is not shown. Any idea?
I have the DataTemplate below, which decides what .png to show based on the DirType. Currently, if we get DirType of 3, we show ./images/file.png
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding Path=DirType}" Value="0">
<Setter Property="Image.Source" TargetName="img" Value="./Images/MyComputer.jpg"></Setter>
</DataTrigger>
<DataTrigger Binding="{Binding Path=DirType}" Value="1">
<Setter Property="Image.Source" TargetName="img" Value="./Images/diskdrive.png"></Setter>
</DataTrigger>
<DataTrigger Binding="{Binding Path=DirType}" Value="2">
<Setter Property="Image.Source" TargetName="img" Value="./Images/folder.png"></Setter>
<Setter Property="Text" TargetName="ObjType" Value="File Folder"></Setter>
</DataTrigger>
<DataTrigger Binding="{Binding Path=DirType}" Value="3">
<Setter Property="Image.Source" TargetName="img" Value="./Images/file.png"></Setter>
<Setter Property="Text" TargetName="ObjType" Value="{Binding Ext}"></Setter>
<Setter Property="Visibility" TargetName="ObjSize" Value="Visible"></Setter>
<Setter Property="Text" TargetName="ObjSize" Value="{Binding Size}"></Setter>
</DataTrigger>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type ListBoxItem}},Path=IsSelected}" Value="True" >
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
Then I use it in a ListBox:
<ListBox x:Name="dirList" Grid.ColumnSpan="3" IsSynchronizedWithCurrentItem="True"
ItemTemplate="{StaticResource DirViewTemplate}" BorderThickness="0"
HorizontalContentAlignment="Left" VerticalContentAlignment="Top"
Grid.Column="0" Grid.Row="1"
ScrollViewer.HorizontalScrollBarVisibility="Disabled"
SelectedItem="{Binding Path=CurrentItem,Mode=OneWayToSource}"
MouseDoubleClick="dirList_MouseDoubleClick"
KeyDown="dirList_KeyDown">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel Orientation="Horizontal" ItemWidth="220"></WrapPanel>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
</ListBox>
Now, I want to dynamically change the image displayed for file type value = 3 based on their file types. Before all files have the same icon displayed in the list box but now I want them to pull the icons displayed in Windows explorer. I tried to access the DataTemplate dynamically while adding the items to the list, but I couldnt get it to work.
Any advice?
Use a MultiDataTrigger. This defines a number of conditions, all of which much evaluate to true for the trigger's setters to be applied.
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding Path=DirType}" Value="3" />
<Condition Binding="{Binding Path=Ext}" Value="txt" />
</MultiDataTrigger.Conditions>
<Setter TargetName="img" Property="Source" Value="./Images/txt-file.png" />
</MultiDataTrigger>
Here you have two conditions, one bound to the DirType property value, the other bound to the Ext property value. If DirType is "3" and Ext is "txt", then the setter will be applied (ie. the image source will change to use a different image file).
Just repeat the trigger in the XAML, changing the binding values, to deal with other file types.