简体   繁体   English

IValueConverter的绑定值重置为默认值

[英]Bound value for IValueConverter resets to default

I have bound a flagged enum to a bunch of checkboxes by using a value converter that works as described in the solution to this question . 我已经通过使用值转换器将标记的枚举绑定到一堆复选框,该转换器按照此问题的解决方案中的描述进行操作。 This works fine when I'm working with a flag that hasn't been set to anything. 当我使用尚未设置为任何标志的标志时,这可以正常工作。

However, in the non-trivial use case that the flag is actually set to some data, on load the checkboxes are bound correctly to the enum field. 但是,在非平凡的用例中,标志实际上设置为某些数据,在加载时,复选框正确绑定到枚举字段。 However, when I check/uncheck the box, it seems this binding has been lost. 但是,当我选中/取消选中该框时,似乎此绑定已丢失。

For example, my enum and value converters are as follows: 例如,我的枚举和值转换器如下:

[Flags]
enum Fruits : int { Apple = 1, Orange = 2, Peach = 4, None = 0 };

public Converter : IValueConverter
{
    private Fruits target;

    public Converter() { }

    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        Fruits mask = (Fruits)parameter;
        this.target = (Fruits)value;
        return ((mask & this.target) != 0);
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        this.target ^= (Fruits)parameter;
        return this.target;
    }
}

My XAML (in a datagrid) is as follows: 我的XAML(在数据网格中)如下:

<toolkit:DataGrid.Columns>
    <toolkit:DataGridTemplateColumn Header="Fruits">
        <toolkit:DataGridTemplateColumn.CellTemplate>
            <DataTemplate>
                <DataTemplate.Resources>
                    <ui:Converter x:Key="Converter" />
                </DataTemplate.Resources>
                <StackPanel>
                    <CheckBox Content="Apple" IsChecked="{Binding Path=Fruits, Converter={StaticResource Converter}, ConverterParameter={x:Static constants:Fruits.Apple}}" />
                    <CheckBox Content="Orange" IsChecked="{Binding Path=Fruits, Converter={StaticResource Converter}, ConverterParameter={x:Static constants:Fruits.Orange}}" />
                    <CheckBox Content="Peach" IsChecked="{Binding Path=Fruits, Converter={StaticResource Converter}, ConverterParameter={x:Static constants:Fruits.Peach}}" />
                </StackPanel>
            </DataTemplate>
        </toolkit:DataGridTemplateColumn.CellTemplate>
    </toolkit:DataGridTemplateColumn>
</toolkit:DataGrid.Columns>

When I load the binding from the object, it goes through Convert for each item in the list (an ObservableCollection) and binds the checkboxes correctly, and stores them in the target field. 当我从对象加载绑定时,它会通过转换为列表中的每个项目(ObservableCollection)并正确绑定复选框,并将它们存储在目标字段中。 However, when I check/uncheck another box, the target field is defaulted to "None" and the behavior is as if you're starting over from scratch. 但是,当我选中/取消选中另一个框时,目标字段默认为“无”,行为就像是从头开始。

Any help? 有帮助吗?

Where is the "Converter" resource defined? “转换器”资源在哪里定义? At what scope? 在什么范围? It would have to be defined at the scope of your items template if you've got a list of items because it needs to maintain state for each item individually. 如果您有项目列表,则必须在项目模板的范围内定义,因为它需要单独维护每个项目的状态。

I might be off-base here, but could it be because your enum defines "Three" as 3? 我可能在这里偏离基地,但可能是因为你的枚举将“三”定义为3?

Since you're using bitwise operators to check and uncheck the checkboxes, the "Three" value (0x11) will correspond to both the "One" and "Two" values (0x01 and 0x10 respectively). 由于您使用的位运算符来检查并取消复选框,在“三公”值(0×11),将对应于“一”和“二” 这两个值(分别为0×01和0×10)。

I know it seems counter-intuitive, but try changing your enum declaration to: 我知道这似乎违反直觉,但尝试将您的枚举声明更改为:

enum Numbers : int { One = 1, Two = 2, Three = 4, None = 0 };

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM