我正在为我的应用程序创建一个资源字典,其中有一些“图标+文本”按钮。由于它们看起来都一样(图标和文本除外),因此我创建了一个通用样式作为其他样式的基础:
<!-- Generic ActionButtonStyle -->
<Style x:Key="ActionButtonStyle" TargetType="{x:Type Button}">
<!-- some setter properties -->
<Setter Property="ContentTemplate" Value="{DynamicResource ButtonDataTemplate}"/>
</Style>
<DataTemplate x:Key="ButtonDataTemplate">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="24" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Image Source="{Binding Source}"
Stretch="Uniform"
Grid.Column="0"
Margin="2"/>
<TextBlock Text="{Binding text}"
TextWrapping="Wrap"
Grid.Column="1"
Margin="2"
VerticalAlignment="Center"/>
</Grid>
</DataTemplate>
我有一些图标图像:
<!-- Icons -->
<ImageSource x:Key="textToSpeech">Images/GreyIcons/TextToSpeech.png</ImageSource>
<ImageSource x:Key="play">Images/GreyIcons/Play.png</ImageSource>
<ImageSource x:Key="playSound">Images/GreyIcons/PaySound.png</ImageSource>
.
.
.
.
<ImageSource x:Key="group">Images/GreyIcons/Goup1.png</ImageSource>
我想为每个按钮创建单独的样式(对应于每个图标)。像这样的事情:
<!-- Specific ActionButtonStyles -->
<Style x:Key="TextToSpeechButtonStyle" TargetType="{x:Type Button}" BasedOn="{StaticResource ActionButtonStyle}">
<Setter Property="Content">
<Setter.Value>
<Image Source="{StaticResource textToSpeech}"
</Setter.Value>
</Setter>
</Style>
我知道这不起作用..我应该怎么做?我应该为通用按钮创建自定义用户控件吗?文本将绑定(bind)到我的模型中的一个对象,命令(操作)也将绑定(bind)到该对象。
最佳答案
您正在寻找的示例似乎丢失了,但您似乎正在寻找“BasedOn” - 它允许您继承,但仍然覆盖先前定义的样式。您可以像这样实现它:
<Style x:Key="MyButtonStyle" BasedOn="{StaticResource ActionButtonStyle}">
<Setter.../>
</Style>
关于wpf - 使用通用样式作为基础与自定义用户控件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10116310/