【问题标题】:Binding to a collection of user controls in XAML绑定到 XAML 中的用户控件集合
【发布时间】:2011-04-12 09:39:37
【问题描述】:

我有一组要在堆栈面板中显示的用户控件。我无法控制这些用户控件包含的方式和内容。我只知道它们是某种用户控件(可以是按钮、文本块或任何 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");
    }

...
...
}

在此示例中,我想绑定到 MyViews 并在堆栈面板中显示集合中的所有视图。我该怎么去绑定它?我是 WPF 世界的新手。

谢谢。

【问题讨论】:

    标签: c# xaml user-controls binding


    【解决方案1】:

    以下是 1 种方法的示例。

    如果您只想要默认的 ItemsPanel 模板(垂直堆栈面板),则可以删除 ItemsControl.ItemsPanel 部分 - 我只是冗余地包含了它,以便您可以查看 StackPanel 实际存在的位置,或者如果您想要以某种方式更改 StackPanel。

    ItemTemplate 由 HasViewItemTemplate 的键名显式引用,因为您的数据项是接口类型,因此您不能隐式应用数据模板。

    [您可能需要做更多的工作,例如在 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();
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-13
      • 2016-01-12
      • 1970-01-01
      • 2016-12-29
      • 2012-04-15
      • 1970-01-01
      相关资源
      最近更新 更多