1

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

它只是一个继承自 TextBox 和另一个 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.

谁能指出我正确的方向?

4

1 回答 1

4

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

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

于 2012-10-23T12:53:20.800 回答