WPF包含如何将数据传递给usercontrol

时间:2016-03-12 21:40:32

标签: wpf mvvm binding user-controls

我不是100%这与整个wpf mvvm设计模式有关,但我对如何做到这一点感到失望。

我在这个主视图中有一个主视图我有一个名为' DemoClass_List'的ObservableCollection,列表中的每个元素都包含一个类' DemoClass'。

在这个视图的wpf中我有了wrappanel,我想要的是让wrappanel为每个元素显示一个usercontrol,同时也将单个类传递给元素。

代码如下。

***UserControl
public partial class DemoClass_Display : UserControl
{
    public DemoClass _demoClass;

    public DemoClass_Display()
    {
        InitialiseComponent();
        //Do processing on _demoClass and display relevant info
    }
}

***MainView WPF Snippet
<ItemsControl ItemsSource = "{Binding DemoClass_List}">
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <WrapPanel/>
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <usercontrol:DemoClass_Display/>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

问题是,如何填写项目&#34; public DemoClass _demoClass;&#34;在usercontrol中?

****************编辑**************** 此代码崩溃

****的MainView

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;

namespace WpfApplication1
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        public static ObservableCollection<DemoClass> DemoClass_List { get; set; }

        public Window1()
        {
            DemoClass_List = new ObservableCollection<DemoClass>();
            DemoClass dc1 = new DemoClass();
            dc1.Property1 = 1;
            dc1.Property2 = 1;
            dc1.Property3 = 1;

           DemoClass dc2 = new DemoClass();
           dc2.Property1 = 2;
           dc2.Property2 = 2;
           dc2.Property3 = 2;

           DemoClass dc3 = new DemoClass();
           dc3.Property1 = 3;
           dc3.Property2 = 3;
           dc3.Property3 = 3;

           DemoClass_List.Add(dc1);
           DemoClass_List.Add(dc2);
           DemoClass_List.Add(dc3);



           InitializeComponent();
       }
   }
}

*** MainViewWPF

<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:UserControl="clr-namespace:WpfApplication1"
    DataContext="{Binding RelativeSource={RelativeSource Self}}"
    Title="Window1" Height="300" Width="300">
    <Grid>
        <ItemsControl ItemsSource="{Binding DemoClass_List}">
            <ItemsControl.ItemsPanel>
                <ItemsPanelTemplate>
                   <WrapPanel/>
                </ItemsPanelTemplate>
            </ItemsControl.ItemsPanel>
            <ItemsControl.ItemTemplate>
               <DataTemplate>
                    <UserControl:DemoClass_Display demoClass="{Binding}"/>
                </DataTemplate>
           </ItemsControl.ItemTemplate>
       </ItemsControl>
    </Grid>
</Window>

***用户控件

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfApplication1
{
    /// <summary>
    /// Interaction logic for DemoClass_Display.xaml
    /// </summary>
    public partial class DemoClass_Display : UserControl
    {
        public static readonly DependencyProperty DemoClassProperty = DependencyProperty.Register("demoClass", typeof (DemoClass), typeof (DemoClass_Display), new PropertyMetadata(default(DemoClass)));

        public DemoClass demoClass
        {
            get { return (DemoClass)GetValue(DemoClassProperty); }
            set { SetValue(DemoClassProperty, value); }
        }

        public DemoClass_Display()
        {
            InitializeComponent();
            Console.WriteLine(demoClass.Property1);
        }
    }
}

控制台因demoClass未设置而崩溃

1 个答案:

答案 0 :(得分:1)

您需要在UserControl

中创建依赖项属性
public static readonly DependencyProperty DemoClassProperty = DependencyProperty.Register(
    "DemoClass", typeof (DemoClass), typeof (DemoClass_Display), new PropertyMetadata(default(DemoClass), OnDemoClassChanged));

public DemoClass DemoClass
{
    get { return (DemoClass) GetValue(DemoClassProperty); }
    set { SetValue(DemoClassProperty, value); }
}

private static void OnDemoClassChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)
{
    var newDemoClass = e.NewValue as DemoClass;
    var demoClassDisplay = (DemoClass_Display)dependencyObject; // you can access this UserControl this way.       

    if(newDemoClass != null)
    {
        //Do processing on newDemoClass and display relevant info
    }
}

然后你可以绑定ItemsControlDataTemplate的当前项:

<ItemsControl.ItemTemplate>
    <DataTemplate>
        <usercontrol:DemoClass_Display DemoClass="{Binding}" />
    </DataTemplate>
</ItemsControl.ItemTemplate>

因此,在UserControl内,您将使用DemoClass属性而不是字段_demoClass。我认为甚至可以删除。