簡體   English   中英

綁定到XAML中的用戶控件集合

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

我有一個要在堆棧面板中顯示的UserControls集合。 我無法控制這些用戶控件的內容和方式。 我只知道它們是某種用戶控件(可以是按鈕,文本塊或任何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世界的新手。

謝謝。

這是一種方法的示例。

如果只需要默認的ItemsPanel模板(垂直堆棧面板),則可以刪除ItemsControl的ItemsControl.ItemsPanel部分-我只是多余地包含了它,這樣您就可以看到StackPanel實際存在的位置,或者是否要更改StackPanel某種程度上來說。

HasTemplateItemTemplate的鍵名由ItemTemplate顯式引用,因為您的數據項是接口類型,因此您不能隱式應用數據模板。

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