本文介绍了如何启动说明在WPF主从(化合物列表)对象绑定最简单的代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图运行(在C#4.0,Visual Studio 2010中)从

I am trying to run (in C# 4.0, Visual Studio 2010) the code from


    代码

  • Unable to display a List as one of the columns of my view with ItemsControl and StackPanel?

具有在主窗口的 myOrders 添加实例。 VS2010 WPF应用程序项目的xaml.cs但在XAML我得到

having added instantiation of myOrders in MainWindow.xaml.cs of VS2010 WPF application project but am getting in XAML

无法解析符号 myOrders '因未知的数据上下文

在在MainWindows.xaml鼠标指向上的 myOrders 在行:

upon pointing in MainWindows.xaml a mouse on myOrders in line:

 ItemsSource="{Binding myOrders}"

什么是在这种情况下启动WPF应用程序的完整XAML脚本?

What would be the full XAML script for launching the WPF app in this case?

更新:

我删除了我的代码,因为它是多余的提供的答案。和评论结果
只是要注意的,我很感兴趣,在简单开球插图实现数据绑定的:

I removed my code since it is redundant to provided answers and comments.
Just to note that I was interested in the simplest kick-off illustration-realization of databinding of:


  • 化合物(分层或多维/交错数组或列表)名单<>对象(实例在C#中,但不是在XAML)

  • 模仿那种主详细关系,TreeView控件

我找不到在互联网准备。

which I could not find ready in the internet.

在教程中的代码,演练或例子,我能找到,要么不编译或过度臃肿的并发症模糊的概念和理解的分别。

The code in tutorials, walkthroughs or examples, I could find, either do not compile or over-bloated by complications blurring the concepts and respectively comprehension.

即。我更喜欢的,虽然我不能给予好评或将其标记为正确的,因为这不是我的。

I.e. I prefer the disappeared answer with simpler code, though I cannot upvote or mark it as correct, since it is not mine.

推荐答案

所有myOrders首先必须是公共财产。它不能被见过,如果它是一个公共变量,二来使视图(XAML)知道这是怎么回事在代码的背后,你需要设置的DataContext =这一点; 在窗口的构造函数

First of all myOrders has to be a public property. it cannot be seen even if it's a public variable, secondly to make the view(XAML) aware of what's going in the code behind you need to set DataContext = this; in the window's constructor

设置属性,像这样:公开名单<排序> myOrder {获取;设置;}

如果您需要更新从代码视图的背后则需要实施 INotifyPropertyChanged的接口。它负责执行更新视图工作。这里是href=\"http://msdn.microsoft.com/en-us/library/ms229614.aspx\" rel=\"nofollow\">从MSDN 小教程

最后一件事,(从我的经验),你应该有类型的属性去的ObservableCollection<排序> 而不是列表<排序>

If you need to update the view from code behind then you need to implement INotifyPropertyChanged interface. it's responsible of doing update view job. Here is a small tutorial from MSDN.

Last thing, (From my experience) you should go with a property of type ObservableCollection<Order> instead of List<Order>.

XAML:

<Grid>
  <ItemsControl  x:Name="visual"
                 ItemsSource="{Binding MyOrders}"
                 HorizontalAlignment="Stretch"
                 HorizontalContentAlignment="Stretch">
    <ItemsControl.ItemTemplate>
      <!-- This defines the DataTemplate to display one Order object-->
      <DataTemplate>
        <StackPanel>
          <TextBlock Text="{Binding OrderName}"
                     Margin="10" />

          <ItemsControl ItemsSource="{Binding PartsList}"
                        HorizontalAlignment="Stretch"
                        HorizontalContentAlignment="Stretch">
            <ItemsControl.ItemTemplate>
              <!-- This defines the DataTemplate to display one Parts object-->
              <DataTemplate>
                <Grid HorizontalAlignment="Stretch">
                  <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition Width="*" />
                  </Grid.ColumnDefinitions>
                  <TextBlock Grid.Column="0"
                             Text="{Binding PartName}"
                             TextAlignment="Center" />
                  <TextBlock Grid.Column="1"
                             Text="{Binding PartQuantity}"
                             TextAlignment="Center" />
                </Grid>
              </DataTemplate>
            </ItemsControl.ItemTemplate>
          </ItemsControl>
        </StackPanel>
      </DataTemplate>
    </ItemsControl.ItemTemplate>
  </ItemsControl>
</Grid>



后面的代码:

Code behind :

  /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window, INotifyPropertyChanged
{
    public ObservableCollection<Order> MyOrders
    {
        get { return _myOrders; }
        set { _myOrders = value; OnPropertyChanged("MyOrders"); }
    }

    Order order1 = new Order
     {
         OrderName = "Order1",
         PartsList = new List<Parts>()
        {
            new Parts {PartName = "Part11", PartQuantity = 11},
            new Parts {PartName = "Part12", PartQuantity = 12}
        }
     };

    private ObservableCollection<Order> _myOrders;

    public MainWindow()
    {
        InitializeComponent();
        MyOrders = new ObservableCollection<Order>();
        MyOrders.Add(order1);
        DataContext = this;
    }

    public event PropertyChangedEventHandler PropertyChanged;

    protected virtual void OnPropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
    }
}
    public class Order
    {
        public string OrderName { get; set; }
        public List<Parts> PartsList { get; set; }
    }

    public class Parts
    {
        public string PartName { get; set; }
        public double PartQuantity { get; set; }
        }
}

这篇关于如何启动说明在WPF主从(化合物列表)对象绑定最简单的代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-11 16:05