2009-12-24 46 views
7

我有一個WPF窗口與設置爲其DataContext的視圖模型,並與一個DataTemplate和其的ItemsSource列表框綁定到視圖模型,如在下面的例子:在WPF中,如何從包含ListBox的DataTemplate中將數據綁定到Window DataContext?

查看模型:

using System.Collections.Generic; 

namespace Example 
{ 
    class Member 
    { 
     public string Name { get; set; } 
     public int Age { get; set; } 
    } 

    class Team 
    { 
     private List<Member> members = new List<Member>(); 

     public string TeamName { get; set; } 
     public List<Member> Members { get { return members; } } 
    } 
} 

MainWindow.xaml:

<Window x:Class="Example.MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
xmlns:l="clr-namespace:Example" 
    Title="Example" Height="300" Width="300" Name="Main"> 

<Window.DataContext> 
    <l:Team TeamName="The best team"> 
    <l:Team.Members> 
    <l:Member Name="John Doe" Age="23"/> 
    <l:Member Name="Jane Smith" Age="20"/> 
    <l:Member Name="Max Steel" Age="24"/> 
    </l:Team.Members> 
    </l:Team> 
</Window.DataContext> 

<ListBox ItemsSource="{Binding Path=Members}"> 
    <ListBox.ItemTemplate> 
    <DataTemplate> 
    <StackPanel Orientation="Horizontal"> 
    <TextBlock Text="{Binding Path=TeamName}" Margin="4"/> 
    <TextBlock Text="{Binding Path=Name}" Margin="4"/> 
    </StackPanel> 
    </DataTemplate> 
    </ListBox.ItemTemplate> 
</ListBox> 
</Window> 

當然,團隊一流的TeamName屬性不會因爲LisBox的每一個項目是List.ItemTemplate的DataContext的顯示在列表框中的項目,它將覆蓋ŧ他窗口的DataContext。

問題是:如何從列表框的DataTemplate中綁定到視圖模型(Window.DataContext)的TeamName屬性?

回答

12

我會提取L:團隊宣言到Window.Resources部分,然後在DataContext和的DataTemplate中引用它:

<Window x:Class="Example.MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
xmlns:l="clr-namespace:Example" 
    Title="Example" Height="300" Width="300" Name="Main"> 

<Window.Resources> 
    <l:Team x:Key="data" TeamName="The best team"> 
    <l:Team.Members> 
    <l:Member Name="John Doe" Age="23"/> 
    <l:Member Name="Jane Smith" Age="20"/> 
    <l:Member Name="Max Steel" Age="24"/> 
    </l:Team.Members> 
    </l:Team> 
<Window.Resources> 

<Window.DataContext> 
    <StaticResource ResourceKey="data"/> 
</Window.DataContext> 

<ListBox ItemsSource="{Binding Path=Members}"> 
    <ListBox.ItemTemplate> 
    <DataTemplate> 
    <StackPanel Orientation="Horizontal"> 
    <TextBlock Text="{Binding Source={StaticResource data}, Path=TeamName}" Margin="4"/> 
    <TextBlock Text="{Binding Path=Name}" Margin="4"/> 
    </StackPanel> 
    </DataTemplate> 
    </ListBox.ItemTemplate> 
</ListBox> 
</Window> 
+0

+1。當然了!我沒有想過將視圖模型作爲一個資源共享,我試圖用RelativeSources和FindAncestor使事情複雜化,但沒有成功。 – 2009-12-25 00:38:09

+0

糟糕,如果我有足夠的聲望,我會爲您投票 – 2009-12-25 00:40:17

1

我想創建一個團隊成員視圖模型,用一個TeamName財產,是這樣的:

class MemberViewModel 
{ 
    ... 
    private TeamViewModel _team; 
    public string TeamName{ get { return _team.Name; } } 
} 

class TeamViewModel 
{ 
    public List<MemberViewModel> Members { get{ ... } } 
    // You may consider using ObservableCollection<> instead of List<> 
} 

那麼你的XAML看起來那樣乾淨你的榜樣。有了MVVM,你不應該在視圖中需要任何特殊的綁定技巧。所有你需要的應該可以通過視圖模型獲得。

+1

我該如何處理數據綁定? – 2009-12-25 00:43:01

14

您還可以使用的RelativeSource約束力,但它並不複雜:

<TextBlock Text="{Binding Path=DataContext.TeamName, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}" Margin="4"/> 
0

你爲什麼不綁定您的DataContext組隊,然後使你的ItemSource勢必team.members?

通常我在我的代碼隱藏中分配我的datacontext,但它仍然不應該爲你做任何不同的事情。

itemsouce = 「{綁定路徑=」 team.Members「}

我想真正的同樣的事情,有什麼建議阿維亞德,只是我給你的datacontext在我隱藏。

相關問題