Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Path from Resources as Content of Button

I would like to set Button's Content to Path defined in Resources. So I created UserControl, defined Path, two Brushes and button Style in resources... and everything was ok.

<UserControl ...>
    <UserControl.Resources>
        <Path x:Key="PageSmallIcon2" Stretch="Fill" Fill="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Control}}, Path=Foreground}" Stroke="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Control}}, Path=Foreground}" SnapsToDevicePixels="True" Data="F1 M 19,25L 27,25L 27,33L 19,33L 19,25 Z M 19,36L 27,36L 27,44L 19,44L 19,36 Z M 31,25L 57,25L 57,33L 31,33L 31,25 Z M 19,47L 27,47L 27,55L 19,55L 19,47 Z "/>
        <SolidColorBrush x:Key="MainColorBrush2" Color="Orange"/>
        <SolidColorBrush x:Key="WhiteColorBrush2" Color="WhiteSmoke"/>
        <Style x:Key="TransparentButtonStyle2" TargetType="{x:Type Button}">
            <Setter Property="Foreground" Value="{StaticResource WhiteColorBrush2}"/>
            <Setter Property="Height" Value="25"/>
            <Setter Property="Width" Value="25"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type Button}">
                        <Grid>
                            <Rectangle Fill="Transparent"/>
                            <ContentPresenter x:Name="contentPresenter" Content="{TemplateBinding Content}" Margin="2" />
                        </Grid>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsPressed" Value="True">
                                <Setter Property="Foreground" Value="{StaticResource MainColorBrush2}"/>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </UserControl.Resources>
    <Grid>
        <Button Command="{Binding Path=SmallPageSizeCommand}" Style="{StaticResource TransparentButtonStyle2}" Content="{StaticResource PageSmallIcon2}"/>
        ...
    </Grid>
</UserControl>

I decided to put those resources in separate ResourceDictionaries. I plan to have more Paths and Brushes... Also I would like to have only one style for button. Basically I am doing this, because I had style for each of my buttons and Path for each specific Button was part of that Button's ControlTemplate.

So I created three ResourceDictionaries Colors.xaml, Icons.xaml and Controls.xaml and then I updated App.xaml

<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="Theme/Colors.xaml"/>
            <ResourceDictionary Source="Theme/Icons.xaml"/>
            <ResourceDictionary Source="Theme/Controls.xaml"/>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>

Now Path's Fill property is not updated when I press that Button. Could anybody please explain why it does not work in separate ResourceDictionaries and provide me with solution to this problem?

UPDATE

So I discovered that problem is in Path. When I have that Path in <UserControl.Resources>, colors in Colors.xaml and button style in Controls.xaml everything works again.

I also discovered that when I have my Path in Icons.xaml and I use it in my UserControl as button's content

<Button Command="{Binding Path=SmallPageSizeCommand}" Style="{StaticResource TransparentButtonStyle2}" Content="{StaticResource PageSmallIcon2}"/>

and I have multiple instances of my UserControl... then Path is present only in one instance of a UserControl at a time.

like image 725
Kapitán Mlíko Avatar asked May 21 '13 14:05

Kapitán Mlíko


1 Answers

The reason for:

I have multiple instances of my UserControl... then Path is present only in one instance of a UserControl at a time

is because you are giving the same Element different parents in XAML. You are setting the content of multiple Button controls to a Path Shape which is defined in resources. For optimization purposes each reference to a Resource will return by default the same Instance of the object and since in WPF each element can have only one parent at a time, only the last Button in your XAML declaration will have it's content set to the desired Path. To solve this you can mark the resource with the x:Shared="False" attribute. In this case the XAML Parser will generate a new instance of the Path element for each Reference.

like image 86
Novitchi S Avatar answered Oct 07 '22 15:10

Novitchi S