1
votes

how do I go about binding the Foreground Colour of one Style setter to the Fill Colour on another Style Setter, it should be the same object type. This is the code I have got below!

Getting these errors:

System.Windows.Data Error: 4 : Cannot find source for binding with reference 'ElementName=MenuItemPath'. BindingExpression:Path=Fill; DataItem=null; target element is 'TextBlock' (Name=''); target property is 'Foreground' (type 'Brush')

<Style x:Key="MenuItemName" TargetType="TextBlock">
        <Setter Property="Foreground" Value="{Binding ElementName=MenuItemPath, Path=Fill}" />
        <Setter Property="FontWeight" Value="Bold" />
        <Setter Property="VerticalAlignment" Value="Center" />
        <Setter Property="HorizontalAlignment" Value="Left" />
    </Style>

    <ControlTemplate x:Key="MenuItem" TargetType="RadioButton">
        <ControlTemplate.Resources>
            <Style TargetType="Path" x:Name="MenuItemPath">
                <Setter Property="Fill" Value="#FF22252C" />
                <Setter Property="Height" Value="25" />
                <Setter Property="Width" Value="25" />
                <Setter Property="Stretch" Value="Uniform" />
                <Style.Triggers>
                    <DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type RadioButton}}, Path=IsChecked}" Value="True">
                        <Setter Property="Fill" Value="White" />
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </ControlTemplate.Resources>
3

3 Answers

1
votes

Personally I would create a separate Resource for storing the brush color, and reference it from both locations.

<SolidColorBrush Color="#FF22252C" x:Key="MenuFillColor" /> 

<Style x:Key="MenuItemName" TargetType="TextBlock">
    <Setter Property="Foreground" Value="{StaticResource MenuFillColor}" />
    ...
</Style>

<ControlTemplate x:Key="MenuItem" TargetType="RadioButton">
    <ControlTemplate.Resources>
        <Style TargetType="Path" x:Name="MenuItemPath">
            <Setter Property="Fill" Value="{StaticResource MenuFillColor}" />
            ...
        </Style>
    </ControlTemplate.Resources>
</ControlTemplate>

That said, I've never actually tried binding to another Style's Setter.Value property... it might be possible assuming everything was the same scope. You look like you may have different scopes since you are using <ControlTemplate.Resources> to limit the scope of your MenuItemPath style to just that ControlTemplate. But personally I would not attempt it, and would assume any requirement that needed something like this could also be done some better way :)

1
votes

It is not possible to access outer property of another style in wpf and that's why you have got binding error. Rather you can declare a color in your resources and from where you can access it.

0
votes

I was making it more complicated than need be, I just added a DataTrigger which fixed the issue:

<Style x:Key="MenuItemName" TargetType="TextBlock">
        <Setter Property="Foreground" Value="Black" />
        <Setter Property="FontWeight" Value="Bold" />
        <Setter Property="VerticalAlignment" Value="Center" />
        <Setter Property="HorizontalAlignment" Value="Left" />
        <Style.Triggers>
            <DataTrigger Binding="{Binding IsChecked, ElementName=MenuItemRadio}" Value="True">
                <Setter Property="Foreground" Value="White" />
            </DataTrigger>
        </Style.Triggers>
    </Style>