2个ObservableCollections 1个组合框WPF

时间:2019-07-15 10:45:22

标签: c# .net wpf combobox observablecollection

我有一个WPF应用程序,可以建立与另一台计算机的连接。在我的应用程序内部,我有一个组合框,用户可以在其中输入计算机的主机名,然后连接到该计算机。现在,一旦建立连接,用户输入的主机名将保存到绑定到组合框的Observable Collection中,因此下次他想连接到同一主机时,可以直接从组合框中选择它。 >

我已经实现了收藏夹列表。这是我可以绑定到同一组合框的一个单独的可观察集合,因此用户可以选择收藏夹或历史记录项。

在组合框的下拉列表中,我想要2个带标题的分组,如下所示:

    [Favorites]
         My Favourite Host | myfavhost.com
         My 2nd Fav | my2ndfav.com
         Secretly My Fav | secretlymyfav.com
    [History]
         hostioncevisited.com
         whyamihere.com
         thanksforhelping.com

现在我真的不知道该怎么做。有没有一种方法可以将多个项目源绑定到组合框,或者在将它们绑定到组合框之前我必须合并两个可观察的集合吗?

这些是我观察到的收藏

public ObservableCollection<string> HistoryItems { get; set; } = new ObservableCollection<string>();

public static ObservableCollection<FavoriteItem> FavoriteItems { get; set; } = new ObservableCollection<FavoriteItem>();

这是我的FavoriteItem类

public class FavoriteItem : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        private string hostName;
        private string description;

        public FavoriteItem(){}
        public FavoriteItem(string _hostName, string _description)
        {
            hostName = _hostName;
            description = _description;
        }

        public string Hostname
        {
            get { return hostName; }
            set
            {
                hostName = value;
                OnPropertyChanged("Hostname");
            }
        }

        public string Description
        {
            get { return description; }
            set
            {
                description = value;
                OnPropertyChanged("Description");
            }
        }

        protected void OnPropertyChanged(string name)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(name));
            }
        }

        public override string ToString()
        {
            return string.Format("{0} | {1}", description, hostName);
        }
    }

这是组合框的XAML

XAML

<ComboBox Name="cbHostName" Style="{StaticResource ComboBoxLarge}" Text="{Binding HostName}" ItemsSource="{Binding HistoryItems}" 
                          MinWidth="300" MaxWidth="300" IsEditable="True" Margin="0,0,15,0" VerticalAlignment="Center" materialDesign:HintAssist.Hint="Computer, IP or HostProfileName"/>

2 个答案:

答案 0 :(得分:3)

您可以使用CompositeCollection将多个集合绑定到同一源。

Here is an example

缺点是,我认为在这种情况下(至少不容易),无法进行分组。


另一种选择将是只有一个列表的对象实现相同的接口,并具有一些属性来区分项目的类型,例如:

public interface IHost : INotifyPropertyChanged
{
    string HostType { get; }
    string Hostname { get; set; }
    string DisplayText { get; set; }
}

public class HistoryItem : IHost
{
    public event PropertyChangedEventHandler PropertyChanged;

    public string HostType => "History";
    public string Hostname { get; set; }
    public string DisplayText => Hostname;
}

public class FavoriteItem : IHost
{
    public event PropertyChangedEventHandler PropertyChanged;

    public string HostType => "Favorites";
    public string Hostname { get; set; }
    public string Description { get; set; }
    public string DisplayText => Description == null ? Hostname : $"{Description} | {Hostname}";
    //other properties....
}

当我发现直接使用烦人的ObservableCollection时,我倾向于为其使用包装器(底部代码)。它处理一些常见问题,例如可能的内存泄漏和在添加多个项目时引发不必要的CollectionChanged事件。它还可以方便地从代码隐藏访问分组,排序,过滤,当前项目以及CurrentChangedCurrentChanging事件。

在ViewModel中:

public ViewableCollection<IHost> MyItems { get; set; }

初始化集合:

this.MyItems  = new ViewableCollection<IHost>();

// decide how your items will be sorted (important: first sort groups, then items in groups)
this.MyItems.View.SortDescriptions.Add(new SortDescription("HostType", ListSortDirection.Ascending)); // sorting of groups
this.MyItems.View.SortDescriptions.Add(new SortDescription("Hostname", ListSortDirection.Ascending)); // sorting of items

PropertyGroupDescription groupDescription = new PropertyGroupDescription("HostType");
this.MyItems.View.GroupDescriptions.Add(groupDescription);

this.MyItems.View.CurrentChanged += MyItems_CurrentChanged;

this.MyItems.AddRange(new IHost[] {
           new HistoryItem { Hostname = "ccc" },
           new HistoryItem { Hostname = "aaa" },
           new HistoryItem { Hostname = "xxx" },
           new FavoriteItem { Hostname = "vvv" },
           new FavoriteItem { Hostname = "bbb" },
           new FavoriteItem { Hostname = "ttt" } });

选中该项目后,将执行以下代码:

private void MyItems_CurrentChanged(object sender, EventArgs e)
    {
        Console.WriteLine("Selected item: " + this.MyItems.CurrentItem?.Hostname);
    }

这是ComboBox的分组分组(使用ViewableCollection,您需要将ItemsSource绑定到MyItems.View而不是直接绑定到MyItems):< / p>

<ComboBox ItemsSource="{Binding MyItems.View, Mode=OneWay}"
              IsSynchronizedWithCurrentItem="True"
              DisplayMemberPath="DisplayText">
        <ComboBox.GroupStyle>
            <GroupStyle>
                <GroupStyle.HeaderTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding Path=Items.CurrentItem.HostType, StringFormat=[{0}]}"/>
                    </DataTemplate>
                </GroupStyle.HeaderTemplate>
            </GroupStyle>
        </ComboBox.GroupStyle>
    </ComboBox>

结果:

combo box code result


[DoNotNotify]
public class ViewableCollection<T> : ObservableCollection<T>
{
    private ListCollectionView _View;

    public ViewableCollection(IEnumerable<T> items)
        : base(items) { }

    public ViewableCollection()
        : base() { }

    [XmlIgnore]
    public ListCollectionView View
    {
        get
        {
            if (_View == null)
            {
                _View = new ListCollectionView(this);
                _View.CurrentChanged += new EventHandler(InnerView_CurrentChanged);
            }
            return _View;
        }
    }

    [XmlIgnore]
    public T CurrentItem
    {
        get
        {
            return (T)this.View.CurrentItem;
        }
        set
        {
            this.View.MoveCurrentTo(value);
        }
    }

    private void InnerView_CurrentChanged(object sender, EventArgs e)
    {
        this.OnPropertyChanged(new PropertyChangedEventArgs("CurrentItem"));
    }

    public void AddRange(IEnumerable<T> range)
    {
        if (range == null)
            throw new ArgumentNullException("range");

        foreach (T item in range)
        {
            this.Items.Add(item);
        }

        this.OnPropertyChanged(new PropertyChangedEventArgs("Count"));
        this.OnPropertyChanged(new PropertyChangedEventArgs("Item[]"));
        this.OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
    }

    public void ReplaceItems(IEnumerable<T> range)
    {
        if (range == null)
            throw new ArgumentNullException("range");

        this.Items.Clear();
        foreach (T item in range)
        {
            this.Items.Add(item);
        }

        this.OnPropertyChanged(new PropertyChangedEventArgs("Count"));
        this.OnPropertyChanged(new PropertyChangedEventArgs("Item[]"));
        this.OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
    }

    public void RemoveItems(IEnumerable<T> range)
    {

        if (range == null)
            throw new ArgumentNullException("range");

        foreach (T item in range)
        {
            this.Items.Remove(item);
        }

        this.OnPropertyChanged(new PropertyChangedEventArgs("Count"));
        this.OnPropertyChanged(new PropertyChangedEventArgs("Item[]"));
        this.OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
    }

    public void ClearAll()
    {
        IList old = this.Items.ToList();
        base.Items.Clear();
        this.OnPropertyChanged(new PropertyChangedEventArgs("Count"));
        this.OnPropertyChanged(new PropertyChangedEventArgs("Item[]"));
        this.OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
    }

    public void CallCollectionChaged()
    {
        this.OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
    }

    // necessary for xml easy serialization using [XmlArray] attribute
    public static implicit operator List<T>(ViewableCollection<T> o)
    {
        return o == null ? default(List<T>) : o.ToList();
    }

    // necessary for xml easy serialization using [XmlArray] attribute
    public static implicit operator ViewableCollection<T>(List<T> o)
    {
        return o == default(List<T>) || o == null ? new ViewableCollection<T>() : new ViewableCollection<T>(o);
    }
}

上面的代码是一个有效的示例。我正在使用nuget软件包PropertyChanged2.Fody注入PropertyChanged通知。

答案 1 :(得分:-2)

否,您不能将多个集合绑定到ItemsSource,必须将它们合并