C#WPF MVVM绑定未更新 [英] C# WPF MVVM Binding not updating

查看:214
本文介绍了C#WPF MVVM绑定未更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用转换器进行简单绑定,以显示满足给定枚举(例如A B C D)的可观察集合中元素的数量.

I'm trying to do simple binding using a converter to display count of elements inside observable collection that satisfy given enum, let's say A B C D.

下面的代码在我与其他项目一起测试时可以使用,但是在最基本的项目绑定上不会更新.完全相同的代码可以在其他项目中使用(真的很奇怪).

The code below works when I test it with my other project, but on the most basic project binding doesn't get updated. The exact same code works inside other project (really strange).

我如何进行装订

<Label Content="{Binding Source={x:Static viewmodels:TestViewModel.Instance}, Path=TestModels, Converter={StaticResource TestConverter}, Mode=OneWay}"></Label>

转换器

class TestConverter : IValueConverter
{
    public object Convert(
        object value,
        Type targetType,
        object parameter,
        CultureInfo culture)
    {
        if (value == null)
            return 0;
        var v = (ObservableCollection<TestModel>)value;
        return $"{v.Count} / {v.AsParallel().Count(x => x.TestEnum == TestEnum.A)} / {v.AsParallel().Count(x => x.TestEnum == TestEnum.B)} / {v.AsParallel().Count(x => x.TestEnum == TestEnum.C)} / {v.AsParallel().Count(x => x.TestEnum == TestEnum.D)}";
    }

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

型号

public enum TestEnum { A, B, C, D }

public class TestModel:INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    private TestEnum _testEnum;

    public TestModel()
    {
        TestEnum = (TestEnum)(TestViewModel.Instance.rnd.Next(0,3));

    }

    public TestEnum TestEnum
    {
        get
        {
            return _testEnum;

        }
        set
        {
            _testEnum = value;
            PropertyChanged?.Invoke(this,new PropertyChangedEventArgs(null));
        }
    }
}

ViewModel

ViewModel

public class TestViewModel : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    private static readonly TestViewModel _instance = new TestViewModel();

    public Random rnd = new Random();
    public static TestViewModel Instance
    {
        get
        {
            return _instance;

        }
    }

    private ObservableCollection<TestModel> _testModels;

    private TestViewModel()
    {
        _testModels = new ObservableCollection<TestModel>();
    }

    public ObservableCollection<TestModel> TestModels
    {
        get { return _testModels;}
        set
        {
            _testModels = value;
            PropertyChanged?.Invoke(this,new PropertyChangedEventArgs(null));
        }
    } 
}

我要开始工作的简单测试用例

Simple test case that I'm trying to get to work

for (var i = 0; i != 100; i++)
{
    TestViewModel.Instance.TestModels.Add(new TestModel());
}

推荐答案

您将绑定到TestModels集合,因此仅在该属性更改时才调用转换器.您的循环会更改集合中的元素,但不会更改TestModels本身的值.如果您希望此方法有效,那么您基本上有两个选择:

You're binding to the TestModels collection, so the converter is only going to be invoked when that property changes. Your loop changes the elements within the collection but it doesn't change the value of TestModels itself. If you want this to work then you basically have two options:

1)使用附加行为,并在首次创建TestModels绑定时使其订阅INotifyCollectionChanged CollectionChanged属性.然后,它将需要某种将结果提供回Label的方式,这可以通过单独的附加属性来实现.

1) Use an attached behaviour and make it subscribe to the INotifyCollectionChanged CollectionChanged property when the TestModels binding is first made. It will then need some way of providing the result back to the Label, that can be achieved with a separate attached property.

2)在您的视图模型中执行所有这些操作,这实际上是应该执行的操作.每当您发现自己在转换器中执行除最基本的,与应用程序无关的任务以外的任何操作时,通常表明您的视图模型层无法正常工作.

2) Do all this in your view model, which is really where it should be being done anyway. Any time you find yourself doing anything but the most basic, application-independent tasks in your converters it's usually a sign that your view model layer isn't doing its job properly.

这篇关于C#WPF MVVM绑定未更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆