相关疑难解决方法(0)

如何将CollectionContainer绑定到视图模型中的集合?

我有一个视图模型,其中包含一个公开事物集合的属性.我有一个ComboBox,其ItemsSource属性绑定到此集合.现在用户可以从列表中进行选择.

我想允许用户清除选择,所以我想向ComboBox添加一个项目(即Null).这很简单.

我决定尝试为ItemsSource使用CompositeCollection,以便我可以将现有列表中的项添加到ComboBox以及额外的Null项.

经过一段时间的斗争,我决定回到CompositeCollection类的文档.我复制了他们的示例并将其修改为使用视图模型而不是静态资源.

我发现当我将CollectionContainer绑定到ViewModel公开的列表时,列表中没有任何项目显示.

我不知道如何解决这个问题,我正在寻找关于这个主题的任何建议.

这是我的XAML代码:

<Window Background="CornflowerBlue" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"   xmlns:c="clr-namespace:TryingWPF"  x:Class="CompositeCollections"  Title="CompositeCollections"  SizeToContent="WidthAndHeight">
<Window.Resources>
    <c:CompositeCollectionVM x:Key="CompositeCollectionVM"/>
    <XmlDataProvider x:Key="GreekHeroesData" XPath="GreekHeroes/Hero">
        <x:XData>
            <GreekHeroes xmlns="">
                <Hero Name="Jason" />
                <Hero Name="Hercules" />
                <Hero Name="Bellerophon" />
                <Hero Name="Theseus" />
                <Hero Name="Odysseus" />
                <Hero Name="Perseus" />
            </GreekHeroes>
        </x:XData>
    </XmlDataProvider>
    <DataTemplate DataType="{x:Type c:GreekGod}">
        <TextBlock Text="{Binding Path=Name}" Foreground="Gold"/>
    </DataTemplate>
    <DataTemplate DataType="Hero">
        <TextBlock Text="{Binding XPath=@Name}" Foreground="Cyan"/>
    </DataTemplate>
</Window.Resources>

<StackPanel DataContext="{StaticResource CompositeCollectionVM}">
    <TextBlock FontSize="18" FontWeight="Bold" Margin="10" HorizontalAlignment="Center" Foreground="WhiteSmoke">Trying Composite Collections</TextBlock>
    <DockPanel>
        <ListBox Name="myListBox" Height="300" Background="#99333333">
            <ListBox.ItemsSource> …
Run Code Online (Sandbox Code Playgroud)

vb.net data-binding wpf xaml

51
推荐指数
2
解决办法
3万
查看次数

Merged ObservableCollection

我有两个ObservableCollections,我需要在一个ListView控件中一起显示它们.为此,我创建了MergedCollection,它将这两个集合显示为一个ObservableCollection.这样我就可以将ListView.ItemsSource设置为我的合并集合,并列出两个集合.添加工作正常,但当我尝试删除项目时,显示未处理的异常:

An unhandled exception of type 'System.InvalidOperationException' occurred in PresentationFramework.dll
Additional information: Added item does not appear at given index '2'.
Run Code Online (Sandbox Code Playgroud)

MergedCollection的代码如下:

public class MergedCollection : IEnumerable, INotifyCollectionChanged
{
    ObservableCollection<NetworkNode> nodes;
    ObservableCollection<NodeConnection> connections;

    public MergedCollection(ObservableCollection<NetworkNode> nodes, ObservableCollection<NodeConnection> connections)
    {
        this.nodes = nodes;
        this.connections = connections;

        this.nodes.CollectionChanged += new NotifyCollectionChangedEventHandler(NetworkNodes_CollectionChanged);
        this.connections.CollectionChanged += new NotifyCollectionChangedEventHandler(Connections_CollectionChanged);
    }

    void NetworkNodes_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
    {
        CollectionChanged(this, e);
    }

    void Connections_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
    {
        CollectionChanged(this, e);
    }

    #region IEnumerable Members

    public IEnumerator GetEnumerator()
    {
        for …
Run Code Online (Sandbox Code Playgroud)

c# collections wpf merge observablecollection

12
推荐指数
1
解决办法
1万
查看次数