C# Wpf集合雙向綁定


說明:

msdn中   ObservableCollection<T> 類    表示一個動態數據集合,在添加項、移除項或刷新整個列表時,此集合將提供通知。

在許多情況下,所使用的數據是對象的集合。 例如,數據綁定中的一個常見方案是使用 ItemsControl(如 ListBoxListView 或 TreeView)來顯示記錄的集合。

可以枚舉實現 IEnumerable 接口的任何集合。 但是,若要設置動態綁定,以便集合中的插入或刪除操作可以自動更新 UI,則該集合必須實現 INotifyCollectionChanged 接口。 此接口公開 CollectionChanged 事件,只要基礎集合發生更改,都應該引發該事件。

WPF 提供 ObservableCollection<T> 類,它是實現 INotifyCollectionChanged 接口的數據集合的內置實現。

還有許多情況,我們所使用的數據只是單純的字段或者屬性,此時我們需要為這些字段或屬性實現INotifyPropertyChanged接口,實現了該接口,只要字段或屬性的發生了改變,就會提供通知機制。

實例:

Xaml指定TwoWay雙向綁定

<Grid>
    <StackPanel Height="295" HorizontalAlignment="Left" Margin="10,10,0,0" Name="stackPanel1" VerticalAlignment="Top" Width="427">
        <TextBlock Height="23" Name="textBlock1" Text="學員編號:" />
        <TextBox Height="23" Name="txtStudentId" Width="301" HorizontalAlignment="Left"/>
        <TextBlock Height="23" Name="textBlock2" Text="學員列表:" />
        <ListBox Height="156" Name="lbStudent" Width="305" HorizontalAlignment="Left">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Name="stackPanel2" Orientation="Horizontal">
                        <TextBlock  Text="{Binding Id,Mode=TwoWay}" Margin="5" Background="Beige"/>
                        <TextBlock Text="{Binding Name,Mode=TwoWay}" Margin="5"/>
                        <TextBlock  Text="{Binding Age,Mode=TwoWay}" Margin="5"/>
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
        <Button Content="Button" Height="23" Name="button1" Width="75" HorizontalAlignment="Left" Click="button1_Click" />
    </StackPanel>
</Grid>

后台代碼創建ObservableCollection<T>實例:

ObservableCollection<Students> infos = new ObservableCollection<Students>() {
    new Students(){ Id=1, Age=11, Name="Tom"},
    new Students(){ Id=2, Age=12, Name="Darren"},
    new Students(){ Id=3, Age=13, Name="Jacky"},
    new Students(){ Id=4, Age=14, Name="Andy"}
    };
public Bind7()
{
    InitializeComponent();

    this.lbStudent.ItemsSource = infos;
    this.txtStudentId.SetBinding(TextBox.TextProperty, new Binding("SelectedItem.Id") { Source = lbStudent });
}
private void button1_Click(object sender, RoutedEventArgs e)
{
    infos[1] = new Students() { Id = 4, Age = 14, Name = "這是一個集合改變" };
    infos[2].Name = "這是一個屬性改變";
}
public class Students
{
    public int Id { get; set; }
    public string Name { get; set; }
    public int Age { get; set; }
}

顯示結果:

說明,ObservableCollection<T>只針對列表數據項修改進行通知,對於列表項屬性修改不進行通知

如果想對於對象屬性修改也通知到UI,需要類實現INotifyPropertyChanged接口

public class Students : INotifyPropertyChanged
{
    string _name;
    public int Id { get; set; }
    public string Name
    {
        get { return _name; }
        set { _name = value; OnPropertyChanged("Name"); }
    }
    public int Age { get; set; }
    protected internal virtual void OnPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }
    public event PropertyChangedEventHandler PropertyChanged;
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM