ItemTemplate中的转换器参数

时间:2015-09-26 16:59:43

标签: c# xaml enums

我在Listbox中有一组Checkbox,绑定到使用转换器的Enum。代码如下所示:

<ListBox HorizontalAlignment="Left" Height="183" Margin="159,30,0,0" VerticalAlignment="Top" Width="144">
  <ListBox.Resources>
    <local:FlagsEnumValueConverter x:Key="FlagsConverter" />
  </ListBox.Resources>
  <CheckBox Content="Checkbox1" IsChecked="{
    Binding Path=TestModel.TestEnum, 
    Converter={StaticResource FlagsConverter}, 
    ConverterParameter={x:Static tc:TestEnum.Test1}}"/>
  <CheckBox Content="Checkbox2" IsChecked="{
    Binding Path=TestModel.TestEnum, 
    Converter={StaticResource FlagsConverter}, 
    ConverterParameter={x:Static tc:TestEnum.Test2}}"/>
  <CheckBox Content="Checkbox3" IsChecked="{
    Binding Path=TestModel.TestEnum, 
    Converter={StaticResource FlagsConverter}, 
    ConverterParameter={x:Static tc:TestEnum.Test3}}"/>
</ListBox>

转换器代码:

class FlagsEnumValueConverter : IValueConverter
{
    private int mTargetValue;

    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        var mask = (int)parameter;
        mTargetValue = (int)value;
        return ((mask & mTargetValue) != 0);
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        mTargetValue ^= (int)parameter;
        return Enum.Parse(targetType, mTargetValue.ToString());
    }
}

我宁愿使用ItemTemplate来保持简洁,但是我不知道如何使用'ConverterParameter',因为它需要特定的Enum值。我一直在谷歌搜索解决方案,但没有找到一个。老实说,我甚至不知道从哪里开始。这甚至可能吗?

0 个答案:

没有答案