当集合中的属性更改时触发转换器上的"ConvertBack"?



我正在创建一个复选框列表"在飞行中";在MVVM WPF应用程序中使用转换器从标记的枚举。绑定属性是一个int,它表示一个标记的枚举。这是我的转换器,它将int转换为ObservableDictionary<int,布尔>。

public class AccessListConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        int realValue;
        switch (value)
        {
            case int intValue:
                realValue = intValue;
                break;
            case AccessEnum enumValue:
                realValue = (int)enumValue;
                break;
            default:
                return Binding.DoNothing;
        } 
        List<AccessEnum> accessComponents = Enum.GetValues(typeof(AccessEnum)).Cast<AccessEnum>().Where(r => ((AccessEnum)realValue & r) == r).Select(r => r).ToList();
        ObservableDictionary<int, bool> accessDictionary = new ObservableDictionary<int, bool>(Enum.GetValues(typeof(AccessEnum)).Cast<int>().Select(i => i).ToDictionary(i => i, i => accessComponents.Contains((AccessEnum) i)));
        accessDictionary.Remove(0);
        return accessDictionary;
    }
    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (!(value is Dictionary<int, bool> intDict))
        {
            throw new NotImplementedException();
        }
        return intDict.Sum(x => x.Value ? x.Key : 0);
    }
}

关键是用户可以选择要为属性启用的枚举。我认为使用转换器是有意义的,但我很难让转换器触发它的";ConvertBack";方法,当复选框被勾选/取消勾选时。

以下是我在Xaml:中的绑定

<ItemsControl ItemsSource="{Binding Access, UpdateSourceTrigger=PropertyChanged, Converter={StaticResource AccessListConverter}}" Margin="87,0,10,0">
<ItemsControl.ItemsPanel>
    <ItemsPanelTemplate>
        <StackPanel CanVerticallyScroll="False" Margin="0"/>
    </ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
    <DataTemplate>
        <CheckBox VerticalAlignment="Center" Margin="6,0,0,0" Content="{Binding Key, Converter={StaticResource AccessStringConverter}, UpdateSourceTrigger=PropertyChanged}" 
                    IsChecked="{Binding Value, UpdateSourceTrigger=PropertyChanged}" />
    </DataTemplate>
</ItemsControl.ItemTemplate>

现在我们来看看OBservableDictionary这就是我的OBservableDiction实现的样子(删除无关部分,缩小其余部分(:

 [Serializable]
public class ObservableDictionary<TKey, TValue> : 
    ObservableCollection<ObservableKeyValuePair<TKey, TValue>>, IDictionary<TKey, TValue>
{
    public ObservableDictionary() {}
    public ObservableDictionary(Dictionary<TKey, TValue> dictionary) : this()
    {
        foreach (TKey key in dictionary.Keys) { Add(key, dictionary[key]); }
    }
    public ObservableDictionary(ObservableDictionary<TKey, TValue> dictionary) : this()
    {
        foreach (TKey key in dictionary.Keys) { Add(key, dictionary[key]); }
    }
    public void Add(TKey key, TValue value)
    {
        if (ContainsKey(key)) { throw new ArgumentException("The dictionary already contains the key"); }
        Add(new ObservableKeyValuePair<TKey, TValue>() { Key = key, Value = value });
    }
    public ICollection<TKey> Keys => (from i in ThisAsCollection() select i.Key).ToList();
    public ICollection<TValue> Values => (from i in ThisAsCollection() select i.Value).ToList();
    public TValue this[TKey key]
    {
        get { if (!TryGetValue(key, out TValue result)) {throw new ArgumentException("Key not found"); } return result;  }
        set { if (ContainsKey(key)) { GetKvpByTheKey(key).Value = value; } else { Add(key, value); } }
    }
}

然后我实现了一个collectionChanged代码片段表单StackOverflow

void TrulyObservableCollection_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
    {
        if (e.NewItems != null) foreach (object item in e.NewItems){ ((INotifyPropertyChanged) item).PropertyChanged += item_PropertyChanged; }
        if (e.OldItems != null) foreach (object item in e.OldItems) { ((INotifyPropertyChanged) item).PropertyChanged -= item_PropertyChanged;}
    }
    void item_PropertyChanged(object sender, PropertyChangedEventArgs e)
    {
        NotifyCollectionChangedEventArgs a = new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset);
        OnCollectionChanged(a);
    }

并将CollectionChanged += TrulyObservableCollection_CollectionChanged;添加到空构造函数中。

但没有骰子。经过几个小时的搜寻,我仍然一无所获。ConvertBack永远不会被触发。我认为这可能是因为集合本身实际上并没有发生变化,但我如何才能强制更新?非常感谢您的帮助:(

编辑:我可能已经通过以下评论找到了我自己问题的答案:https://stackoverflow.com/a/59637445/3390519我试试看!

我通过更改搜索参数解决了自己的问题!而不是寻找一个解决方案;当集合中的属性发生更改时,是否触发转换器上的"ConvertBack"&";,我开始寻找";wpf-将标志枚举转换为复选框列表";找到了一个不错的解决方案!它不是动态的(枚举值必须手动更新(,但对我来说已经足够好了:(https://stackoverflow.com/a/59637445/3390519

感谢您抽出时间XAMIMAX:(

相关内容

  • 没有找到相关文章

最新更新