2
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                xmlns:local="clr-namespace:CuratioCMS.Client.UI.Controls">
<Style BasedOn="{StaticResource {x:Type Button}}" TargetType="{x:Type local:ImageButton}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type local:ImageButton}">
                <StackPanel Height="Auto" Orientation="Horizontal">
                    <Image Width="20"
                           Height="20"
                           Margin="10,4,0,4"
                           Source="{Binding Path=Image,
                                            RelativeSource={RelativeSource TemplatedParent}}"
                           Stretch="Fill" />
                    <TextBlock Margin="5,0,10,0"
                               HorizontalAlignment="Left"
                               VerticalAlignment="Center"
                               FontSize="12"
                               FontWeight="Bold"
                               Foreground="{TemplateBinding Foreground}"
                               Text="{TemplateBinding Label}" />
                </StackPanel>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

这是 ImageButton 自定义控件的 Generic.xaml 中的代码。whit 按预期工作,但我不能从 Base button stale 继承,所以不是从 Button 继承所有基本样式,这种样式只创建没有任何基本样式的自定义控件,并且尽可能

BasedOn="{StaticResource {x:Type Button}}"

在 VS 内部,一行显示错误,显示找不到 Resource '{x:Type System.Windows.Controls.Button}'

我不知道如何实现所需的样式以及为什么此错误消息会出现在 Visual Studio 编辑器中

4

1 回答 1

0

您正在基于通用按钮样式(位于 Generic.xaml 中)定义样式,然后重新定义样式的模板。这样基本样式将被覆盖。

如果你想稍微改变一下风格,你有两个选择:

选项1:

  1. 为通用按钮样式提供一个键(找到在 Generic.xaml 中定义的样式TargetType="{x:Type Button}"并添加x:Key="someName"到它的属性中)
  2. 将所有其他按钮(ImageButtons 除外)的样式设置为{StaticResource someName}
  3. 将 ImageButton 样式的 BasedOn 属性设置为{StaticResource someName}

选项2:

  1. 您可以复制通用按钮样式
  2. 通过将其 TargetType 设置为 ImageButton 并“不”为其设置任何键来使其通用
  3. 更改所需的部分。

选项 2 通常是一个更好的解决方案,因为它不需要进行大量更改,但是我已经完成了您前一段时间想要做的事情,但我没有找到任何方法来避免重复的样式代码。

于 2012-10-15T14:52:35.243 回答