Wpf 主细节数据绑定

Wpf 主细节数据绑定,wpf,xaml,data-binding,windows-8.1,Wpf,Xaml,Data Binding,Windows 8.1,我的应用程序中有两种型号: 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; } } 我有两个可观察到的集合: lines - ObservableCollection

我的应用程序中有两种型号:

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; }
}
我有两个可观察到的集合:

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

如何设置内部列表框的DataContext以显示每行的点?

您的
模型不适合此场景。它应该有一个名为
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;
    }
  }
}
上面的模型只是一个展示主要思想的例子。当然,根据当前项目的不同,全面实施应该是不同的和更先进的

更新:不使用上述型号,您可以尝试使用
转换器
进行
绑定
绑定
直接设置到当前项目(行)。
转换器
转换为
(可能基于
行id
和您的查询方法):

定义
LineToPointsConverter
的静态属性或在
资源中创建该转换器的实例

<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>  


我使用的ORM无法序列化此类集合属性,但无论如何还是要感谢。@demas我担心这是唯一的方法,最直观且符合XAML的模型。无论如何,内部列表框中的
绑定
具有当前项(行)的上下文。更改上下文还应更改“绑定点”集合。代码中的
集合在此不清楚(它位于何处?),或者它是
数据上下文
的成员?如果是这样,它是所有行的混合点?它是一个单独的集合(每个集合对应于DB中的一个表)。我在line和Point中使用line_id属性将点链接到line。@demas我不熟悉ORM。看起来您可以使用绑定转换器或自定义MarkupExtension从直线获取点。请参阅我的更新以了解其中一种方法(使用转换器)。我不确定这是否有效,但值得一试。
<ListBox ItemsSource="{Binding Points, Mode=TwoWay}">
    <ListBox.ItemTemplate>
       <DataTemplate>
          <StackPanel>
            <TextBlock Text="{Binding weight, Mode=OneWay}" />                  
          </StackPanel>
       </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>
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;
   }                             
}
<Window.Resources>
   <local:LineToPointsConverter x:Key="lineToPointsConverter"/>  
</Window.Resources>
<ListBox ItemsSource="{Binding Mode=TwoWay, Path=.,
                             Converter={StaticResource lineToPointsConverter}}">
    <ListBox.ItemTemplate>
       <DataTemplate>
          <StackPanel>
            <TextBlock Text="{Binding weight, Mode=OneWay}" />                  
          </StackPanel>
       </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>