Is that possible to have a conditional setter, more than 0 value?
How to assign a setter to a conditional value such as more than 开发者_StackOverflow社区0
? I need to hide an object if the value will be not 0
. What would be the best way to do it? Any ideas are highly appreciated.
XAML example of DataTrigger
:
<DataTrigger Binding="{Binding Role.Count}" Value="0">
<Setter TargetName="Counts" Property="Visibility" Value="Hidden" />
</DataTrigger>
Write a value converter, say IsGreaterThanZero
, then write your XAML as,
<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>
And IsGreaterThanZero can be written as,
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
}
精彩评论