I'm just trying to learn DataTriggers. What's wrong with this simple code? It tells me that "Source" is not a property of FrameworkElement.
<Image>
<Image.Triggers>
<DataTrigger Binding="{Binding Status}" Value="Sent">
<Setter Property="Source" Value="Resources\Approve_64_Trans.bmp"/>
</DataTrigger>
<DataTrigger Binding="{Binding Status}" Value="SendFailed">
<Setter Property="Source" Value="Resources\Cancel_64_Trans.bmp"/>
</DataTrigger>
</Image.Triggers>
</Image>
Also, is it correct to use a value "SendFailed" or "Sent" when the Status field is of enum type and SendFailed and Sent are two valid enum values?
You need to put that kind of trigger in a style, like so:
<Image>
<Image.Style>
<Style TargetType="Image">
<Style.Triggers>
<DataTrigger Binding="{Binding Status}" Value="Sent">
<Setter Property="Source" Value="Resources\Approve_64_Trans.bmp"/>
</DataTrigger>
<DataTrigger Binding="{Binding Status}" Value="SendFailed">
<Setter Property="Source" Value="Resources\Cancel_64_Trans.bmp"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Image.Style>
</Image>
Related
I want to detect when the selected item changes in a ListBox and a TreeView via DataTriggers
so this is my code
<TextBlock>
<TextBlock.Style>
<Style TargetType="TextBlock">
<Style.Triggers>
<DataTrigger Binding="{Binding ElementName=DirTreeView, Path=SelectedItemChanged}" Value="{x:Null}">
<Setter Property="Text" Value="{Binding ElementName=DirTreeView, Path=SelectedItem, Converter={StaticResource contentConverter}}"/>
</DataTrigger>
<DataTrigger Binding="{Binding ElementName=listbox, Path=SelectionChanged}" Value="{x:Null}">
<Setter Property="Text" Value="{Binding ElementName=listbox, Path=SelectedItem, Converter={StaticResource contentConverter}}"/>
</DataTrigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
</TextBlock>
if i comment out the last DataTrigger it works fine, but when I use both nothing works.
Why is that and how do I get this to work using both triggers?
This is the code i use but it is notworking
<Window.Resources>
<Style x:Key="PinkRow" TargetType="{x:Type DataGridRow}">
<Style.Triggers>
<DataTrigger Binding="{Binding Rank}" Value="Master">
<Setter Property="Source" Value="A_Cancel.png"/>
</DataTrigger>
<DataTrigger Binding="{Binding RANK}" Value="Bosun">
<Setter Property="Background" Value="Green" />
</DataTrigger>
<DataTrigger Binding="{Binding RANK}" Value="">
<Setter Property="Background" Value="yellow" />
</DataTrigger>
</Style.Triggers>
</Style>
</Window.Resources>
If field rank has the value "Master" i want the image A_cancel.png
The C# is case sensitive, so the Rank and RANK are different properties. Use proper property name in your bindings. Also you must mention in your Setter the TargetName for Image element.
<DataTrigger Binding="{Binding Rank}" Value="Master">
<Setter TargetName="ImageElementName" Property="Source" Value="A_Cancel.png"/>
</DataTrigger>
I am using a WPF Devexpress GridControl and I have a column that contains an image. I want to modify the Image using a DataTrigger. Here is the XAML:
<dxg:GridColumn Header="{Binding Source={StaticResource MainWindowResources}, Path=Resource.Status}"
FieldName="SaveStatus"
MinWidth="60"
Width="60"
ReadOnly="True"
>
<dxg:GridColumn.CellTemplate>
<DataTemplate>
<Image HorizontalAlignment="Center"
VerticalAlignment="Center"
Style="{StaticResource ImageStatusStyle}">
</Image>
</DataTemplate>
</dxg:GridColumn.CellTemplate>
</dxg:GridColumn>
and the style:
<Style TargetType="{x:Type Image}" x:Key="ImageStatusStyle">
<Style.Triggers>
<DataTrigger Binding="{Binding Path=Data.SaveStatus, UpdateSourceTrigger=
PropertyChanged}" Value="{x:Static enums:SaveState.DoneSuccesfuly}">
<Setter Property="Source" Value="..\Icons\StatusOk.png" />
</DataTrigger>
<DataTrigger Binding="{Binding Path=Data.SaveStatus, UpdateSourceTrigger=
PropertyChanged}" Value="{x:Static enums:SaveState.DoneUnsuccesfuly}">
<Setter Property="Source" Value="..\Icons\StatusError.png" />
</DataTrigger>
</Style.Triggers>
</Style>
This doesn't work. What am I doing wrong? Is there a better solution to this problem?
I managed to find the solution. The problem was the datatrigger binding instead of Data.SaveStatus should be RowData.Row.SaveStatus:
<Style.Triggers>
<DataTrigger Binding="{Binding Path=RowData.Row.SaveStatus, UpdateSourceTrigger=PropertyChanged}" Value="{x:Static enums:SaveState.DoneSuccesfuly}">
<Setter Property="Source" Value="..\Icons\StatusOk.png"></Setter>
</DataTrigger>
<DataTrigger Binding="{Binding Path=RowData.Row.SaveStatus, UpdateSourceTrigger=PropertyChanged}" Value="{x:Static enums:SaveState.DoneUnsuccesfuly}">
<Setter Property="Source" Value="..\Icons\StatusError.png"></Setter>
</DataTrigger>
</Style.Triggers>
Try accessing you images like this:
<Style TargetType="{x:Type Image}" x:Key="ImageStatusStyle">
<Style.Triggers>
<Setter Property="Source" Value="/ApplicationName;component/Icons/StatusOk.png" />
<DataTrigger Binding="{Binding Path=Data.SaveStatus, UpdateSourceTrigger=
PropertyChanged}" Value="{x:Static enums:SaveState.DoneUnsuccesfuly}">
<Setter Property="Source" Value="/ApplicationName;component/Icons/StatusError.png" />
</DataTrigger>
</Style.Triggers>
</Style>
I'm trying to define a DataTrigger for an Image element so that it shows a connected/disconnected image. I keep getting an Invalid PropertyDescriptor message. Any ideas?
<Image>
<Image.Style>
<Style>
<Style.Triggers>
<DataTrigger Binding="{Binding Source={x:Static my:Server.Instance}, Path=Connected, Mode=OneWay}"
Value="True">
<Setter Property="Source"
Value="serverconnected.png"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Image.Style>
</Image>
I just changed Property="Source" to Property="Image.Source" and it's working. Thanks everyone
Add a BitmapImage to your resources like so:
<BitmapImage x:Key="serverConnected" UriSource="serverconnected.png" />
And change your existing code to refer to it:
<Image>
<Image.Style>
<Style>
<Style.Triggers>
<DataTrigger Binding="{Binding Source={x:Static my:Server.Instance}, Path=Connected, Mode=OneWay}"
Value="True">
<Setter Property="Source"
Value="{StaticResource serverConnected}"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Image.Style>
This is my XAML:
<TextBlock Name="SeverityText"
Grid.Column="1"
Grid.Row="0"
Foreground="Red">
<TextBlock.Triggers>
<DataTrigger Binding="{Binding Path=Severity}">
<DataTrigger.Value>
<sm:Severity>Warning</sm:Severity>
</DataTrigger.Value>
<Setter TargetName="SeverityText"
Property="Foreground"
Value="Yellow" />
</DataTrigger>
<DataTrigger Binding="{Binding Path=Severity}">
<DataTrigger.Value>
<sm:Severity>Information</sm:Severity>
</DataTrigger.Value>
<Setter TargetName="SeverityText"
Property="Foreground"
Value="White" />
</DataTrigger>
</TextBlock.Triggers>
<TextBlock>Severity:</TextBlock>
<TextBlock Text="{Binding Path=Severity}" />
</TextBlock>
This is my error message:
Cannot find the static member 'ForegroundProperty' on the type 'ContentPresenter'.
sm:Severity is an enumeration I imported.
Your triggers and setters need to be defined in a style, rather than on the TextBlock directly:
<TextBlock>
<TextBlock.Style>
<Style TargetType="TextBlock">
<Style.Triggers>
<DataTrigger Binding="{Binding Severity}">
<DataTrigger.Value>
<sm:Severity>Warning</sm:Severity>
</DataTrigger.Value>
<Setter TargetName="SeverityText"
Property="Foreground"
Value="Yellow" />
</DataTrigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
</TextBlock>
Writing the full path of the property also works:
So
Property="Foreground" -> Property="TextBlock.Foreground"
However as suggested in the previous answer, you get:
System.InvalidOperationException: Triggers collection members must be of type EventTrigger.
...if you don't put it in a style.