2013-05-15 1 views
0

나는 매우 간단한 문제라고 생각하지만, 어떤 이유로 대답이 나에게 도피하고 있습니다. Silverlight에서 간단한 Master/Detail DataGrid를 만들고 있습니다. 대부분의 웹 샘플은 컬렉션이있는 개체를 만들고 컬렉션에 세부 그리드를 바인딩하여이를 표시합니다. 필자는 세부 그리드를 마스터 역할을하는 행과 동일한 객체에 바인딩하려고합니다. 내 예제 코드가 간단하다는 것을 알고 있지만, 간단하게 가능한 데모로 다시 만들려고합니다. 즉,이 데이터가 있다고 가정 해 보겠습니다.Silverlight Simple Master Detail Binding

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" }); 
    } 
} 

OK. 꽤 간단합니다. 이제이 컬렉션을 DataGrid에 바인딩 할 것입니다. 각 행에는 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> 

관련 문제