2010-05-25 104 views
0

我有一个基于网格控件的自定义用户控件。我有一个ViewModel公开这个属性。我想在视图上的XAML绑定到这个。我相信这一定很容易,但对于WPF来说我还是个新手。这是如何实现的?WPF XAML绑定网格

预先

非常感谢(编辑补充的详细信息)的视图

实施例而不结合视图模型。请注意,我有一个自定义网格,其中包含许多自定义堆叠面板,其中包含许多自定义的内容控件。这些是在ViewModel加载期间确定的。

<MyCustomGrid:CustomGrid> 
    <MyCustomGrid:CustomStackPanel> 
     <MyCustomGrid:CustomHeaderedContentControl/> 
    </MyCustomGrid:CustomStackPanel> 
    <MyCustomGrid:CustomStackPanel> 
     <MyCustomGrid:CustomHeaderedContentControl/> 
    </MyCustomGrid:CustomStackPanel> 
</MyCustomGrid:CustomGrid> 

视图模型只包含其中包含一个列表等。注意,CustomGrid是因为可能有不止一个,但只有一个具有特定属性将被绑定列表的列表。

+2

有很多方法可以做到这一点,取决于它是如何设置的。 你可以发表一些关于你的用户控件和viewmodel的外观的代码吗?当问题情景更具体时,解释答案会简单得多。 – ThomasAndersson 2010-05-25 09:48:49

+0

我已更新帖子,谢谢。 – 2010-05-25 09:54:52

回答

0

基本步骤

  1. 设置用户控件的DataContext的(或控制/窗口,包含用户控件)一些模型类型的实例,与数据属性,是一个ObservableCollection<SomeDataType>

  2. ListViewItemSource绑定到Data属性。

  3. GridViewColumnDisplayMemberBinding财产归属于SomeDataType的财产。

因此,你将有这样的事情:

<ListView SelectionMode="Single" 
      HorizontalAlignment="Stretch" 
      ItemsSource="{Binding Data}"> 
    <ListView.View> 
     <GridView > 
      <GridViewColumn Header="Name of Prop2" 
          DisplayMemberBinding="{Binding Path=Prop1}" /> 
      <GridViewColumn Header="Name of Prop2" 
          DisplayMemberBinding="{Binding Path=Prop2}" /> 
      <GridViewColumn Header="Name of Prop3" 
          DisplayMemberBinding="{Binding Path=Prop3}" /> 
     </GridView> 
    </ListView.View> 
</ListView> 

您可以绑定到其他类型,并且有超过格式化等的控制,但是这是一个简单的情况。

+0

这并不完全给我我想要的。网格,它应该呈现的堆叠面板以及要在这些面板中显示的内容在ViewModel实例化时在视图模型中实例化。我只是想在视图中呈现。对不起,如果我没有解释得很好。 – 2010-05-25 10:30:45

+0

@JonArchway:WPF控件将绑定到控件的'DataContext'上的属性。所以你需要确保你已经将'DataContext'设置为适当的属性,并且这些属性被填充。如果你包含了你想要绑定的结构的某些东西(我怀疑你错过了一些明显的东西 - 一旦它被指出)。 – Richard 2010-05-25 12:21:04

+0

好吧,所以如果我在代码隐藏中这样做,我可以通过做一些像SomeGridOnTheXamlView.Children.Add(viewModel.MyCustomGrid)添加一切。我想直接在XAML中做这样的事情?我显然在这里很愚蠢,所以随时指出我的愚蠢! :-) – 2010-05-25 12:34:25

0

我现在还不能确定你要实现什么,但我有一个去反正,也许我们可以从那里迭代到一个解决方案。

从上面的XAML判断,您有几个自定义控件。你提到的最高级别是基于Grid的。 Grid是在控件中,还是作为ItemsControl的ItemPanelTemplate?

我有点觉得你想要数据绑定你的viewmodel的内容,并且你需要将你的自定义控件基于ItemsControl。纠正我,如果我错了。

像这样的东西(如mentionded,这是假设MyCustomGrid作为一个ItemsControl:

<UserControl Name="MyCustomGrid"> 
      <ItemsControl ItemsSource="{Binding Path=MyStackPanelsCollection}"> 
       <ItemsControl.ItemsPanel> 
        <ItemsPanelTemplate> 
         <Grid IsItemsHost="True" /> 
        </ItemsPanelTemplate> 
       </ItemsControl.ItemsPanel> 
       <ItemsControl.ItemContainerStyle> 
        <Style TargetType="{x:Type MyCustomGrid:MyCustomStackPanel}"> 
         <Setter Property="Grid.Row" Value="{Binding Path=GridRow}" /> 
         <Setter Property="Grid.Column" Value="{Binding Path=GridColumn}" /> 
        </Style> 
       </ItemsControl.ItemContainerStyle> 
       <ItemsControl.ItemTemplate> 
        <DataTemplate DataType="{x:Type MyCustomGrid:MyCustomStackPanelViewModel}"> 
         <ContentPresenter Content="{Binding Path=MySerializedObjectTurnedInToControl}" /> 
        </DataTemplate> 
       </ItemsControl.ItemTemplate> 
      </ItemsControl> 
     </UserControl> 

如果情况并非如此,因为在你直接绑定您的CustomStackPanel等,以一个指定的属性在您的视图模型,它可能看起来像:

<MyCustomGrid:CustomGrid DataContext="{Binding Path=ViewModel}"> 
    <MyCustomGrid:CustomStackPanel DataContext="{Binding Path=MyFirstStackPanelViewModel}"> 

...如果

对不起,我误会,我仍然很可能是缺少明显的东西

+0

我认为我遇到的问题是在XAML中创建视图时不知道CustomStackPanel。加载后才知道CustomStackPanels的数量。与这些堆叠面板的内容相同。我没有解释得很好,这些控件的内容基本上是从文件中加载的。该文件包含一个表示应该加载的序列化对象模型。我有点想简单地说是否合理? – 2010-05-25 12:17:42

+0

这确实使事情更清楚一点。我仍在考虑为您的自定义序列化对象基于ItemsControl和DataTemplates的解决方案。我将用示例编辑我的帖子。 – ThomasAndersson 2010-05-25 12:21:36