2012-10-23 5 views
1

스타일 지정하려는 사용자 정의 컨트롤이 있습니다.WPF 스타일 지정 사용자 정의 컨트롤

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. 

누구나 올바른 방향으로 나를 가리킬 수 있습니까?

+0

앞에'Trigger' 만 할 수있다 스타일에 적용됩니다. 귀하의 경우'styleListBoxItem' 아니라'this'. – LPL

+0

간단하게, 나는 그것을 바꿨다. 그리고 더 이상 오류를받지 않는다. 그러나 스타일은 효과가 없다, 어떤 생각? –

+0

스타일을 적용하지 않습니다. – LPL

답변

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

+0

이것은 나를 위해 일한 하나의 질문 : 나는 BackGroundProperty를 Brushes.Gray로 설정하고 있지만 'System.Drawing.SolidBrush'속성 'Background'의 값이 올바르지 않습니다. 어떻게해야합니까? 배경을? –

+0

@ChrisjanL 대신'System.Windows.Media.Brushes'를 사용하십시오. –

+0

마지막 질문! 어떻게하면 다음과 같은 xaml이 코드에서와 같이 될 것입니까?

관련 문제