c# - 将 UserControl 转换为自定义控件

标签 c# .net wpf user-controls custom-controls

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

我尝试过的一件事是将其转换为自定义控件,但我仍然停留在一些基础知识上,例如如何在处理属性更改的静态方法中设置工具提示(见下文)

我试图将 Style 移动到 ResourceDictionary 中的通用按钮样式的另一件事,但这是这个问题的主题

如何在我的子类按钮中设置工具提示?

干杯

用户控件 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());
    }

}

尝试创建自定义控件失败:

public class MyButton : Button
{
    public static readonly DependencyProperty SubjectProperty = DependencyProperty.Register(
        "ItemName", typeof(string), typeof(MyButton),
        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 MyButton;
        if (control == null) return;

        ToolTip = ??;
    }
}

更新

根据 Phil 的回答,控件(底部的那个)比我想要的更“隐蔽”:--)

结果

代码

 public class AddNewItemButton : Button
{
    static AddNewItemButton() {
        var type = typeof (AddNewItemButton);
        DefaultStyleKeyProperty.OverrideMetadata(type, new FrameworkPropertyMetadata(type));
    }

    #region Subject

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

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

    #endregion

}

通用.xaml

<Style TargetType="{x:Type local:AddNewItemButton}">

    <Setter Property="ToolTip" Value="{Binding RelativeSource={RelativeSource Self}, Path=Subject, Converter={StaticResource AddNewItemForToolTip}}"/>

    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type local:AddNewItemButton}">
                <Border Background="{TemplateBinding Background}"
                    BorderBrush="{TemplateBinding BorderBrush}"
                    BorderThickness="{TemplateBinding BorderThickness}">
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="Auto"/>
                            <ColumnDefinition/>
                        </Grid.ColumnDefinitions>

                        <Image Stretch="Uniform" VerticalAlignment="Center"
                            Source="{resx:Resx ResxName=Smack.Core.Presentation.Resources.MasterDetail, Key=bullet_add}" />

                        <AccessText 
                            Text="{Binding RelativeSource={RelativeSource Mode=TemplatedParent}, Path=Subject, Converter={StaticResource AddNewItemForLabel}}" />

                    </Grid>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

enter image description here

最佳答案

这是一个带有工具提示的自定义按钮示例(基于您最近提出的问题):

这是代码

public class CustomButton : Button
{
    static CustomButton()
    {
        DefaultStyleKeyProperty.OverrideMetadata(typeof(CustomButton), 
           new FrameworkPropertyMetadata(typeof(CustomButton)));
    }

    public static readonly DependencyProperty SubjectProperty =
        DependencyProperty.Register("Subject", typeof (string),
        typeof (CustomButton), new PropertyMetadata(default(string)));

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

这在 Themes/generic.xaml 中

<System:String x:Key="Test">Add new: </System:String>

<Style TargetType="{x:Type local:CustomButton}">
    <Setter Property="ToolTip" 
            Value="{Binding RelativeSource={RelativeSource Self}, Path=Subject}"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type local:CustomButton}">
                <Border Background="{TemplateBinding Background}"
                        BorderBrush="{TemplateBinding BorderBrush}"
                        BorderThickness="{TemplateBinding BorderThickness}">
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="Auto"/>
                            <ColumnDefinition/>
                        </Grid.ColumnDefinitions>

                        <Label Grid.Column="0" Content="Image here" 
                               VerticalAlignment="Center" Padding="0,0,5,0"/>

                        <AccessText Grid.Column="1" VerticalAlignment="Center">
                            <AccessText.Text>
                                <MultiBinding StringFormat="{}_{0} {1}">
                                    <Binding Source="{StaticResource Test}"/>
                                    <Binding RelativeSource=
                                        "{RelativeSource TemplatedParent}" 
                                        Path="Subject"/>
                                </MultiBinding>
                            </AccessText.Text>
                        </AccessText>
                    </Grid>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

关于c# - 将 UserControl 转换为自定义控件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10351682/

相关文章:

c# - 在 .NET 4 中最大化窗口时,窗口顶部和左侧值未正确更新

.net - 任何人都可以向我解释 System.Xml.XmlDictionaryWriter.WriteNode(XmlReader, bool) 方法的 bool 参数的含义是什么?

.net - .NET 应用程序上的 Dependency Walker : FlsAlloc

命名空间 "clr-namespace"中不存在 WPF <class>

c# - RabbitMQ 和序列化奇怪的错误

C# 声明静态列表

c# - 从文件中打印出字符串的一部分,从开始序列到停止序列

c# - 了解外部变量何时更改的有效方法是什么?

wpf - 如何隐藏 wpf datagrid 行选择器

c# - 如何在 WPF 中调整多边形的大小?