MultiDataTrigger on two DataGrids - c#

I'm trying to have a button in a disabled state until both DataGrids have at least one selected item. Currently it remains in a Enabled state.
<Button MaxWidth="200" Grid.Column="0" x:Name="Add" Content="{x:Static UiStrings:Labels.Add}" >
<Button.Style>
<Style TargetType="Button">
<Style.Triggers>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding Path=SelectedIndex, ElementName=EconomicFactors}" Value="-1"></Condition>
<Condition Binding="{Binding Path=SelectedIndex, ElementName=TargetTypes}" Value="-1"></Condition>
</MultiDataTrigger.Conditions>
<Setter Property="IsEnabled" Value="False"/>
</MultiDataTrigger>
</Style.Triggers>
</Style>
</Button.Style>
</Button>
Regards,

In MultiDataTrigger all conditions has to be true. I think simplest solution is to replace MultiDataTrigger with two DataTrigger
<Style.Triggers>
<DataTrigger Binding="{Binding Path=SelectedIndex, ElementName=EconomicFactors}" Value="-1">
<Setter Property="IsEnabled" Value="False"/>
</DataTrigger>
<DataTrigger Binding="{Binding Path=SelectedIndex, ElementName=TargetTypes}" Value="-1">
<Setter Property="IsEnabled" Value="False"/>
</DataTrigger>
</Style.Triggers>

Related

trying to change background color of rows in a datagrid based on multicell values

I need help binding this WPF to multiple datatriggers. I need to change row color based on two values not just the one. I wasn't able to figure out how to do that
<DataGrid.RowStyle>
<Style TargetType="DataGridRow">
<Style.Triggers>
<DataTrigger Binding="{Binding AgentState}" Value="AUX-IN">
<Setter Property="Background" Value="Green" />
<Setter Property="Foreground" Value="White"/>
</DataTrigger>
</Style.Triggers>
</Style>
</DataGrid.RowStyle>
<DataGrid.RowStyle>
<Style TargetType="DataGridRow">
<Style.Triggers>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding AgentState}" Value="ACD-IN"/>
<Condition Binding="{Binding AuxReasons}" Value="General"/>
</MultiDataTrigger.Conditions>
<Setter Property="Background" Value="Green" />
<Setter Property="Foreground" Value="White"/>
</MultiDataTrigger>
</Style.Triggers>
</Style>
</DataGrid.RowStyle>

Setting Property when No DataTrigger Values are Satisfied WPF

I am VERY new to WPF AND C#, so there may be a much better way to accomplish what I am attempting. Therefore, I am open to other methods.
As far as what I've got, I am trying to program a Digital VMB (Visual Management Board) for the maintenance department where I work. They want a section to display the number of days our plant has gone without an accident: "Days Safe". I successfully have the binding set for this TextBlock:
<TextBlock Text="{Binding DaysSafe}" FontSize="60" FontWeight="Bold" HorizontalAlignment="Center" VerticalAlignment="Center" >
I have a series of DataTriggers to change the Foreground color of the text to a certain color, based on the value of the text. Basically, I want the DaysSafe text to be:
Red when 0
Orange when 1 and so on (you can see the colors in my code below):
<StackPanel VerticalAlignment="Center" >
<TextBlock Text="{Binding DaysSafe}" FontSize="60" FontWeight="Bold" HorizontalAlignment="Center" VerticalAlignment="Center" >
<TextBlock.Style>
<Style TargetType="{x:Type TextBlock}">
<Style.Triggers >
<DataTrigger Binding="{Binding DaysSafe}" Value="0">
<Setter Property="Foreground" Value="Red"/>
</DataTrigger>
<DataTrigger Binding="{Binding DaysSafe}" Value="1">
<Setter Property="Foreground" Value="OrangeRed" />
</DataTrigger>
<DataTrigger Binding="{Binding DaysSafe}" Value="2">
<Setter Property="Foreground" Value="Orange" />
</DataTrigger>
<DataTrigger Binding="{Binding DaysSafe}" Value="3">
<Setter Property="Foreground" Value="Yellow" />
</DataTrigger>
<DataTrigger Binding="{Binding DaysSafe}" Value="4">
<Setter Property="Foreground" Value="Yellow" />
</DataTrigger>
<DataTrigger Binding="{Binding DaysSafe}" Value="5">
<Setter Property="Foreground" Value="GreenYellow" />
</DataTrigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
</TextBlock>
</StackPanel>
This functions correctly, the colors change when the days are lower. HOWEVER, I would like for the foreground color to "Default" to green when the value of DaysSafe is over 5. Right now, any value above 5 is black when I want it to be green. I tried adding the foreground="green" attribute in the first TextBlock section, but this overrides the DataTriggers and the foreground is always green.
Any help with how I may create a default value for the foreground?
Thanks.
You could just add a setter that sets the default Foreground of the TextBlock to Green:
<Style TargetType="{x:Type TextBlock}">
<Setter Property="Foreground" Value="Green" />
<Style.Triggers >
<DataTrigger Binding="{Binding DaysSafe}" Value="0">
<Setter Property="Foreground" Value="Red"/>
</DataTrigger>
<DataTrigger Binding="{Binding DaysSafe}" Value="1">
<Setter Property="Foreground" Value="OrangeRed" />
</DataTrigger>
<DataTrigger Binding="{Binding DaysSafe}" Value="2">
<Setter Property="Foreground" Value="Orange" />
</DataTrigger>
<DataTrigger Binding="{Binding DaysSafe}" Value="3">
<Setter Property="Foreground" Value="Yellow" />
</DataTrigger>
<DataTrigger Binding="{Binding DaysSafe}" Value="4">
<Setter Property="Foreground" Value="Yellow" />
</DataTrigger>
<DataTrigger Binding="{Binding DaysSafe}" Value="5">
<Setter Property="Foreground" Value="GreenYellow" />
</DataTrigger>
</Style.Triggers>
</Style>

Changing background colour of cells if value is greater than or equal to

I have this DataGrid and I have been playing with setting the background colour of selected cells:
<DataGridTextColumn Header="Next Study" Binding="{Binding NextStudy}">
<DataGridTextColumn.ElementStyle>
<Style TargetType="{x:Type TextBlock}">
<Style.Triggers>
<Trigger Property="Text" Value="25">
<Setter Property="Background" Value="Red"/>
</Trigger>
</Style.Triggers>
</Style>
</DataGridTextColumn.ElementStyle>
<DataGridTextColumn.CellStyle>
<Style TargetType="{x:Type DataGridCell}">
<Setter Property="ToolTip" Value="{Binding NextStudyDescription}" />
</Style>
</DataGridTextColumn.CellStyle>
</DataGridTextColumn>
It works as you can see:
But that is not really what I want. Instead I would like to highlight all cells where the value is greater than or equal to 18. So I tried:
<DataGridTextColumn Header="Next Study" Binding="{Binding NextStudy}">
<DataGridTextColumn.ElementStyle>
<Style TargetType="{x:Type TextBlock}">
<Style.Triggers>
<DataTrigger Binding="{NextStudy Converter={StaticResource IsEqualOrGreaterThanConverter}, ConverterParameter=18}" Value="True">
<Setter Property="Background" Value="Red"/>
</DataTrigger>
</Style.Triggers>
</Style>
</DataGridTextColumn.ElementStyle>
<DataGridTextColumn.CellStyle>
<Style TargetType="{x:Type DataGridCell}">
<Setter Property="ToolTip" Value="{Binding NextStudyDescription}" />
</Style>
</DataGridTextColumn.CellStyle>
</DataGridTextColumn>
It doesn't like: <DataTrigger Binding="{NextStudy
In addition, I would like to do this background test if the element comboActiveStudentAssignmentType selected index is 0, 1 or 2. Otherwise it does not need to do this highlighting.
Thanks.
You have a syntax error: Binding is the property name, you still have to declare it as a Binding
<DataTrigger Binding="{Binding NextStudy, Converter={StaticResource IsEqualOrGreaterThanConverter}, ConverterParameter=18}" Value="True">
As for the second part of the question, you can use a MultiDataTrigger. The conditions in the MultiDataTrigger have to ALL be true for the trigger to execute the setters. You will probably need to write another converter to transform your AssignmentType to True/False and you should be set.
Here's a quick example:
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding NextStudy, Converter={StaticResource IsEqualOrGreaterThanConverter}, ConverterParameter=18}" Value="True"/>
<Condition Binding="{Binding comboActiveStudentAssignmentType, Converter={StaticResource YourOtherConverter}" Value="True"/>
</MultiDataTrigger.Conditions>
<MultiDataTrigger.Setters>
<Setter Property="Background" Value="Red"/>
</MultiDataTrigger.Setters>
</MultiDataTrigger>

Multidata trigger condition for listbox to check if an item is selected

I want to disable a button if there are no items in the listbox.
I am now confused on which property I should check in the Multidata trigger condition for the listbox in order to check whether there is any element in the listbox. Please help.
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding ElementName=JobItemsListBox, Path=ItemsSource}"
Value="">
</Condition>
</MultiDataTrigger.Conditions>
<Setter Property="IsEnabled" Value="False" />
</MultiDataTrigger>
qqww2 comment is correct -- this works
<Style TargetType="Button">
<Setter Property="IsEnabled" Value="True" />
<Style.Triggers>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding ElementName=listBox1, Path=Items.Count}" Value="0" />
</MultiDataTrigger.Conditions>
<Setter Property="IsEnabled" Value="False" />
</MultiDataTrigger>
</Style.Triggers>
</Style>

Different selection colors for different DataGrid rows

I'm trying to set different colors to different rows in datagrid, based on Type of the row and whether it is selected:
<Style TargetType="DataGridCell" x:Key="ActiveCellStyle">
<Setter Property="Foreground" Value="Black"/>
<Style.Triggers>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding Type}" Value="0"/>
<Condition Property="IsSelected" Value="False"/>
</MultiDataTrigger.Conditions>
<Setter Property="Background" Value="#FFDFE6ED"/>
</MultiDataTrigger>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding Type}" Value="0"/>
<Condition Property="IsSelected" Value="True"/>
</MultiDataTrigger.Conditions>
<Setter Property="Background" Value="#FF6CAFF1"/>
</MultiDataTrigger>
...
</Style.Triggers>
</Style>
When I run this, I'm getting ArgumentException: key cannot be null.
How can I fix this?
With MultiTrigger you use Property and with MultiDataTrigger you use Binding, you can't combine them. So change
<Condition Property="IsSelected"
to
<Condition Binding="{Binding RelativeSource={RelativeSource Self},
Path=IsSelected}"
and it should work
<Style TargetType="DataGridCell" x:Key="ActiveCellStyle">
<Setter Property="Foreground" Value="Black"/>
<Style.Triggers>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding Type}" Value="0"/>
<Condition Binding="{Binding RelativeSource={RelativeSource Self},
Path=IsSelected}"
Value="False"/>
</MultiDataTrigger.Conditions>
<Setter Property="Background" Value="#FFDFE6ED"/>
</MultiDataTrigger>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding Type}" Value="0"/>
<Condition Binding="{Binding RelativeSource={RelativeSource Self},
Path=IsSelected}"
Value="True"/>
</MultiDataTrigger.Conditions>
<Setter Property="Background" Value="#FF6CAFF1"/>
</MultiDataTrigger>
</Style.Triggers>
</Style>

Categories

Resources