2012-10-23 100 views
1

我有我想要的风格自定义控制:WPF的造型自定义控件

这仅仅是从文本框和其他接口继承的类,接口只增加了一个额外的属性。

如何将样式应用于此自定义控件,以便设置只读属性时,背景变为灰色?


public class DionysusTextBox : TextBox, IDionysusControl 
    { 

    public DionysusTextBox() 
    { 
     SetStyle(); 
    } 

    #region IDionysusControl Members 

    public bool KeepReadOnlyState 
    { 
     get { return (bool)GetValue(KeepReadOnlyStateProperty); } 
     set { SetValue(KeepReadOnlyStateProperty, value); } 
    } 

    // Using a DependencyProperty as the backing store for MyProperty. This enables animation, styling, binding, etc... 
    public static readonly DependencyProperty KeepReadOnlyStateProperty = 
     DependencyProperty.Register("KeepReadOnlyState", typeof(bool), typeof(DionysusTextBox), new UIPropertyMetadata(true)); 

    #endregion 

    #region Style 

    Style styleListBoxItem = new Style(typeof(DionysusTextBox)); 
    Trigger triggerReadonly = new Trigger { Property = DionysusTextBox.IsReadOnlyProperty, Value = true }; 

    private void SetStyle() 
    { 
     triggerReadonly.Setters.Add(new Setter(DionysusTextBox.BackgroundProperty, Brushes.Black)); 
     this.Triggers.Add(triggerReadonly); 
    } 

    #endregion 


    } 

以上是整个类的代码,我使用的样式似乎是适当的方式,但该方式,当我将此控件添加到设计师,我得到以下错误:

Triggers collection members must be of type EventTrigger. 

任何人都可以指向正确的方向吗?

+0

一个'Trigger'只能是应用于'Style'。在你的情况'styleListBoxItem'不是'this'。 – LPL

+0

这么简单,我改变它,不再收到错误,但风格不起作用,有什么想法? –

+0

我没有看到你应用了这种风格。 – LPL

回答

4

你可以重新定义依赖属性的默认行为,特别是,你可以定义PropertyChangedCallback S:

public class DionysusTextBox : TextBox, IDionysusControl 
{ 
    static DionysusTextBox() 
    { 
     //For the IsReadOnly dependency property  
     IsReadOnlyProperty.OverrideMetadata(
      //On the type DionysusTextBox 
      typeof(DionysusTextBox), 
      //Redefine default behavior   
      new FrameworkPropertyMetadata(
       //Default value, can also omit this parameter 
       null, 
       //When IsReadOnly changed, this is executed 
       new PropertyChangedCallback(
        (dpo, dpce) => 
        { 
         //dpo hold the DionysusTextBox instance on which IsReachOnly changed 
         //dpce.NewValue hold the new value of IsReadOnly 

         //Run logic to set the background here, you are on the UI thread. 

         //Example of setting the BorderBrush from ARGB values: 
         var dioBox = dpo as DionysusTextBox; 
         //Should always be true, of course, it's just my OCD ;) 
         if (dioBox != null)     
         { 
          dioBox.BorderBrush = 
           ColorConverter.ConvertFromString("#FFDDDDDD") as Color?; 
         } 
        }))); 

     //For the BorderBrush property 
     BorderBrushProperty.OverrideMetadata(
      //On the type DionysusTextBox 
      typeof(DionysusTextBox), 
      //Redefine default behavior   
      new FrameworkPropertyMetadata(
       //Default value 
       ColorConverter.ConvertFromString("#FFDDDDDD") as Color?)); 
    } 


    public DionysusTextBox() 
    { 
     SetStyle(); 
    } 
} 

请注意:UIPropertyMetadata = FrameworkPropertyMetadata

+0

这工作对我来说,只是一个问题:我设置BackGroundProperty Brushes.Gray,但我得到以下错误:'System.Drawing.SolidBrush'是不属性'背景'的有效值..我应该什么将背景设置为? –

+0

@ChrisjanL改为使用'System.Windows.Media.Brushes'。 –

+0

最后一个问题!如何在你的代码中完成以下xaml: