1

スタイルを設定したいカスタムコントロールがあります。

これは、TextBoxと別のインターフェイスから継承する単なるクラスであり、インターフェイスは追加のプロパティを追加するだけです。

このカスタムコントロールにスタイルを適用して、読み取り専用プロパティが設定されている場合に背景が灰色になるようにするにはどうすればよいですか?


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

依存関係プロパティの既定の動作を再定義できます。具体的には、以下を定義できます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

于 2012-10-23T12:53:20.800 に答える