11

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.

4
  • Everything looks fine to me. Are you getting any errors in Output Window with debugger attached? As long as you include your Colors.xaml before Controls.xaml, you should get the desired results.
    – Novitchi S
    Commented May 21, 2013 at 18:56
  • @NovitchiS No. There is no Error in Output Window. Commented May 22, 2013 at 9:31
  • 2
    The second problem from Update is because you are giving the same Element different parents in XAML. For multiple Buttons you are setting the same Path, defined as a resource, as Button's Content. 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.
    – Novitchi S
    Commented May 22, 2013 at 9:46
  • @NovitchiS Wow... didn't think af that. Luckily it solves first problem too. Please add your answer. I'll accept it. Commented May 22, 2013 at 10:39

1 Answer 1

14

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.

1
  • 3
    It seems like this default is backwards. Why would you resource something if you're not going to actually share it? Commented Jun 29, 2015 at 13:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.