【问题标题】:Xaml two way binding of textbox to observable collectionXaml 两种方式将文本框绑定到可观察集合
【发布时间】:2014-02-03 03:47:00
【问题描述】:

在我的 WP8 应用程序中,我有一个类,它有一个名为 Matrix 的 ObservableCollection<ObservableCollection<int>> 属性。

我想使用项目控件显示这些矩阵。

 <ItemsControl ItemsSource="{Binding FirstMatrix.Matrix}">
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <ItemsControl ItemsSource="{Binding}">
                        <ItemsControl.ItemsPanel>
                            <ItemsPanelTemplate>
                                <StackPanel Orientation="Horizontal"></StackPanel>
                            </ItemsPanelTemplate>
                        </ItemsControl.ItemsPanel>
                        <ItemsControl.ItemTemplate>
                            <DataTemplate>
                                <TextBox Text="{Binding}" />
                            </DataTemplate>
                        </ItemsControl.ItemTemplate>
                    </ItemsControl>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>

代码在显示方面起作用(它用零填充,这是默认值)。但我也希望允许更改文本框,这将反映在 Matrix 属性中 - 现在无法更改文本框,因为我猜它们的值以一种方式绑定到 Matrix 单元格。我尝试设置&lt;TextBox Text="{Binding Mode=TwoWay}" /&gt;或类似的东西,但它似乎不起作用。 任何想法应该如何绑定数据?

编辑: 我已经实现了 INotifyPropertyChanged。 这是我课堂的一部分:

public partial class CalcMatrix : INotifyPropertyChanged
    {
        public ObservableCollection<ObservableCollection<int>> Matrix 
        { 
            get { return _matrix; }
            set
            {
                _matrix = value;
                OnPropertyChanged("Matrix");
            } 
        }
        private ObservableCollection<ObservableCollection<int>> _matrix;

        private void OnPropertyChanged(string argName)
        {
            var handler = PropertyChanged;
            if(handler != null)
                handler(this, new PropertyChangedEventArgs(argName));
        }
        public event PropertyChangedEventHandler PropertyChanged;
    }

我认为 TexBoxes 不改变的原因是因为绑定是单向的 - 文本始终是矩阵内部的内容。我相信我应该以某种方式将 XAML 绑定更改为 TwoWay 或其他东西,但不知道如何。有什么想法吗?

【问题讨论】:

  • "public ObservableCollection> matrix" .. 它是一个属性,但它不是像字符串或整数这样的原始数据类型,它是一个集合。

标签: xaml data-binding mvvm windows-phone-8 observablecollection


【解决方案1】:

它不起作用的原因是 itemsource 是一个 Matrix 列表,并且您没有对列表本身进行任何更改,例如从列表中添加或删除,而是您正在更改列表中存在的项目的属性我假设您正在使用 ObservableCollection.... 所以你需要实现一个 INotifyPropertyChanged 接口来告诉 UI 嘿我变了请更新你自己....

 class YourClass : INotifyPropertyChanged
{

 private string yourProperty;
 public string YourPropety{
   get{ 
        return yourProperty; 
     }
   set{
         if (value != this.yourProperty)
            {
                this.yourProperty = value;
                NotifyPropertyChanged();
            }
    }
 }       

 public event PropertyChangedEventHandler PropertyChanged;

    private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        } 
    }
 }

【讨论】:

【解决方案2】:

双向模式绑定需要路径(为什么?see this SO answer),所以你不能像{Binding Mode=TwoWay}那样做,它必须像{Binding SomePath, Mode=TwoWay}。因此,在这种情况下,您必须将矩阵项包装为某个类而不是普通 int,并将该 int 作为该类的属性值。

//your Matrix property type become:
...
public ObservableCollection<ObservableCollection<MatrixElement>> Matrix
...

//MatrixElement class is something like:
public class MatrixElement : INotifyPropertyChanged
{
    private int _value;
    public int Value
    {
        get { return _value; }
        set { 
                _value = value; 
                OnPropertyChanged("Value"); 
            }
    }

    ....
}

//then you can bind TextBox in two way mode
...
<ItemsControl.ItemTemplate>
    <DataTemplate>
        <TextBox Text="{Binding Value, Mode=TwoWay}" />
    </DataTemplate>
</ItemsControl.ItemTemplate>
...

【讨论】:

    猜你喜欢
    • 2011-12-03
    • 2013-05-21
    • 2011-02-16
    • 1970-01-01
    • 2023-02-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-29
    相关资源
    最近更新 更多