WPF style behaves strangely

I have the following Stylein my App.xaml:

<Style x:Key="BrowseBtn" TargetType="Button">
    <Setter Property="Content">
         <Setter.Value>
              <Image Source="pack://application:,,,/Resources/BrowseIcon.png" />
         </Setter.Value>
    </Setter>
</Style>

I use this style in the Windowfollowing way:

<Button x:Name="btnBrowseCampaigns" Grid.Row="0" Style="{StaticResource BrowseBtn}" />
<Button x:Name="btnBrowseDatabase" Grid.Row="1" Style="{StaticResource BrowseBtn}" />

As you can see, there is no obvious difference between the two buttons, but in the designer (and at runtime) only one of these buttons shows an icon. Is this a mistake or what?

+3
source share
1 answer

Try setting the style x:Shared="False"as follows:

<Window.Resources>
    <Style x:Key="BrowseBtn"
           x:Shared="True" 
           TargetType="{x:Type Button}">

        <Setter Property="Content">
            <Setter.Value>
                <Image Source="pack://application:,,,/BrowseIcon.jpg" />
            </Setter.Value>
        </Setter>
    </Style>
</Window.Resources>

By x:Shared="True"default, one style is common to all - in this case, the system swears at a duplicate Content. When x:Shared="False"creating a style for each element whenever it requests it. Quote from MSDN:

false, WPF, new instance .

.

MSDN: x:Shared Attribute

+1

All Articles