2013-05-15 23 views
0

我有我認爲是一個非常簡單的問題,但出於某種原因,答案是逃避我。我正在Silverlight中創建一個簡單的Master/Detail DataGrid。 Web上的大多數示例都通過創建具有某種集合的對象來顯示此內容,並將Detail網格綁定到集合。在我的情況下,我只想將細節網格綁定到與充當主人的行相同的對象。我知道我的示例代碼很簡單,但我只是試圖讓最簡單的演示重新創建它。也就是說,我有這樣的數據:Silverlight簡單大師詳細綁定

public class Customer 
{ 
    public int CustomerId { get; set; } 
    public string CustomerName { get; set; } 
    public string FavoriteColor { get; set; } 
} 

public class CustomerCollection : ObservableCollection<Customer> 
{ 
    public CustomerCollection() 
    { 
     Add(new Customer() { CustomerId = 101, CustomerName = "Todd", FavoriteColor = "Red" }); 
     Add(new Customer() { CustomerId = 102, CustomerName = "Melissa", FavoriteColor = "White" }); 
     Add(new Customer() { CustomerId = 102, CustomerName = "Alicia", FavoriteColor = "Blue" }); 
     Add(new Customer() { CustomerId = 104, CustomerName = "Matthew", FavoriteColor = "Yellow" }); 
    } 
} 

好的。相當簡單。現在,我將把這個集合綁定到一個數據網格。每行應該顯示CustomerId和CustomerName。當你點擊這一行時,我想在細節數據網格中顯示他們最喜歡的顏色。

所以問題是...如何綁定細節網格,使其顯示最喜歡的顏色?換句話說,如何將綁定到父行作爲我的數據源?

<UserControl x:Class="Sample.MainPage" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    mc:Ignorable="d" 
    d:DesignHeight="419" d:DesignWidth="742" 
      xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk" 
      xmlns:src="clr-namespace:Sample"> 

    <UserControl.Resources> 
     <src:CustomerCollection x:Key="CustDs"></src:CustomerCollection> 
    </UserControl.Resources> 

    <Grid x:Name="LayoutRoot" Background="White" DataContext="{Binding Source={StaticResource CustDs}}"> 
     <Grid.RowDefinitions> 
      <RowDefinition Height="56*" /> 
      <RowDefinition Height="363*" /> 
     </Grid.RowDefinitions> 
     <TextBlock Name="TextBlock1" Text="Customer Information" FontSize="28" TextAlignment="Center" /> 
     <sdk:DataGrid AutoGenerateColumns="False" Grid.Row="1" 
         Height="301" HorizontalAlignment="Left" Margin="30,22,0,0" 
         Name="DgCust" VerticalAlignment="Top" Width="681" ItemsSource="{Binding}" 
         HeadersVisibility="All" ColumnWidth="*"> 
      <sdk:DataGrid.Columns> 
       <sdk:DataGridTextColumn Header="Customer Id" Binding="{Binding CustomerId}"></sdk:DataGridTextColumn> 
       <sdk:DataGridTextColumn Header="Customer Name" Binding="{Binding CustomerName}"></sdk:DataGridTextColumn> 
      </sdk:DataGrid.Columns> 
      <sdk:DataGrid.RowDetailsTemplate> 
       <DataTemplate> 
        <sdk:DataGrid Height="200" Width="600" AutoGenerateColumns="False" ColumnWidth="*" 
            ItemsSource="{Binding}"> 
         <sdk:DataGrid.Columns> 
          <sdk:DataGridTextColumn Header="Favorite Color" Binding="{Binding}"></sdk:DataGridTextColumn> 
         </sdk:DataGrid.Columns> 
        </sdk:DataGrid> 
       </DataTemplate> 
      </sdk:DataGrid.RowDetailsTemplate> 
     </sdk:DataGrid> 
    </Grid> 
</UserControl> 

回答

1

您的數據不代表主/從情況。如果你只是想顯示在細節區域的最喜歡的顏色,在DataTemplate中部分做到這一點:

  <sdk:DataGrid.RowDetailsTemplate> 
      <DataTemplate> 
       <TextBlock Text="{Binding FavoriteColor}" /> 
      </DataTemplate> 
     </sdk:DataGrid.RowDetailsTemplate> 

如果你真的想主/詳細信息,試試這個:

public class Customer 
{ 
    public int CustomerId { get; set; } 
    public string CustomerName { get; set; } 
    public string FavoriteColor { get; set; } 
    public List<FavoriteShow> Shows { get; set; } 
} 

public class FavoriteShow 
{ 
    public string Name {get;set;} 
    public int Stars {get;set;} 
} 

public class CustomerCollection : ObservableCollection<Customer> 
{ 
    public CustomerCollection() 
    { 
     List<FavoriteShow> showList1 = new List<FavoriteShow>(); 
     showList1.Add(new FavoriteShow { Name="Bugs Bunny", Stars = 4}); 
     showList1.Add(new FavoriteShow { Name="Superman", Stars=2}); 
     showList1.Add(new FavoriteShow { Name="A-Team", Stars=3}); 

     List<FavoriteShow> showList2 = new List<FavoriteShow>(); 
     showList2.Add(new FavoriteShow { Name = "Dallas", Stars = 1 }); 
     showList2.Add(new FavoriteShow { Name = "Loony Tunes", Stars = 3 }); 

     Add(new Customer() { CustomerId = 101, CustomerName = "Todd", FavoriteColor = "Red", Shows = showList1 }); 
     Add(new Customer() { CustomerId = 102, CustomerName = "Melissa", FavoriteColor = "White" }); 
     Add(new Customer() { CustomerId = 102, CustomerName = "Alicia", FavoriteColor = "Blue", Shows = showList2 }); 
     Add(new Customer() { CustomerId = 104, CustomerName = "Matthew", FavoriteColor = "Yellow" }); 
    } 
} 

而XAML :

<UserControl x:Class="Sample.MainPage" 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
mc:Ignorable="d" 
     d:DesignHeight="419" 
     d:DesignWidth="742" 
     xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk" 
     xmlns:src="clr-namespace:Sample"> 

<UserControl.Resources> 
    <src:CustomerCollection x:Key="CustDs"></src:CustomerCollection> 
</UserControl.Resources> 

<Grid x:Name="LayoutRoot" 
     Background="White" 
     DataContext="{Binding Source={StaticResource CustDs}}"> 
    <Grid.RowDefinitions> 
     <RowDefinition Height="56*" /> 
     <RowDefinition Height="363*" /> 
    </Grid.RowDefinitions> 
    <TextBlock Name="TextBlock1" 
       Text="Customer Information" 
       FontSize="28" 
       TextAlignment="Center" /> 
    <sdk:DataGrid AutoGenerateColumns="False" 
        Grid.Row="1" 
        Height="301" 
        HorizontalAlignment="Left" 
        Margin="30,22,0,0" 
        Name="DgCust" 
        VerticalAlignment="Top" 
        Width="681" 
        ItemsSource="{Binding}" 
        HeadersVisibility="All" 
        ColumnWidth="*"> 
     <sdk:DataGrid.Columns> 
      <sdk:DataGridTextColumn Header="Customer Id" 
            Binding="{Binding CustomerId}"></sdk:DataGridTextColumn> 
      <sdk:DataGridTextColumn Header="Customer Name" 
            Binding="{Binding CustomerName}"></sdk:DataGridTextColumn> 
     </sdk:DataGrid.Columns> 
     <sdk:DataGrid.RowDetailsTemplate> 
      <DataTemplate> 
       <StackPanel> 
        <TextBlock Text="{Binding FavoriteColor}" /> 
        <sdk:DataGrid ItemsSource="{Binding Shows}" /> 
       </StackPanel> 
      </DataTemplate> 
     </sdk:DataGrid.RowDetailsTemplate> 
    </sdk:DataGrid> 
</Grid>