2012-07-26 3 views
0

열거 형을 사용하여 단추의 배경색을 업데이트하는 종속성 속성을 정의했습니다. 그것이 실행되면 "기본값 유형이 'CurrentWarningLevel'속성의 유형과 일치하지 않습니다. 다음과 같이 종속성 속성은 다음과 같습니다열거 형을 사용하는 종속성 속성

public enum WarningLevels 
{ 
    wlError=0, 
    wlWarning, 
    wlInfo 
}; 

public class WarningLevelButton : Button 
{ 
    static WarningLevelButton() 
    { 
    } 

    public WarningLevels CurrentWarningLevel 
    { 
     get { return (WarningLevels)GetValue(WarningLevelProperty); } 
     set { base.SetValue(WarningLevelProperty, value); } 
    } 

    public static readonly DependencyProperty WarningLevelProperty = 
     DependencyProperty.Register("CurrentWarningLevel", typeof(WarningLevels), typeof(WarningLevelButton), new PropertyMetadata(false)); 
} 

나는 다음과 같은 속성을 사용하는 것을 시도하고있다 :

<Trigger Property="local:WarningLevelButton.CurrentWarningLevel" Value="wlError"> 
    <Setter TargetName="ButtonBody" Property="Background" Value="{StaticResource CtlRedBrush}" /> 
    <Setter TargetName="ButtonBody" Property="BorderBrush" Value="{StaticResource CtlRedBrush}" /> 
</Trigger> 
<Trigger Property="local:WarningLevelButton.CurrentWarningLevel" Value="wlWarning"> 
    <Setter TargetName="ButtonBody" Property="Background" Value="{StaticResource GreyGradientBrush}" /> 
    <Setter TargetName="ButtonBody" Property="BorderBrush" Value="{StaticResource BorderBrush}" /> 
</Trigger> 

답변

1

1) 당신이 WarningLevelsfalse(bool)을 시전 할 수 없기 때문에.

PropertyMetadata의 첫 번째 매개 변수가 기본값임을 기억하십시오.

2) 다른 문제가 발생할 수 있습니다. 귀하의 방아쇠 값

은 유형 변환기가 없으므로 열거 형으로 변환 할 수없는 문자열입니다. 수정하는 가장 쉬운 방법은 귀하의 가치를 확장하는 것입니다 :

<Trigger Property="local:WarningLevelButton.CurrentWarningLevel" > 
    <Trigger.Value> 
     <my:WarningLevels>wlError</my:WarningLevels> 
    </Trigger.Value> 
    <Setter TargetName="ButtonBody" Property="Background" Value="{StaticResource CtlRedBrush}" /> 
    <Setter TargetName="ButtonBody" Property="BorderBrush" Value="{StaticResource CtlRedBrush}" /> 
</Trigger> 
+0

두 번째 질문에 대해 질문드립니다. [형식 변환기] (http://msdn.microsoft.com/en-us/library/cc645047.aspx) [일반적으로 열거 형]에 대한 지원 (http://msdn.microsoft.com/en-us/library) /system.componentmodel.enumconverter.aspx). –