WPF accesses only single instance of derived class with dependency property

Viewed 236

I have a simple class derived from Button with a dependency property:

public sealed class CircleButton : Button
{
    public Visual Visual
    {
        get { return (Visual)GetValue(VisualProperty); }
        set { SetValue(VisualProperty, value); }
    }

    public static readonly DependencyProperty VisualProperty = DependencyProperty.Register("Visual", typeof(Visual), typeof(CircleButton));
}

I have the following style for the CircleButton:

<Style x:Key="CircleButtonStyle" TargetType="{x:Type controls1:CircleButton}">
    ...
    <Setter Property="Content">
        <Setter.Value>
            <Rectangle Style="{StaticResource CircleButtonRectangleStyle}"/>
        </Setter.Value>
    </Setter>
    ...
</Style>

The rectangle style referenced in the CircleButton style is defined as:

<Style x:Key="CircleButtonRectangleStyle" TargetType="{x:Type Rectangle}">
    <Setter Property="Width" Value="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type controls1:CircleButton}}, Path=Width, Converter={StaticResource ArithmeticConverter}}"/>
    <Setter Property="Height" Value="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type controls1:CircleButton}}, Path=Height, Converter={StaticResource ArithmeticConverter}}"/>
    <Setter Property="Fill" Value="White"/>
    <Setter Property="OpacityMask">
        <Setter.Value>
            <VisualBrush Visual="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type controls1:CircleButton}}, Path=Visual}"/>
        </Setter.Value>
    </Setter>
</Style>

Finally, my XAML defines three CircleButton instances:

        <StackPanel Grid.Row="1" Grid.Column="2">
            <controls:CircleButton HorizontalAlignment="Center" VerticalAlignment="Center" Width="30.0" Height="30.0" 
                    Style="{DynamicResource CircleButtonStyle}" Command="{Binding Add}" ToolTip="Add new product filter." BorderBrush="White" BorderThickness="1" 
                                   Background="{DynamicResource AccentColorBrush}" Visual="{DynamicResource appbar_add}"/>
            <controls:CircleButton HorizontalAlignment="Center" VerticalAlignment="Center" Width="30.0" Height="30.0" 
                    Style="{DynamicResource CircleButtonStyle}" Command="{Binding Remove}" ToolTip="Remove product filter." BorderBrush="White" BorderThickness="1" 
                                   Background="{DynamicResource AccentColorBrush}" Visual="{DynamicResource appbar_minus}"/>
            <controls:CircleButton HorizontalAlignment="Center" VerticalAlignment="Center" Width="30.0" Height="30.0" 
                    Style="{DynamicResource CircleButtonStyle}" Command="{Binding Remove}" ToolTip="Remove product filter." BorderBrush="White" BorderThickness="1" 
                                   Background="{DynamicResource AccentColorBrush}" Visual="{DynamicResource appbar_music}"/>
        </StackPanel>

All three buttons display, but only the last in the sequence has an image (it is the correct one). This occurs no matter how many buttons I add.

Anyone see what I'm missing and what I need to do to correct it?

1 Answers
Related