这可能有一个条件设定器,超过0值吗?

时间:2011-01-25 23:15:03

标签: wpf wpf-controls binding

如何将setter分配给条件值,例如超过0?如果值不是0,我需要隐藏一个对象。最好的方法是什么?任何想法都受到高度赞赏。

DataTrigger的XAML示例:

<DataTrigger Binding="{Binding Role.Count}" Value="0">
    <Setter TargetName="Counts" Property="Visibility" Value="Hidden" />
</DataTrigger>

1 个答案:

答案 0 :(得分:3)

写一个值转换器,比如说IsGreaterThanZero,然后把你的XAML写成,

<Window.Resources>
         <local:IsGreaterThanZero x:Key="isGreaterThanZero"/>
</Window.Resources>

<DataTrigger Binding="{Binding Role.Count, Converter={StaticResource isGreaterThanZero}" Value="True">
    <Setter TargetName="Counts" Property="Visibility" Value="Hidden" />
</DataTrigger>

IsGreaterThanZero可以写成,

public class IsGreaterThanZero: IValueConverter
{ 
        #region IValueConverter Members
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return Int32.Parse(value as string) > 0;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException() ;//"PresenterConverter.ConvertBack() is not implemented!");
        }
        #endregion
}