【问题标题】:Styling UserControls样式化用户控件
【发布时间】:2012-04-27 21:43:43
【问题描述】:

下面的 UserControl 运行良好,但我想让更改样式更容易。

我尝试过的一件事是将样式移动到 ResourceDictionary 中的通用按钮样式中,但是会出现如下所示的错误。

我尝试的另一件事是将其转换为自定义控件,但这是 this question 的主题

目前有几个类似的按钮被定义为用户控件。有没有推荐的方法来改变所有的样式?

干杯

尝试使用其他样式应用样式失败

<Style TargetType="{x:Type Button}">
    <Setter Property="Style" Value="{StaticResource blueButtonStyle}"/>
</Style>

Error: Style object is not allowed to affect the style property of the object to which it applies

用户控件 XAML

<UserControl.Resources>
    <ResourceDictionary Source="pack://application:,,,/Smack.Core.Presentation.Wpf;component/Themes/generic.xaml" />
</UserControl.Resources>

<Button x:Name="_button" Style="{StaticResource blueButtonStyle}" Command="{Binding AddNewItemCommand}"  >
    <StackPanel Orientation="Horizontal" >
        <Image Source="{resx:Resx ResxName=Smack.Core.Presentation.Resources.MasterDetail, Key=bullet_add}" Stretch="Uniform" VerticalAlignment="Center" />
        <AccessText x:Name="_accesText" VerticalAlignment="Center">_Add New Subject</AccessText>
        <ContentPresenter/>
    </StackPanel>
</Button>

后面的用户控制代码

public partial class AddNewItemButton : UserControl
{
    public AddNewItemButton() { InitializeComponent(); }

    public static readonly DependencyProperty SubjectProperty = DependencyProperty.Register(
        "Subject", typeof (string), typeof (AddNewItemButton),
        new FrameworkPropertyMetadata(OnSubjectChanged));

    public string Subject { get { return (string) GetValue(SubjectProperty); } set { SetValue(SubjectProperty, value); } }

    private static void OnSubjectChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args) {
        var control = obj as AddNewItemButton;
        if (control == null) return;

        control._accesText.Text = "_" + string.Format(MasterDetail.Subject_AddNew_Label, control.Subject.Capitalize());

        control._button.ToolTip = string.Format(MasterDetail.Subject_AddNew_ToolTip, control.Subject.ToLower());
    }

}

【问题讨论】:

    标签: wpf user-controls styles custom-controls


    【解决方案1】:

    使用第一种方法可能会更好:

    <Style TargetType="{x:Type Button}">
        <Setter Property="Style" Value="{StaticResource blueButtonStyle}"/>
    </Style>
    

    在样式中设置样式会出错,这是可以理解的。相反,您可以使用“BasedOn”属性强制您的“默认”样式从您想要的样式继承;

    <Style TargetType="{x:Type Button}" BasedOn="{StaticResource blueButtonStyle}">
        <!-- Nothing Here --> 
    </Style>
    

    那应该做你想做的;也就是说,如果我正确理解了您的问题(老实说,这似乎有点不清楚)。

    附带说明,没有任何理由为此按钮使用自定义用户控件。这一切都可以通过常规按钮完成,使用 WPF 的核心工具(如 Bindings 和 Converters)在按钮内容和工具提示中显示动态变化的文本。

    【讨论】:

    • 是的,我应该抓住它,而且它有效。只要允许我(约 2 分钟!),就会立即奖励答案。干杯
    • @BTownKid。我想在几个演示文稿中使用该按钮,并且这种方式感觉更加封装。您是否建议将绑定放入样式中?
    • 对我来说这似乎是最好的。在我看来,只有全新的功能才需要创建新的用户控件;这似乎只是在做绑定的工作,正如你所说,它可以在样式中重用。
    • @BTownKD。我最初是这样做的,但我不知道将 SubjectProperty(现在是 UserControl 中的 DP)放在哪里。它来自 resx,而不是视图模型绑定属性。你会把它放在哪里?
    • 您可以 - 例如 - 在样式中设置文本,并使用绑定到“主题”视图模型属性的 DataTriggers;当特定主题触发器被击中时,新文本被设置。甚至更简单;将按钮文本直接绑定到基于当前主题的 ViewModel 字符串属性。
    猜你喜欢
    • 1970-01-01
    • 2023-01-04
    • 1970-01-01
    • 2016-03-21
    • 1970-01-01
    • 1970-01-01
    • 2011-07-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多