Search code examples
wpfxamlpathfill

Path Fill property binding erratic behavior?


I got some kind of weird behavior while trying to use all the power of MVC... :) I was to trying to bound a simple Path Fill property to some value and cannot succeed.

As a small exemple is worth...
Here's how to reproduce simply:

<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
<Grid>
    <Path Name="p" Data="M 0,0 h 30 v 30 h -30 v -30" Fill="Beige">
        <Path.Style>
            <Style TargetType="{x:Type Path}">
                <Style.Triggers>
                    <DataTrigger Binding="{Binding IsChecked, ElementName=ch}" Value="True">
                        <Setter Property="Path.Fill" Value="Red"/>
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </Path.Style>
    </Path>

    <CheckBox Name="ch" Margin="35,12,-35,-12" />
</Grid>
</Window>

An other issue, It works with this layout but not with a similar one (removing Fill from declaration):

<Path Name="p" Data="M 0,0 h 30 v 30 h -30 v -30">
    <Path.Style>
        <Style TargetType="{x:Type Path}">
                <Style.Triggers>
                    <DataTrigger Binding="{Binding IsChecked, ElementName=ch}" Value="True">
                        <Setter Property="Path.Fill" Value="Red"/>
                    </DataTrigger>
                    <DataTrigger Binding="{Binding IsChecked, ElementName=ch}" Value="False">
                        <Setter Property="Path.Fill" Value="Blue"/>
                    </DataTrigger>

                </Style.Triggers>
            </Style>
        </Path.Style>
    </Path>

Could someone enlighten me on the cause ?


Solution

  • Do not set any Fill on the path itself if you want to change it in a style as that will override your style due to dependency property value precedence.

    Move the initial Fill value into a Setter of the style.