2012-10-23 99 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: