如何在TwoWay模式下将WPF UniformGrid与TextBoxes正确绑定到ObservableCollection

时间:2013-03-07 20:53:01

标签: wpf binding textbox observablecollection uniformgrid

我现在正在摆弄WPF很长一段时间但是我无法以某种方式抓住这个概念。我经常有几天试图解决问题。有一段时间,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中的元素,以反映在名为myMatrix的UniformGrid样式ItemsControl中。更改第一个单元格中的文本并按下按钮会显示这不起作用。应该这么简单......我错过了什么?

2 个答案:

答案 0 :(得分: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));
        }
    }
}

的Xaml:

<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>

答案 1 :(得分:0)

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

wpf-mvvm-inotifypropertychanged

Implementing the MVVM Pattern

WPF/MVVM Quick Start Tutorial --- Super Handy This One