I'm trying to use a special binding for comparisons found here and recommended on another question. NEQ is an added operator that just gives the opposite result of EQ. An InvalidOperationException is thrown every time with the message "Must have non-null value for 'Binding'".
I've tried reducing the statements to just {Binding SourceExpanded} in the condition for testing and even that throws the same exception.
Source, SourceExpanded, and SourceCollapsed are all dependency properties defined in the class this style is associated with.
Is the issue, that you can't bind to a dependency property with a null value? And if so, why is the property value not allowed to be null?
<Style.Triggers>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Property="IsExpanded" Value="True"/>
<Condition Binding="{local:ComparisonBinding SourceCollapsed, NEQ, {x:Null}}" Value="{x:Null}"/>
</MultiDataTrigger.Conditions>
<Setter Property="Source" Value="{Binding SourceExpanded}"/>
</MultiDataTrigger>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Property="IsExpanded" Value="False"/>
<Condition Binding="{local:ComparisonBinding SourceCollapsed, NEQ, {x:Null}}" Value="{x:Null}"/>
</MultiDataTrigger.Conditions>
<Setter Property="Source" Value="{Binding SourceCollapsed}"/>
</MultiDataTrigger>
</Style.Triggers>
I know it is kind of late, you must have solved your problem, if not, here is how to solve it.
You need to change
<Condition Property="IsExpanded" Value="True"/> to
<Condition Binding="{Binding IsExpanded, RelativeSource={RelativeSource Self}}" Value="True" />
Building upon Ray's and this answer: for the MultiDataTrigger Condition element, use explicit Binding instead of Property, and define a default value if null, using TargetNullValue. Here is the code:
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding IsMouseOver, TargetNullValue=''}" Value="True"/>
</MultiDataTrigger.Conditions>
<Setter Property="Foreground" Value="Black" />
</MultiDataTrigger>
Related
This is my Multitrigger:
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding Path=Dringlichkeit, RelativeSource={RelativeSource Self}}" Value="Normal"/>
<Condition Property="ItemsControl.AlternationIndex" Value="0"/>
</MultiDataTrigger.Conditions>
<Setter Property="Background" Value="LightPink"/>
</MultiDataTrigger>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding Path=Dringlichkeit, RelativeSource={RelativeSource Self}}" Value="Normal"/>
<Condition Property="ItemsControl.AlternationIndex" Value="1"/>
</MultiDataTrigger.Conditions>
<Setter Property="Background" Value="Pink"/>
</MultiDataTrigger>
If I use this trigger as a DataTrigger, the binding works.
But I need the AlternationIndex. I thought it was because I was loading the data via SQL, but when I load the style after SQL, I still have the problem that the binding has a NULL value.
From documentation I can read that you should refer to your AlterationIndex like so:
{Binding RelativeSource={RelativeSource Self}, Path=(ItemsControl.AlternationIndex)}
MSDN Doc
HTH
I have datagrid in my view and I am trying to trigger a style for the DataGridRowHeader so that it has a particular background when both of the following are true:
IsDirty=True (Property on the DataContext of the row)
IsRowSelected=True (Property on the DataGridRowHeader)
How do I write a multi-trigger that triggers for the above paired conditions as my following style code throws InvalidOperationException/{"Must have non-null value for 'Property'."}:
<Style x:Key="DataGridStandardRowHeaderStyle" TargetType="DataGridRowHeader">
<Style.Triggers>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Binding="{Binding IsDirty}" Value="True" />
<Condition Property="IsRowSelected" Value="True" />
</MultiTrigger.Conditions>
<Setter Property="Background" Value="LightYellow" />
</MultiTrigger>
</Style.Triggers>
</Style>
Kindly help me out.
The mistake in my style code finally got across to me and the correct one that now works for me is given below:
<Style x:Key="DataGridStandardRowHeaderStyle" TargetType="DataGridRowHeader">
<Style.Triggers>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding IsDirty}" Value="True" />
<Condition Binding="{Binding IsRowSelected, RelativeSource={RelativeSource Self}}" Value="True" />
</MultiDataTrigger.Conditions>
<Setter Property="Background" Value="LightYellow" />
</MultiDataTrigger>
</Style.Triggers>
</Style>
I am working with some multi-trriggers as per this posting. The binding and error are below.
I am using a custom markup extension to display the resource image so it could be suspect but I don't think so since I have used it in styles before.
The error message says I am applying the wrong property for the type and I don't see why yet.
Cheers,
Berryl
triggers
<Style x:Key="AvatarPathImageStyle" TargetType="{x:Type Image}">
<Setter Property="Source" Value="{Binding AvatarPath}"/>
<Setter Property="Height" Value="96"/>
<Setter Property="Width" Value="96"/>
<Setter Property="Stretch" Value="UniformToFill"/>
<Style.Triggers>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding AvatarPath}" Value="{x:Null}" />
<Condition Binding="{Binding Gender}" Value="{x:Static domain:Gender.Female}"/>
</MultiDataTrigger.Conditions>
<Setter Property="Source" Value="{resx:Resx ResxName=Smack.Parties.Presentation.Resources.PersonDetailView, Key=Img_Female}"/>
</MultiDataTrigger>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding AvatarPath}" Value="{x:Null}" />
<Condition Binding="{Binding Gender}" Value="{x:Static domain:Gender.Male}"/>
</MultiDataTrigger.Conditions>
<Setter Property="Source" Value="{resx:Resx ResxName=Smack.Parties.Presentation.Resources.PersonDetailView, Key=Img_Male}"/>
</MultiDataTrigger>
</MultiDataTrigger>
</Style.Triggers>
</Style>
binding
<Image Grid.Column="1" Grid.Row="4"
HorizontalAlignment="Center" Margin="10, 0" Style="{StaticResource AvatarPathImageStyle}"
/>
error
System.Windows.Markup.XamlParseException occurred
... InnerException: System.ArgumentException
Message='System.Windows.Controls.Image' is not a valid value for the 'System.Windows.Controls.Image.Source' property on a Setter.
...
update
Accessing the image statically also gets an Invalid Type error:
<Setter Property="Source" Value="{x:Static imgResources:PersonDetailView.Img_Female}"/>
Comment converted to answer:
The reason for the error is that images stored in a resx file are of type System.Windows.Drawing.Image, which are not directly compatible with WPF. You can write a converter, but unless you have a strong reason for using a resource dictionary I would just embed the images and reference them by URL relative to the XAML file
I'm having trouble with a Condition for a MultiTrigger. If I do the following:
<Condition Binding="{Binding RelativeSource={RelativeSource
AncestorType={x:Type ListView}}}" Property="IsEnabled" Value="True"/>
Then I get this exception:
Condition cannot use both Property and Binding. Error at object 'System.Windows.Condition' in markup file
However, when I do the following:
<Condition Binding="{Binding RelativeSource={RelativeSource
AncestorType={x:Type ListView}}, Path=IsEnabled}" Value="True"/>
Then I get this exception:
Must specify both Property and Value for Trigger. Error at object 'System.Windows.Condition' in markup file
What gives? If it matters, here's the entire trigger:
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Binding="{Binding Path=IsSelected}" Value="True"/>
<Condition Binding="{Binding Path=ItemsControl.AlternationIndex}"
Value="0"/>
<Condition Binding="{Binding RelativeSource={RelativeSource
AncestorType={x:Type ListView}}, Path=IsEnabled}"
Value="True"/>
</MultiTrigger.Conditions>
<Setter Property="Background"
Value="{StaticResource evenSelected}" />
<Setter Property="BorderBrush"
Value="{StaticResource evenSelectedBorder}" />
</MultiTrigger>
The API in this case is confusing. Condition is used for two different types of multi-triggers, and the properties used are different. When using MultiTrigger, you will use the Property and Value properties. When using MultiDataTrigger (which is what you need), you specify a Binding and a Value. So, if you just switch your code to use a MultiDataTrigger, you'll be good to go:
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding Path=IsSelected}" Value="True"/>
<Condition Binding="{Binding Path=ItemsControl.AlternationIndex}"
Value="0"/>
<Condition Binding="{Binding RelativeSource={RelativeSource
AncestorType={x:Type ListView}}, Path=IsEnabled}"
Value="True"/>
</MultiDataTrigger.Conditions>
<Setter Property="Background"
Value="{StaticResource evenSelected}" />
<Setter Property="BorderBrush"
Value="{StaticResource evenSelectedBorder}" />
</MultiDataTrigger>
I've been struggling with this code for some time now and can't seem to find any complete answers to my question. I've created a small sample to illustrate the problem:
<ListView >
<ListView.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Margin="0,0,20,0" IsItemsHost="True" />
</ItemsPanelTemplate>
</ListView.ItemsPanel>
<ListView.Items>
<TextBlock>Test1</TextBlock>
<TextBlock>Test2</TextBlock>
<TextBlock>Test3</TextBlock>
<TextBlock>Test4</TextBlock>
<TextBlock>Test5</TextBlock>
</ListView.Items>
<ListView.ItemContainerStyle>
<Style TargetType="{x:Type ListViewItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ListViewItem}">
<Grid>
<ContentPresenter/>
</Grid>
<ControlTemplate.Triggers>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Binding="{Binding RelativeSource={RelativeSource Self}, Path=IsMouseOver}" Value="True" />
<Condition Property="IsSelected" Value="True"/>
</MultiTrigger.Conditions>
<Setter Property="Visibility" Value="Collapsed"/>
</MultiTrigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ListView.ItemContainerStyle>
</ListView>
According to the MultiTrigger settings, the selected item should reappear when the mouse is no longer over the selected item. This code, however, produces an InvalidOperationException with the message "Must have non-null value for 'Property'." If you remove the Condition that uses the "Binding" attribute the exception is not thrown. In the MSDN documentation it states that you must have either the Property or Binding attribute set. The above code functions like the Binding attribute is not set. In fact, in all my test cases, it doesn't matter what the Binding attribute is set to; the exception is still thrown. Any thoughts?
This is one of those times when you have to suck it up and admit that you've made a bonehead mistake. However, to save some other unlucky soul from the same fate, I'll reveal my epiphany.
First, if I had read all of the documentation I would have read the part that said if you're using the condition's "Binding" attribute, it needs to be included in a MultiDataTrigger element (instead of the MutiTrigger element in my posted example).
Second, upon making those changes, the MultiTrigger element is replace with the following code:
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding RelativeSource={RelativeSource Self}, Path=IsMouseOver}" Value="True" />
<Condition Binding="{Binding RelativeSource={RelativeSource Self}, Path=IsSelected}" Value="True"/>
</MultiDataTrigger.Conditions>
<Setter Property="Visibility" Value="Collapsed"/>
</MultiDataTrigger>
Now the example works but because the selected item is collapsed, the trigger condition switches back and forth causing the selected item to flicker in and out of view. Makes sense but admittedly not what I intended.
At any rate, hope this helps someone from making the same bonehead mistake!
On a very similar note, pulling IsMouseOver from a border as the main data template content, and pulling the IsSelected from the Ancestor. Its interesting that both conditions have to have a relative path, I would assume that the default path would be the local datacontext. Thanks for the above solution.
Broken Code
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding Mode=OneWay, RelativeSource={RelativeSource AncestorType={x:Type ListBoxItem}}, Path=IsSelected}"
Value="True" />
<Condition SourceName="Border"
Property="IsMouseOver"
Value="True" />
</MultiDataTrigger.Conditions>
<Setter TargetName="Border"
Property="Background"
Value="{StaticResource OnBrushSelected}" />
</MultiDataTrigger>
Working Code
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding Mode=OneWay, RelativeSource={RelativeSource Self}, Path=IsMouseOver}"
Value="True" />
<Condition Binding="{Binding Mode=OneWay, RelativeSource={RelativeSource AncestorType={x:Type ListBoxItem}}, Path=IsSelected}"
Value="True" />
</MultiDataTrigger.Conditions>
<Setter TargetName="Border"
Property="Background"
Value="{StaticResource OnBrushSelected}" />
</MultiDataTrigger>