Custom DependencyProperty on Style.Trigger

2.6k Views Asked by At

for an UserControl I declared a DependencyProperty :

        public static readonly DependencyProperty DeselectedPresentationModeProperty =
        DependencyProperty.Register(
        "DeselectedPresentationMode",
        typeof(int),
        typeof(MyUserControl),
        new FrameworkPropertyMetadata(1,FrameworkPropertyMetadataOptions.AffectsRender | FrameworkPropertyMetadataOptions.BindsTwoWayByDefault));

    public static readonly DependencyProperty SelectedPresentationModeProperty =
        DependencyProperty.Register(
        "SelectedPresentationMode",
        typeof(int),
        typeof(MyUserControl),
        new FrameworkPropertyMetadata(1, FrameworkPropertyMetadataOptions.AffectsRender | FrameworkPropertyMetadataOptions.BindsTwoWayByDefault));

Now I want to use these properties within a MultiTrigger:

    <Style TargetType="{x:Type ListBoxItem}" BasedOn="{StaticResource {x:Type ListBoxItem}}" x:Key="MyUserControlItemStyle">
        <Style.Triggers>
            <MultiTrigger>
                <MultiTrigger.Conditions>
                    <Condition Property="IsSelected" Value="False" />
                    <Condition Property="gui:MyUserControl.DeselectedPresentationMode" Value="0" />
                </MultiTrigger.Conditions>
                <MultiTrigger.Setters>
                    <Setter Property="ContentTemplate" Value="{StaticResource PresentationModeIconOnly}" />
                </MultiTrigger.Setters>
            </MultiTrigger>
            <MultiTrigger>
                <MultiTrigger.Conditions>
                    <Condition Property="IsSelected" Value="False" />
                    <Condition Property="gui:MyUserControl.DeselectedPresentationMode" Value="1" />
                </MultiTrigger.Conditions>
                <MultiTrigger.Setters>
                    <Setter Property="ContentTemplate" Value="{StaticResource PresentationModeSimple}" />
                </MultiTrigger.Setters>
            </MultiTrigger>
            <MultiTrigger>
                <MultiTrigger.Conditions>
                    <Condition Property="IsSelected" Value="True" />
                    <Condition Property="gui:MyUserControl.SelectedPresentationMode" Value="0" />
                </MultiTrigger.Conditions>
                <MultiTrigger.Setters>
                    <Setter Property="ContentTemplate" Value="{StaticResource PresentationModeExtended}" />
                </MultiTrigger.Setters>
            </MultiTrigger>
            <MultiTrigger>
                <MultiTrigger.Conditions>
                    <Condition Property="IsSelected" Value="True" />
                    <Condition Property="gui:MyUserControl.SelectedPresentationMode" Value="1" />
                </MultiTrigger.Conditions>
                <MultiTrigger.Setters>
                    <Setter Property="ContentTemplate" Value="{StaticResource PresentationModeIconOnly}" />
                </MultiTrigger.Setters>
            </MultiTrigger>
        </Style.Triggers>
    </Style>
    ...
    ...
    <ListBox ... ItemContainerStyle="{StaticResource MyUserControlItemStyle}"/>

But if I use MyUserControl another project, there is no reaction if I change my DependencyProperty. Whether I use xaml e.g:

<Window 
...
x:Class="WpfControlsTester.MainWindow" 
Title="MainWindow">
<Grid>
    <GUI:MyUserControl Name="test"/>
    <Slider Minimum="0" Maximum="1" Value="{Binding SelectedPresentationMode, ElementName=test}" />
    <Slider Minimum="0" Maximum="1" Value="{Binding DeselectedPresentationMode, ElementName=test}" />
</Grid>

or if I use any kind of manipulation the value like this.test.DeselectedPresentationMode = 0; on an event or smth else.

I used WPF Inspector to "debug" my Triggers, and both properties always had their default value (1). Where is my mistake or what should I add to update my triggers?

Edit : Changed false "MyControl to "MyUserControl"

2

There are 2 best solutions below

1
On BEST ANSWER

You can try the MultiDatatrigger instead like this in order to get the property changes:

<Style.Triggers>
                <MultiDataTrigger>
                        <MultiDataTrigger.Conditions>
                        <Condition Binding="{Binding IsSelected, RelativeSource={RelativeSource Self}}" Value="False" />
                        <Condition Binding="{Binding DeselectedPresentationMode, RelativeSource={RelativeSource AncestorType={x:Type MyUserControl}}}" Value="0" />
                        </MultiDataTrigger.Conditions>
                            <MultiDataTrigger.Setters>
                        <Setter Property="ContentTemplate" Value="{StaticResource PresentationModeIconOnly}" />
                        </MultiDataTrigger.Setters>
                </MultiDataTrigger>
            </Style.Triggers>

Thanks

1
On

This could just be a copy and paste error, but in your code example where you declare the Dependency Properties, your control is named MyUserControl, but in the other code examples, it is called MyControl. These names will need to match if you want this to work.

UPDATE >>>

Ok, I thought that might be the case... so, moving on... can you see the property value if you simply bind to it?

<TextBlock Text="{Binding SelectedPresentationMode, ElementName=test}" />

If you can, then we can move on... usually when a binding does not work, there is a line that starts with the word Error output into the Output Window in Visual Studio. What does your error say. if you have one? (You may need to turn this functionality on by going to Tools > Options > Debugging > Output Window > WPF Trace Settings and setting the Data Binding option to Warning or Error.)

If you have no error in the Output Window, this could be trickier to debug. Let me know.