2014-09-21 3 views
0

내 응용 프로그램에서 두 가지 모델이 있습니다마스터 - 세부 데이터 바인딩

class Line 
{ 
    string line_id { get; set; } 
    string color { get; set; } 
} 

class Point 
{ 
    string point_id { get; set; } 
    string line_id { get; set; } 
    int weight { get; set; } 
} 

를 내가 두 ObservableCollection에 있습니다

lines - ObservableCollection<Line> 
points - ObservableCollection<Point> 

내가 두 ListBox'es를 표시하려면 : 첫 번째 (외부)에 디스플레이 라인, 보조 라인 (,)을 표시합니다. 내부옵니다 각 라인에 대한 포인트를 표시 할

lvPoint.DataContext = lines; 

가 어떻게 DataContext를 설정할 수 있습니다

<ListView x:Name="lvPoint" ItemsSource="{Binding}"> 
    <ListView.ItemTemplate> 
    <DataTemplate> 
     <StackPanel> 
     <TextBlock Text="{Binding color, Mode=TwoWay}" /> 
      <ListBox ItemsSource="{Binding SOMETHING, Mode=TwoWay}"> 
       <ListBox.ItemTemplate> 
       <DataTemplate> 
        <StackPanel> 
        <TextBlock Text="{Binding weight, Mode=OneWay}" />             
        </StackPanel> 
       </DataTemplate> 
       </ListBox.ItemTemplate> 
      </ListBox> 
     </StackPanel> 
    </DataTemplate> 
    </ListView.ItemTemplate> 
</ListView> 

나는 코드에서 외부 목록 상자에 대한 DataContext를 설정?

답변

1

귀하의 Line 모델은이 시나리오에 적합하지 않습니다. 그것은 라인에 속한 관심있는 포인트를 포함하는 Points과 같은 속성을 가져야합니다. 그런 다음 바인딩은 간단하다 :

class Line { 
    public string line_id { get; set; } 
    public string color { get; set; } 
    ObservableCollection<Point> _points; 
    public ObservableCollection<Point> Points { 
    get { 
     if (_points == null) _points = new ObservableCollection<Point>(); 
     return _points; 
    } 
    } 
} 

그런 다음 XAML 코드에서 그냥 같이 PointsPath을 설정할 수 있습니다

<ListBox ItemsSource="{Binding Points, Mode=TwoWay}"> 
    <ListBox.ItemTemplate> 
     <DataTemplate> 
      <StackPanel> 
      <TextBlock Text="{Binding weight, Mode=OneWay}" />     
      </StackPanel> 
     </DataTemplate> 
    </ListBox.ItemTemplate> 
</ListBox> 

위의 모델은 주요 아이디어를 보여주는 단지 예입니다. 물론 전체 구현은 현재 프로젝트에 따라 달라지고 더 발전되어야합니다.

업데이트 : 위 모델을 사용하지 않고 BindingConverter을 사용해보세요. Binding은 현재 항목 (선)에 직접 설정됩니다. ConverterLinepoints에 (아마도 line_id를 기반으로 쿼리 방법)으로 변환됩니다

public class LineToPointsConverter : IValueConverter { 
    public object Convert(object value, Type targetType, object parameter, 
         System.Globalization.CultureInfo culture){ 
     var line = value as Line; 
     //convert to points by querying the points based on line.line_id here 
     return ... 
    } 
    public object ConvertBack(object value, Type targetType, 
          object parameter, 
          System.Globalization.CultureInfo culture){ 
     return Binding.DoNothing; 
    }        
} 

LineToPointsConverter의 정적 속성을 정의하거나 Resources에 그 계산기의 인스턴스를 생성 : 다음

<Window.Resources> 
    <local:LineToPointsConverter x:Key="lineToPointsConverter"/> 
</Window.Resources> 

에서 해당 변환기를 설정 한 XAML 코드 :

<ListBox ItemsSource="{Binding Mode=TwoWay, Path=., 
          Converter={StaticResource lineToPointsConverter}}"> 
    <ListBox.ItemTemplate> 
     <DataTemplate> 
      <StackPanel> 
      <TextBlock Text="{Binding weight, Mode=OneWay}" />     
      </StackPanel> 
     </DataTemplate> 
    </ListBox.ItemTemplate> 
</ListBox> 
+0

그런 직렬화 할 수없는 ORM을 사용합니다. 어쨌든 감사합니다. – demas

+0

@demas이 방법이 가장 쉽고 직관적이며 XAML의 모델을 따르는 유일한 방법이라고 생각합니다. 어쨌든 내부 ListBox 내부의 Binding은 현재 항목 (Line)의 컨텍스트를가집니다. 컨텍스트를 변경하면 바운드 포인트 컬렉션도 변경되어야합니다. 귀하의 코드에서'points' 콜렉션은 여기서 (어디에서 찾을 수 있습니까?) 명확하지 않거나'DataContext'의 멤버입니까? 그렇다면 모든 라인의 혼합 포인트입니까? –

+0

별도의 컬렉션입니다 (각 컬렉션은 DB에서 하나의 테이블에 해당). Line과 Point에서 line_id 속성을 사용하여 점을 라인에 연결합니다. – demas

관련 문제