是否可以将相同样式设置为多个控件? 我尝试了以下方法。但1st Button风格没有正确应用,在第二种风格中应用得很好。WPF - 将相同样式设置为多个控件
设计:
<StackPanel Orientation="Horizontal">
<TextBlock Foreground="White" Margin="0,0,5,0">1st Button</TextBlock>
<Button Style="{StaticResource ViewButton}" />
<TextBlock Foreground="White" Margin="25,0,5,0">2nd Button</TextBlock>
<Button Style="{StaticResource ViewButton}" />
</StackPanel>
资源:
<Style x:Key="ViewButton" TargetType="Button" BasedOn="{StaticResource ButtonStyle}">
<Setter Property="Content">
<Setter.Value>
<StackPanel Orientation="Horizontal">
<Image Source="/Images/View.png" Stretch="None" Width="24" Height="24" />
<TextBlock Margin="5,0,0,0" HorizontalAlignment="Center" VerticalAlignment="Center" FontWeight="Bold">View</TextBlock>
</StackPanel>
</Setter.Value>
</Setter>
<Setter Property="Padding" Value="2,0,10,0"/>
</Style>
你不应该使用的模板,而不是内容的风格? –
@JanneMatikainen是的,我之前使用过Template,但我必须使用ContentTemplate才能使其工作。最后nkoniishvt的解决方案帮助了我。 –