在Xaml中指定强类型值的简写方法

时间:2015-01-22 04:11:35

标签: c# wpf xaml markup-extensions

这就是我想要的结果:

<Grid Visibility={Binding EnablePurchase, Converter={local:ConditionalConverter TrueValue=(Visibility)Visible FalseValue=(Visibility)Collapsed}}/>

目前我正在做的事情:

<Grid>
    <Grid.Visibility>
        <Binding Path="EnablePurchase">
            <Binding.Converter>
                <local:ConditionalConverter>
                    <local:ConditionalConverter.TrueValue>
                        <Visibility>Visible</Visibility>
                    <local:ConditionalConverter.TrueValue>
                    <local:ConditionalConverter.FalseValue>
                        <Visibility>Collapsed</Visibility>
                    <local:ConditionalConverter.FalseValue>
                </local:ConditionalConverter>
            </Binding.Converter>
        </Binding>
    </Grid.Visibility>
</Grid>

1 个答案:

答案 0 :(得分:1)

您可以简单地创建一个具有以下属性的转换器:

public class ValueConverterWithProperties : IValueConverter
{
    public int TrueValue { get; set; }

    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if ((int) value == TrueValue)
        {
            return true;
        }
        return false;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

然后像这样使用它:

<Window x:Class="Converter.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:converter="clr-namespace:Converter"
    Title="MainWindow" Height="350" Width="525">
<Window.Resources>
    <converter:ValueConverterWithProperties TrueValue="5" x:Key="converterWithProperties"></converter:ValueConverterWithProperties>
</Window.Resources>
<Grid>
    <CheckBox IsChecked="{Binding item, Converter={StaticResource converterWithProperties}}"></CheckBox>
</Grid>

您还可以从转换器中的MarkupExtension派生出更好的用法:

public class ValueConverterWithProperties : MarkupExtension, IValueConverter
{
    public int TrueValue { get; set; }

    public override object ProvideValue(IServiceProvider serviceProvider)
    {
        return this;
    }

    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if ((int) value == TrueValue)
        {
            return true;
        }
        return false;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

然后您可以直接在使用转换器的位置设置属性,以便您轻松地为每个转换器设置不同的值:

<Window x:Class="Converter.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:converter="clr-namespace:Converter"
    Title="MainWindow" Height="350" Width="525">
<Grid>
    <CheckBox IsChecked="{Binding item, Converter={converter:ValueConverterWithProperties TrueValue=5}}"></CheckBox>
    <CheckBox IsChecked="{Binding item2, Converter={converter:ValueConverterWithProperties TrueValue=10}}"></CheckBox>
</Grid>