簡體   English   中英

XAML定義中的ObservableCollection =項混合在一起

[英]ObservableCollection in XAML definition = Items mixed up

我的C#代碼:

namespace App1 {

public sealed partial class MyUserControl1 : UserControl {

    public ObservableCollection<Foo> Foos
    {
        get { return (ObservableCollection<Foo>)GetValue(FoosProperty); }
        set { SetValue(FoosProperty, value); }
    }

    // Using a DependencyProperty as the backing store for MyProperty.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty FoosProperty =
        DependencyProperty.Register("Foos", typeof(ObservableCollection<Foo>), typeof(MyUserControl1), new PropertyMetadata(new ObservableCollection<Foo>()));

    public MyUserControl1() {
        this.InitializeComponent();
    }
}

public class Foo : DependencyObject, INotifyPropertyChanged {

    public ObservableCollection<Bar> Bars
    {
        get { return (ObservableCollection<Bar>)GetValue(BarsProperty); }
        set { SetValue(BarsProperty, value); }
    }

    // Using a DependencyProperty as the backing store for Bars.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty BarsProperty =
        DependencyProperty.Register("Bars", typeof(ObservableCollection<Bar>), typeof(Foo), new PropertyMetadata(new ObservableCollection<Bar>()));

    public Foo() {
    }

    public event PropertyChangedEventHandler PropertyChanged;
}

public class Bar {

    public Bar() {
    }
}
}

我的XAML代碼:

<Page
    x:Class="App1.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:App1"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">

    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
        <local:MyUserControl1 x:Name="Ctrl1">
            <local:MyUserControl1.Foos>
                <local:Foo>
                    <local:Foo.Bars>
                        <local:Bar/>
                    </local:Foo.Bars>
                </local:Foo>
                <local:Foo>
                    <local:Foo.Bars>
                        <local:Bar/>
                    </local:Foo.Bars>
                </local:Foo>
            </local:MyUserControl1.Foos>
        </local:MyUserControl1>
    </Grid>
</Page>

在調試器中,我得到以下結果:

兩個Bar對象都在Foo對象中Bars Collection

但是我希望在每個Foo Bars系列中都顯示一個Bar。

在調試器中,我實際上認識到,如果我從一個集合中刪除一個Bar,則將從兩個集合中都刪除它-因此,兩個集合都是對同一對象的引用。

我很困惑

這是我在Stackoverflow中的第一個問題。 也許已經有人問過了-我找不到答案-一直在尋找安靜的時間...雖然我對uwp和xaml不熟悉,但我可以想象這不是最佳實踐。

樣例項目

在綁定中,您正在使用Observable集合。 可觀察的集合具有在進行更改時進行更新的屬性。 在您的XAML上半年

 <local:Foo>
  <local:Foo.Bars>
  <local:Bar/>
  </local:Foo.Bars>
 </local:Foo>

當前Foo數量= 1酒吧數量= 1

您正在添加For Myusercontrols.foo和Foo.bar值。 現在,在下半部分中,您將向相同的“可觀察”列表Foo和Bar添加更多的值,而在同一列表中添加更多的值。 所以當你再重復一次
當前Foo數量= 2條數= 2
您不需要參考當前列表,而是創建一個新的Foo並將其添加到MyUserControl1

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM