2012-04-27 80 views
2

下面的用户控件运行良好,但我希望更容易更改样式。将用户控件转换为自定义控件

一两件事,我曾尝试是将其转换为一个自定义的控制,但我坚持像如何设置与属性的变化涉及的静态方法里面的工具提示基本知识(见下文)

另一件事我试图将样式转换为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 = ??; 
    } 
} 

UPDATE

基于菲尔的回答,控制(下一个)是更多的 '无外观的' 比我想: - )

结果

代码

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

<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

回答

3

这里有一个自定义按钮与工具提示(根据您一直在问最近的问题)的例子:

这是代码

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

<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> 
+0

感谢您通过不同的技术工作的帮助。我无法让自定义控件做得非常好,但是发布了我的代码(请参阅更新的帖子),希望能够明显地跳出某些东西。干杯 – Berryl 2012-04-27 21:44:01

+0

用于其他目的,但绑定{RelativeSource TemplatedParent}是我的热门。谢谢 – DRapp 2013-04-01 12:00:59