【问题标题】:Windows Phone 7: Binding a Collection to a ListBox such that a change in an Item of the Collection updates in the ListBoxWindows Phone 7:将集合绑定到 ListBox,以便集合项中的更改在 ListBox 中更新
【发布时间】:2012-02-11 06:31:10
【问题描述】:

对不起,如果这是基本的,但我没有找到一个很好的例子来准确描述我需要做什么来启用以下场景:

我有两个班级:

公共类事物:依赖对象 {

    // Fields
    private string name = "";


    // Properties
    public string Name
    {
        get { return name; }
        set { name = value; }
    }


    // Dependency Properties

    public int Count
    {
        get { return (int)GetValue(CountProperty); }
        set { SetValue(CountProperty, value); }
    }

    public static readonly DependencyProperty CountProperty =
        DependencyProperty.Register("Count", typeof(int), typeof(Thing), null);


    // Methods
    public override string ToString()
    {
        return DateTime.Now.ToString() + " " + this.Name + " " + this.Count.ToString();

    }

    // Constructors

    public Thing(string name)
    {
        this.Name = name;
    }

}

以及一个包含 Thing 对象的类

公共类事物:DependencyObjectCollection {

}

MainPage.xaml.cs 文件创建几个 Thing 对象并将它们添加到 Things 集合中。

公共部分类 MainPage : PhoneApplicationPage { 事物事物 = 新事物();

    // Constructor
    public MainPage()
    {
        InitializeComponent();
        Thing thingA = new Thing("A");
        Thing thingB = new Thing("B");
        Things.Add(thingA);
        Things.Add(thingB);


    }

    private void Action_Click(object sender, RoutedEventArgs e)
    {
        this.Things[0].Count += 1;
        Debug.WriteLine(this.Things[0].ToString());
    }
}

我的问题是如何编写绑定代码,以便列表框显示事物对象中包含的事物,并且当事物对象的 DependencyProperty 计数发生更改时,列表框中的事物对象作为字符串的显示会自动更新。

具体来说,我该怎么做以下 XAML 才能使上述情况发生。
也就是说,当我向 Things 对象添加一个新的 Thing 对象时,会向 ListBox 添加一个新项目。当我使用 Things 对象更改现有项目时,更改会出现在 ListBox 中。

在 Windows Phone 7 中,MainPage 包含一个 ListBox:

    <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
        <StackPanel>
            <ListBox x:Name="ThingsBox" />
            <Button x:Name="Action" Content="Action" Click="Action_Click"/>
        </StackPanel>
    </Grid>
</Grid>

【问题讨论】:

    标签: c# windows-phone-7


    【解决方案1】:

    首先,我建议为您的模型使用普通的旧对象(实现INotifyPropertyChanged)。依赖对象的使用是多余的。请参阅此相关问题:

    INotifyPropertyChanged vs. DependencyProperty in ViewModel

    为了将您的对象集合绑定到列表,以便 UI 在您添加/删除对象时自动更新,您应该创建一个 ObservableCollection 并将其设置为 ListBoxItemsSource

    ObservableCollection<Thing> ThingsCollection { get; set }
    
    <MainPage x:Name="_mainPage">
        <ListBox ItemsSource="{Binding ThingsCollection}" />
    </MainPage>
    

    这里的关键是ObservableCollection 实现了INotifyCollectionChanged,它会在集合更改时引发事件。 ListBox 处理这些事件以保持 UI 同步。

    【讨论】:

      【解决方案2】:

      如果您想以任何方式使用绑定,您应该考虑更多地遵循 MVVM 模式,

      即使没有它,您也可以绑定到您的对象。您应该能够执行类似于以下的操作

      //code behind main page
      Things ThingCollection { get; set }
      
      
      //in xaml
      <MainPage x:Name="_mainPage">
      
          <ListBox ItemsSource="{Binding ThingsCollection}" />
      </MainPage>
      

      这是一个简单的例子,但基本上是在 xaml 中做什么。

      【讨论】:

        猜你喜欢
        • 2011-10-03
        • 2012-03-17
        • 2014-05-03
        • 2015-01-16
        • 1970-01-01
        • 1970-01-01
        • 2011-05-28
        • 2011-02-10
        • 1970-01-01
        相关资源
        最近更新 更多