I want to make buttons with icons.

In the view model:

public string Icon { get { return icon; } set { icon = value; OnPropertyChanged(); } } 

Well, and some value:

 this.Icon = "pack://application:,,,/Icons/folder_96px.png"; 

In the markup:

  <Style TargetType="Button" x:Key="IconButton" BasedOn="{StaticResource BaseButton}"> <Setter Property="Content"> <Setter.Value> <Image Source="{Binding Icon}"/> </Setter.Value> </Setter> </Style> 

But in the end it looks weird:

enter image description here

Googled, the case seems to be solved by some kind of flag x: Shared = "False", but with this markup I do not understand where to stick.

    1 answer 1

    You do almost right.

    The fact is that it is impossible to share UI elements: after all, a UI element can have only one ancestor. Solution with x:Shared is a rough hack. It will be correct to share not UI-elements, but templates.

    Total we get:

     <Style TargetType="Button" x:Key="IconButton" BasedOn="{StaticResource BaseButton}"> <Setter Property="Content" Value="{Binding Icon}"/> <Setter Property="ContentTemplate"> <Setter.Value> <DataTemplate> <Image Source="{Binding}"/> </DataTemplate> </Setter.Value> </Setter> </Style> 
    • That damn, did not even think that you can redefine the template and work already as with any other content. - Monk