2011-01-28 10 views
1

폼의 필드 요구 사항이 변경 될 때 프로그램 방식으로 스타일을 설정하는 대신 Label의 마지막 문자를 검사하는 트리거를 사용할 수 있습니다. 내용은 '*'이며, 레이블에 대한 속성을 설정합니다 ?WPF 레이블 스타일 레이블 내용에서 알 수없는 색인의 char을 기반으로 트리거 할 수 있습니까?

이와 비슷한 방법이지만 Content 속성의 마지막 문자를 확인하는 방법은 무엇입니까?

답변

3

당신은 변환기를 사용해야한다고 생각합니다. 이

<Style x:Key="LabelStandard" TargetType="Label"> 
    <Setter Property="HorizontalAlignment"  Value="Left"/> 
    <Setter Property="VerticalAlignment"  Value="Top"/> 
    <Style.Triggers> 
     <DataTrigger Binding="{Binding RelativeSource={RelativeSource self}, 
             Path=Content, 
             Converter={StaticResource LastCharConverter}, 
             ConverterParameter=*}" 
        Value="True"> 
      <Setter Property="Foreground" Value="Red"/> 
     </DataTrigger> 
    </Style.Triggers> 
</Style> 
public class LastCharConverter : IValueConverter 
{ 
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
    { 
     if (value == null) 
     { 
      return false; 
     } 
     string content = value.ToString(); 
     if (content.Length > 0 && 
      content[content.Length - 1] == (char)parameter) 
     { 
      return true; 
     } 
     return false; 
    } 
    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
    { 
     throw new NotImplementedException(); 
    } 
} 

업데이트처럼 뭔가를 시도

당신만큼 당신이 위치를 알고있는 Content 문자열에서 특정 문자에 바인딩 할 수 있습니다.

<Style x:Key="LabelStandard" TargetType="Label"> 
    <Setter Property="HorizontalAlignment"  Value="Left"/> 
    <Setter Property="VerticalAlignment"  Value="Top"/> 
    <Style.Triggers> 
     <DataTrigger Binding="{Binding RelativeSource={RelativeSource self}, 
             Path=(Content)[2]}" 
         Value="*"> 
      <Setter Property="Foreground" Value="Red"/> 
     </DataTrigger> 
    </Style.Triggers> 
</Style> 

하지만 당신의 문자열 길이는 내부 [2]을 결합 할 수 없기 때문에 당신에게 매우 좋은하지 않을 것이다 (나는 경우가 있다고 가정한다) 다를 경우 나는 알고 있어요 어떤 식 으로든 (바인딩 의). 이 외에

, 당신이 자신을

+0

흠 .. 너무 많은 일이 .. 차라리보다 프로그래밍 방식으로 그것을 할 것이라고 지적 당신이 솔루션 뒤에 코드를해야 할 것 같아요 :) lbl.Style = (Style) this.Resources [ "LabelRequired"]; – markmnl

+0

그래도 고마워! – markmnl

+0

업데이트 주셔서 감사합니다 - 너무 가까이에 노력하고 싶어하지만 .. 아무 방법으로 보이지 않습니다 .. – markmnl