Reputation: 55
So I don't understand how this happens but I defined a button style in a resource dictionary and load that dictionary in the Windows.Resources
.
But it"s not applied to all the buttons I want to only to the last element.
The style:
<Style TargetType="{x:Type Button}" x:Key="FolderOpenBtn">
<Setter Property="Content">
<Setter.Value>
<StackPanel Orientation="Horizontal">
<Viewbox Width="16" Height="16" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" Margin="0,0,2,0">
<Rectangle Width="16" Height="16">
<Rectangle.Fill>
<DrawingBrush>
<DrawingBrush.Drawing>
<DrawingGroup>
<DrawingGroup.Children>
<GeometryDrawing Brush="#00FFFFFF" Geometry="F1M0,0L16,0 16,16 0,16z" />
<GeometryDrawing Brush="#FFF6F6F6" Geometry="F1M0,2.9688L0,11.9688C0,12.5858 0.227,13.0718 0.57,13.4038 1.14,13.9478 2,13.9688 2,13.9688L13.677,13.9688 16,8.1648 16,6.9688 15,6.9688 15,4.9688C15,3.6698,13.97,2.9688,13,2.9688L10.116,2.9688 9.116,0.9688 2,0.9688C1.005,0.9688,0,1.6658,0,2.9688" />
<GeometryDrawing Brush="#FFEFEFF0" Geometry="F1M2,3L8,3 9,5 13,5 13,8 4,8 2,13z" />
<GeometryDrawing Brush="#FF424242" Geometry="F1M1,3L1,12C1,12.97,1.94,12.984,1.997,12.984L2,12.984 2,3 8,3 9,5 13,5 13,8 4,8 2,13 13,13 15,8 14,8 14,5C14,4,12.764,4,13,4L9.5,4 8.5,2 2,2C2,2,1,2,1,3" />
</DrawingGroup.Children>
</DrawingGroup>
</DrawingBrush.Drawing>
</DrawingBrush>
</Rectangle.Fill>
</Rectangle>
</Viewbox>
<TextBlock Text="Select..." />
</StackPanel>
</Setter.Value>
</Setter>
</Style>
Loading the dictionaries:
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/templates/Styles.xaml" />
<ResourceDictionary Source="/templates/DatabaseTabItem.xaml" />
<ResourceDictionary Source="/templates/SatzartTabItem.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Window.Resources>
And applying the style to two buttons:
Button 1:
<Button x:Name="ClassPathSelectBtn" Grid.Column="1" Grid.Row="0" MinWidth="70" Width="Auto" Margin="10,0,0,0" Height="23" VerticalAlignment="Bottom"
Click="BtnSelectPath_Click" Background="#e3e3e3" Style="{StaticResource FolderOpenBtn}" />
Button 2:
<Button x:Name="reportPathSelectBtn" Grid.Row="1" Grid.Column="1" Margin="10,0,0,0" MinWidth="70" Width="Auto" Height="23" VerticalAlignment="Bottom"
Click="BtnSelectPath_Click" Background="#e3e3e3" Style="{StaticResource FolderOpenBtn}"/>
The result looks like this however:
Please ignore the blue hover effect.
Can you please help me with what I'm doing wrong?
Upvotes: 2
Views: 2462
Reputation: 128157
You have a UI element in the Style, applied to the Button's Content.
Since the Style and hence also the UI element is shared, there is only once instance of it. However, a single UI element can only be shown once, i.e. can only have a single parent element. You may get around this by setting the x:Shared
attribute of the Style to false:
<Style TargetType="Button" x:Key="FolderOpenBtn" x:Shared="False">
...
<Style>
Instead of directly creating a UI element by assigning the Button's Content, you may better set the ContentTemplate property:
<Style TargetType="Button" x:Key="FolderOpenBtn">
<Setter Property="Content" Value="Select..."/>
<Setter Property="ContentTemplate">
<Setter.Value>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Viewbox Width="16" Height="16" Margin="0,0,2,0">
...
</Viewbox>
<TextBlock Text="{Binding Content,
RelativeSource={RelativeSource TemplatedParent}}"/>
</StackPanel>
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
That would also allow to easily replace the text, just by setting a Button's Content to some other value than the default "Select..."
.
Upvotes: 6