2010-09-14 37 views
2

我有一个UserControl集合,我想在堆栈面板中显示。我无法控制这些用户控件包含的方式和内容。我只知道,他们是某种用户控制的(可能是一个按钮,一个文本块或任何UIElement。)受 这里是一个小例子绑定到XAML中的用户控件集合

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世界的新手。

谢谢。

回答

1

下面是一种实现方法的示例。

ItemsControl的ItemsControl.ItemsPanel部分可以被删除,如果你只是想要默认的ItemsPanel模板是一个垂直的stackpanel - 我只是冗余地包含它,所以你可以看到你的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(); 
     } 
    } 
}