1

如果在 WPF 我有这个代码:

using System.Collections.ObjectModel;

...
...

public partial class MainWindow : Window
{
    public ObservableCollection<String> myMatrixData = new ObservableCollection<String>();
    public MainWindow()
    {
        InitializeComponent();

        int i;
        for (i = 0; i < 100; i++)
            myMatrixData.Add("Test");

        myMatrix.ItemsSource = myMatrixData;
    }

    private void Button_Click_1(object sender, RoutedEventArgs e)
    {
        Cell00.Text = myMatrixData[0];
    }
}

XAML:

    xmlns:sys="clr-namespace:System;assembly=mscorlib" 
...
...
<Grid>
    <StackPanel>
        <ItemsControl x:Name="myMatrix">
            <ItemsControl.Resources>

            </ItemsControl.Resources>

            <ItemsControl.ItemsPanel>
                <!-- specify the panel that is the container for the items -->
                <ItemsPanelTemplate>
                    <UniformGrid Rows="10" Columns="10" />
                </ItemsPanelTemplate>
            </ItemsControl.ItemsPanel>
            <!-- specify the template used to render each item -->
            <ItemsControl.ItemTemplate>
                <DataTemplate DataType="{x:Type sys:String[]}">
                    <TextBox Text="{Binding Path=., UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"/>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>
        <Button Content="Read cell 0/0" Click="Button_Click_1"/>
        <TextBlock x:Name="Cell00"/>
    </StackPanel>
</Grid>

我只想反映ObservableCollection myMatrixData要反映在一个名为的UniformGrid样式中的元素。更改第一个单元格中的文本并按下按钮表明这不起作用。ItemsControlmyMatrix

我错过了什么?

4

2 回答 2

2

ObservableCollection<string>不会更新对的更改,string因为string它本身没有实现INotifypropertyChanged

尝试为您的收藏创建一个简单的模型

例子:

private ObservableCollection<Matrix> _myMatrix = new ObservableCollection<Matrix>();

public MainWindow()
{
    InitializeComponent();
    this.DataContext = this;

    for (int i = 0; i < 100; i++)
    {
        MyMatrix.Add(new Matrix { Value = "Test" + i });
    }
}

public ObservableCollection<Matrix> MyMatrix
{
    get { return _myMatrix; }
    set { _myMatrix = value; }
}

.......


public class Matrix : INotifyPropertyChanged
{
    private string _value;
    public string Value
    {
        get { return _value; }
        set { _value = value; NotifyPropertyChanged("Value"); }
    }

    public event PropertyChangedEventHandler PropertyChanged;
    public void NotifyPropertyChanged(string property)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(property));
        }
    }
}

xml:

<Window x:Class="WpfApplication9.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:WpfApplication9"
        Title="MainWindow" Height="350" Width="525" Name="UI">
    <StackPanel>
        <ItemsControl x:Name="myMatrix" ItemsSource="{Binding MyMatrix}" >
            <ItemsControl.ItemsPanel>
                <!-- specify the panel that is the container for the items -->
                <ItemsPanelTemplate>
                    <UniformGrid Rows="10" Columns="10" />
                </ItemsPanelTemplate>
            </ItemsControl.ItemsPanel>
            <!-- specify the template used to render each item -->
            <ItemsControl.ItemTemplate>
                <DataTemplate DataType="{x:Type local:Matrix}">
                    <TextBox Text="{Binding Path=Value, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"/>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>
        <Button Content="Read cell 0/0" Click="Button_Click_1" />
        <TextBlock x:Name="Cell00" />
    </StackPanel>

</Window>
于 2013-03-07T21:39:12.950 回答
0

我发现这对于使用 NotifyPropertyChanged 和命令进行绑定非常有用

wpf-mvvm-inotifypropertychanged

实现 MVVM 模式

WPF/MVVM 快速入门教程 --- 超级好用的这个

于 2013-03-07T21:15:56.190 回答