转换一个用户控件自定义控件控件、自定义、用户

2023-09-06 17:49:57 作者:炒鸡帅的小哥哥

下面的用户控件效果很好,但我想更容易地更改样式。

The UserControl below works well but I would like to make it easier to change the Style.

一件事,我曾尝试是将其转换为自定义控制,但我停留在想怎么设置工具提示,随着属性的改变处理静态方法中的基础知识(见下文)

One thing I have tried is to convert this to a Custom Control, but I am stuck on basics like how to set the ToolTip inside the static method that deals with a change in a property (see below)

其他的事情,我想样式移动到在ResourceDictionary中通用的按钮样式,但就是这个问题的主题

The other thing I tried to move the Style into a generic button style in the ResourceDictionary, but that is the subject of this question

我怎么能在我的子类的按键设置工具提示?

How can I set the ToolTip in my subclassed Button?

干杯

用户控件的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>

用户控件code背后:

UserControl Code Behind:

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());
    }

}

失败的尝试创建一个自定义控制:

Failed attempt to create a Custom Control:

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 = ??;
    }
}

更新

根据菲尔的回答,控制(下一个)是更无外观比我想: - )

UPDATE

Based on Phil's answer, the control (the bottom one) is more 'lookless' than I'd like :--)

结果

code

 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

}

Generic.xaml

Generic.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>

推荐答案

下面是一个自定义按钮的工具提示(根据你最近一直在问的问题)的一个例子:

Here's an example of a custom button with a tooltip (based on the questions you've been asking recently):

这是在code

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); }
    }
}

这正好在主题/ generic.xaml

This goes in 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>