简体   繁体   English

绑定到XAML中的用户控件集合

[英]Binding to a collection of user controls in XAML

I've a collection of UserControls which I want to show in a stack panel. 我有一个要在堆栈面板中显示的UserControls集合。 I've no control over how and what those user controls contain. 我无法控制这些用户控件的内容和方式。 I just know that they are some sort of user controls (could be a button, a textblock or any UIElement.) Here is a small example 我只知道它们是某种用户控件(可以是按钮,文本块或任何UIElement。)这是一个小示例

public class Car : IHasView
{
     public UserControl MyView { get return new MyCarViewThatHasAButton(); }
}

public class Jeep : IHasView
{
    public UserControl MyView { get return new MyJeepViewThatHasATextblock(); }
}

public class MainView : INotifyPropertyChanged{
    private ICollection _myViews;
    public ICollection MyViews {
    get { return _myViews;}
    set{ 
       _myViews = value; 
       NotifyPropertyChanged("MyViews");
    }

...
...
}

In this example, I want to bind to MyViews and show all the views in the collection in a stack panel. 在此示例中,我想绑定到MyViews并在堆栈面板中显示集合中的所有视图。 How should I go and bind it? 我应该如何绑定它? I'm new to WPF world. 我是WPF世界的新手。

Thanks. 谢谢。

Here's an example of 1 way to do it. 这是一种方法的示例。

The ItemsControl.ItemsPanel part of the ItemsControl can be removed if you just want the default ItemsPanel template which is a vertical stackpanel - I've just redundantly included it so you can see where your StackPanel actually exists, or if you want to change the StackPanel in some way. 如果只需要默认的ItemsPanel模板(垂直堆栈面板),则可以删除ItemsControl的ItemsControl.ItemsPanel部分-我只是多余地包含了它,这样您就可以看到StackPanel实际存在的位置,或者是否要更改StackPanel某种程度上来说。

The ItemTemplate is referred to by explicitly by the key name of HasViewItemTemplate, because you can't have the data templates applied implicity because your data items are interface types. HasTemplateItemTemplate的键名由ItemTemplate显式引用,因为您的数据项是接口类型,因此您不能隐式应用数据模板。

[ You might need to do more work like use a singleton on the MyCarViewThatHasAButton and MyCarViewThatHasAButton, use a better pattern for NotifyPropertyChanged, etc. ] [您可能需要做更多的工作,例如在MyCarViewThatHasAButton和MyCarViewThatHasAButton上使用单例,对NotifyPropertyChanged使用更好的模式,等等。]


<Window x:Class="WpfApplication3.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:WpfApplication3="clr-namespace:WpfApplication3" Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <WpfApplication3:MainView x:Key="MainView"/>
        <DataTemplate x:Key="HasViewItemTemplate">
            <ContentPresenter Content="{Binding Path=MyView}"/>
        </DataTemplate>
    </Window.Resources>
    <Grid>
        <ItemsControl Background="Yellow" ItemTemplate="{StaticResource HasViewItemTemplate}" ItemsSource="{Binding Source={StaticResource MainView}, Path=MyViews}">
            <ItemsControl.ItemsPanel>
                <ItemsPanelTemplate>
                    <VirtualizingStackPanel Orientation="Vertical"/>
                </ItemsPanelTemplate>
            </ItemsControl.ItemsPanel>
        </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;
using System.Collections;
using System.ComponentModel;
using System.Collections.ObjectModel;

namespace WpfApplication3
{
    public interface IHasView
    {
        UserControl MyView { get; }
    }

    public class Car : IHasView
    {
        public UserControl MyView
        { 
            get
            {
                // Demo UserControl as prodcued by MyCarViewThatHasAButton

                UserControl ctl = new UserControl();

                ctl.Background = Brushes.Red;

                Button button = new Button();

                button.Content = "a car";

                ctl.Content = button;

                return ctl;
            }
        }
    }

    public class Jeep : IHasView
    {
        public UserControl MyView
        {
            get
            {
                // Demo UserControl as produced by MyJeepViewThatHasATextblock

                UserControl ctl = new UserControl();

                ctl.Background = Brushes.Blue;
                ctl.Width = 50;

                TextBlock textblock = new TextBlock();

                textblock.Text = "a jeep";

                ctl.Content = textblock;

                return ctl;
            }
        }
    }

    public class MainView : INotifyPropertyChanged{

        public MainView()
        {
            ObservableCollection<IHasView> list = new ObservableCollection<IHasView>()
                                                      {
                                                          new Car(),
                                                          new Jeep()
                                                      };

            MyViews = list;
        }

        private ICollection _myViews;
        public ICollection MyViews
        {
            get
            {
                return _myViews;
            }
            set
            { 
               _myViews = value; 
               NotifyPropertyChanged("MyViews");
            }
        }

        private void NotifyPropertyChanged(string p)
        {
            if (PropertyChanged != null)
            {
                PropertyChangedEventArgs e = new PropertyChangedEventArgs(p);

                PropertyChanged(this, e);
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
    }

    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM