Skip to main content
 首页 » 编程设计

c#之WPF 样式自定义控件

2024年02月20日11exmyth

我有一个要设置样式的自定义控件:

它只是一个继承自TextBox和另一个接口(interface)的类,接口(interface)只是增加了一个额外的属性。

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


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. 

谁能指出我正确的方向?

请您参考如下方法:

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

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