DataContext和Bindings的新手问题

时间:2013-06-19 09:16:33

标签: c# wpf windows-phone-7 xaml data-binding

我绝对不是C#和.NET的新手,但由于我很长时间没有使用XAML,我面临一个非常简单的问题...

我已经创建了一个测试模型(ResultsModel),如下所示:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;

namespace Test_Project_
{
    public class ResultsModel : INotifyPropertyChanged
    {
        private string _Monthly;
        public string Monthly
        {
            get { return _Monthly; }
            set { _Monthly = value; NotifyPropertyChanged("Monthly"); }
        }

        private string _TotalAmount;
        public string TotalAmount
        {
            get { return _TotalAmount; }
            set { _TotalAmount = value; NotifyPropertyChanged("TotalAmount"); }
        }

        private string _TotalInterest;
        public string TotalInterest
        {
            get { return _TotalInterest; }
            set { _TotalInterest = value; NotifyPropertyChanged("TotalInterest"); }
        }

        List<Dictionary<string, double>> _Items;
        public List<Dictionary<string, double>> Items
        {
            get { return _Items; }
            set { _Items = value; NotifyPropertyChanged("Items"); }
        }

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

在我的MainPage课程中

public ResultsModel Results;
public MainPage()
{
    InitializeComponent();
    Results = new ResultsModel();
    DataContext = Results;
}

现在,抓住这个问题:

当我尝试绑定时,例如我的模型的一些简单字符串值的TextBlock Text属性(例如Text={Binding Monthly}),它可以正常工作。

现在,请注意我还有一个网格,有四列。

我想获取Items属性(ResultsModel)中的所有项目,并按键在网格中显示它们。

E.g。

显示列表项的“A”键,在“A”列(比如网格的第一列)等。

我该怎么做(在XAML和/或C#代码中)?


更新:


(基于建议,我尝试了以下方法,但仍然无效)

内部Grid XAML:

<ItemsControl ItemsSource="{Binding Items, Mode=TwoWay}">
     <ItemsControl.ItemTemplate>
          <DataTemplate>
               <TextBlock Grid.Column="0" Text="{Binding Path=num}" />
          </DataTemplate>
     </ItemsControl.ItemTemplate>
</ItemsControl>

但我收到以下错误:

  

System.Windows.Data错误:BindingExpression路径错误:'num'   找不到物业   'System.Collections.Generic.Dictionary 2[System.String,System.String]' 'System.Collections.Generic.Dictionary 2 [System.String,System.String]'   (的HashCode = 57616766)。 BindingExpression:Path ='num'   的DataItem = 'System.Collections.Generic.Dictionary`2 [System.String,System.String]'   (的HashCode = 57616766);目标元素是   'System.Windows.Controls.TextBlock'(Name ='');目标属性是   'Text'(输入'System.String')..

此外,请注意Items已转换为字符串字符串键值对列表。

有什么想法吗?

2 个答案:

答案 0 :(得分:3)

<ItemsControl ItemsSource="{Binding Items}">
  <ItemsControl.ItemTemplate>
    <!-- datatemplate to render your items -->
  </ItemsControl.ItemTemplate>
  <ItemsControl.ItemsPanelTemplate>
    <!-- wrappanel or what ever to control orientation and stuff -->
  </ItemsControl.ItemsPanelTemplate>

答案 1 :(得分:0)

你刚刚在绑定中尝试过这个:

{Binding Monthly, Mode = TwoWay}

在列定义和textBox中。

编写列时,必须具有以下内容:

<datagrid.column>
<datagridtextColumn header
binding />
</datagrid.column>

如果我记得很清楚,你可以在列上进行绑定。

这必须链接textBlock和Column之间的值。

这可以解决您的问题。

这适合我。

相关问题