C#XAML主细节绑定不起作用

时间:2017-08-21 12:05:04

标签: c# wpf master-detail

我想在带有主详细信息视图的ListView中显示List。主视图工作正常,但绑定到详细信息视图不起作用。我做错了什么?

代码背后:

DataContext = new VirtualizingCollection<LinesSummary>(fs, 100)

LinesSummary Class:

public class LinesSummary {

     public string dateString { get; set; }
}

XAML:

    <StackPanel>
        <ListView Margin="5" Style="{DynamicResource lvStyle}" Height="200" x:Name="Master"/>
        <ListView Margin="5" Style="{DynamicResource lvStyle_Detail}" Height="200" x:Name="Detail"/>
    </StackPanel>

主视图的动态资源:         

        <Setter Property="VirtualizingStackPanel.IsVirtualizing" Value="True"/>
        <Setter Property="VirtualizingStackPanel.VirtualizationMode" Value="Recycling"/>
        <Setter Property="ScrollViewer.IsDeferredScrollingEnabled" Value="True"/>
        <Setter Property="ListView.ItemsSource" Value="{Binding}"/>
        <Setter Property="ListView.View">
            <Setter.Value>
                <GridView>
                    <GridViewColumn Header="Date" Width="100">
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <TextBlock Text="{Binding dateString}"/>
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>
                </GridView>
            </Setter.Value>
        </Setter>
    </Style>

详细信息视图的动态资源:

    <Style x:Key="lvStyle_Detail" TargetType="{x:Type ListView}">
        <Setter Property="VirtualizingStackPanel.IsVirtualizing" Value="True"/>
        <Setter Property="VirtualizingStackPanel.VirtualizationMode" Value="Recycling"/>
        <Setter Property="ScrollViewer.IsDeferredScrollingEnabled" Value="True"/>
        <Setter Property="ListView.ItemsSource" Value="{Binding ElementName=Master, Path=SelectedItem.LinesSummary}"/>
        <Setter Property="ListView.View">
            <Setter.Value>
                <GridView>
                    <GridViewColumn Header="aaa" Width="100">
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <TextBlock Text="{Binding dateString}"/>
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>
                </GridView>
            </Setter.Value>
        </Setter>
    </Style>

1 个答案:

答案 0 :(得分:1)

ItemsSource的{​​{1}}只能绑定到ListView,所以即使您绑定第二个IEnumerable的{​​{1}}属性,它也是如此不会工作:

ItemsSource

...因为ListView不是ItemsSource="{Binding ElementName=Master, Path=SelectedItem}"

由于LinesSummary中只选择了一个项目,因此您可以将IEnumerable绑定到其Master的{​​{1}}属性:

TextBlock

dateString

中删除此setter
SelectedItem