ListView控件绑定到一个ObservableCollection和两个可视化控件 [英] Binding ListView to an ObservableCollection and two visual controls

查看:177
本文介绍了ListView控件绑定到一个ObservableCollection和两个可视化控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想一个ListView绑定,我想从两个可视控件中的数据(比如,preFIX并为这些字符串后缀)。

I want to bind a ListView to an ObservableCollection of strings which I want to modify with data from two visual controls (say, a prefix and a postfix for those strings).

一个简单的例子:

XAML:

<TextBox Name="tbPrefix"/>
<TextBox Name="tbPostfix"/>

<ListView Name="lvTarget"/>

C#:

public ObservableCollection<string> sources = GetFromSomewhere();

public IEnumerable<string> Items()
{
    foreach (var source in sources) 
    {
        yield return tbPrefix.Text + source + tbPostfix.Text;
    }
}

要保持更新我目前只是在CollectionChanged事件重置其ItemsSource的ListView的:

To keep the ListView updated I'm currently simply resetting its ItemsSource on the CollectionChanged event:

void sources_CollectionChanged(...)
{
    lvTarget.ItemsSource = Items();
}

不过,我也想在ListView绑定到的变化的任何的它的三个来源:收集和preFIX / postfix的控制。我想,我想无论是MultiBinding或MultiDataTrigger,但我不能完全环绕的语法我的头,所有的例子我能找到控件绑定到其他的控件的,而我也有一个的ObservableCollection作为源。

But I also want the ListView to be bound to changes in any of its three sources: the collection and the prefix/postfix controls. I think I want either a MultiBinding or a MultiDataTrigger but I can't quite wrap my head around the syntax and all examples I could find bind a control to other controls, while I also have that ObservableCollection as a source.

P.S。很抱歉,如果它的简单和明显,这只是我的第三天与WPF,我有点不知所措!谢谢!

P.S. Sorry if it's simple and obvious, it's only my third day with WPF and I'm a little overwhelmed! Thanks!

推荐答案

该观察的集合时使用集合中的项目被改变,如添加,删除,移动等通知...它不会通知,如果您更改在字符串中的文本。首先在WPF的最佳实践规则,使用视图模型不code背后绑定属性。可以通过使用以下解决这个问题:


1 - 创建一个名为SomethingViewModel新类


2 - 添加到它的所有属性需要结合您的看法:

The observable collection is used to notify when the items in the collection are changed such as add, remove, move, etc... and it will not notify if you change the text in the string. First best practice rule in WPF to use ViewModel to bind properties not code behind. You can solve this problem by with the following:
1- Create a new class called SomethingViewModel
2- Add to it all the properties needs binding to your view:

public class SomethingViewModel : INotifyPropertyChanged
{
    private string _prefix;
    private string _postfix;

    public SomethingViewModel()
    {
        Sources = new ObservableCollection<string>(/*pass initial data of the list*/);
        Sources.CollectionChanged += (sender, args) => OnPropertyChanged("Items");
    }

    public event PropertyChangedEventHandler PropertyChanged;

    protected virtual void OnPropertyChanged(string propertyName = null)
    {
        var handler = PropertyChanged;
        if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
    }

    private ObservableCollection<string> Sources { get; set; }

    public IList<string> Items
    {
        get { return Sources.Select(x => string.Format("{0}{1}{2}", Prefix, x, Postfix)).ToList(); }
    }

    public string Prefix
    {
        get
        {
            return _prefix;
        }
        set
        {
            if (_prefix == value) return;
            _prefix = value;
            OnPropertyChanged("Prefix");
            OnPropertyChanged("Items");
        }
    }

    public string Postfix
    {
        get
        {
            return _postfix;
        }
        set
        {
            if (_postfix == value) return;
            _postfix = value;
            OnPropertyChanged("Postfix");
            OnPropertyChanged("Items"); // we will notify that the items list has changed so the view refresh its items
        }
    }
}

3在View的构造函数把下面的code给init视图的DataContext:

3- In the constructor of the View put the following code to init the datacontext of the view:

public MainWindow()
{
    this.DataContext= new SomethingViewModel();
}

4,最后绑定视图元素的视图模型属性:

4-Finally bind the view elements to the viewmodel properties:

<TextBox Text={Binding Prefix,Mode=TwoWay}/>
<TextBox  Text={Binding Postfix,Mode=TwoWay}/>

<ListView ItemsSource={Binding Items}/>

5。如果要更改源的项目不初始化一个新的对象,只是使用的方法:

5-If you want to change the items in the source don't initialize a new object just use the method:

Sources.Clear();
Sources.Add();

这篇关于ListView控件绑定到一个ObservableCollection和两个可视化控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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