分层数据与嵌套WPF列表视图绑定 [英] Hierarchical data binding with nested ListViews in WPF

查看:149
本文介绍了分层数据与嵌套WPF列表视图绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些数据,有一个详细的表格。我希望数据是psented在ListView $ P $。我想,当你在原来的列表中选择一个项目的明细数据显示为一个嵌套的ListView。我似乎无法弄清楚如何获取数据绑定工作。

下面是我到目前为止,(问题是 {绑定路径= FK_History_HistoryItems} ):

 < ListView控件名称=lstHistory的ItemsSource ={绑定源= {的StaticResource历史}}的SelectionChanged =lstHistory_SelectionChanged>
    < ListView.View>
        < GridView控件>
            < GridViewColumn DisplayMemberBinding ={绑定路径=名称}标题=名称WIDTH =100/>
            < GridViewColumn DisplayMemberBinding ={绑定路径=说明}标题=说明WIDTH =150/>
            < GridViewColumn DisplayMemberBinding ={绑定路径=总,转换器= {的StaticResource moneyConvert}}标题=总WIDTH =100/>
            < GridViewColumn DisplayMemberBinding ={结合转换器= {的StaticResource categoryAggregate}}标题=分类WIDTH =100/>
        < / GridView的>
    < /ListView.View>
    < ListView.Resources>
        <风格的TargetType ={X:类型的ListViewItem}>
            < setter属性=模板>
                < Setter.Value>
                    <的ControlTemplate的TargetType ={X:类型的ListViewItem}>
                        < BORDER>
                            < StackPanel的>
                                < BORDER NAME =presenter
                                        背景={TemplateBinding背景}
                                        BorderBrush ={TemplateBinding BorderBrush}
                                        了borderThickness ={TemplateBinding了borderThickness}
                                        填充={TemplateBinding填充}>
                                    < GridViewRow presenter />
                                < /边框>
                                < BORDER名称=细节能见度=坍塌保证金=5
                                        BorderBrush =黑了borderThickness =2>
                                    < StackPanel的保证金=5>
                                        < ListView控件的ItemsSource ={绑定路径= FK_History_HistoryItems}>
                                            < ListView.View>
                                                < GridView控件>
                                                    < GridViewColumn DisplayMemberBinding ={绑定路径= ammount的}标题=ammount的WIDTH =100/>
                                                    < GridViewColumn DisplayMemberBinding ={绑定路径=类别}标题=类别WIDTH =100/>
                                                < / GridView的>
                                            < /ListView.View>
                                        < / ListView控件>
                                    < / StackPanel的>
                                < /边框>
                            < / StackPanel的>
                        < /边框>
                        < ControlTemplate.Triggers>
                            <触发属性=IsSelectedVALUE =真>
                                <二传手的TargetName =细节属性=能见度VALUE =可见/>
                                <二传手的TargetName =presenter属性=背景VALUE =海军/>
                                <二传手的TargetName =presenter属性=TextElement.ForegroundVALUE =白/>
                            < /触发>
                        < /ControlTemplate.Triggers>
                    < /控件模板>
                < /Setter.Value>
            < /二传手>
        < /样式和GT;
    < /ListView.Resources>
< / ListView控件>
 

解决方案

如果我正确地理解你的问题,你需要绑定到原始列表的的SelectedItem:

 < ListView控件的ItemsSource ={结合的ElementName = lstHistory,路径=的SelectedItem}>
 

,然后设置的DataTemplate /视图需要。如果你不希望使用的ElementName的结合,你也可以使用的RelativeSource,但我发现的ElementName更容易阅读和理解。<​​/ P>

I have some data that has a detail table. I want the data to be presented in a ListView. I want the detail data to appear as a nested ListView when you select an item in the original list. I can't seem to figure out how to get the data binding to work.

Here's what I have so far, (the problem is the {Binding Path=FK_History_HistoryItems}):

<ListView Name="lstHistory" ItemsSource="{Binding Source={StaticResource History}}" SelectionChanged="lstHistory_SelectionChanged">
    <ListView.View>
        <GridView>
            <GridViewColumn DisplayMemberBinding="{Binding Path=Name}" Header="Name" Width="100" />
            <GridViewColumn DisplayMemberBinding="{Binding Path=Description}" Header="Description" Width="150" />
            <GridViewColumn DisplayMemberBinding="{Binding Path=Total, Converter={StaticResource moneyConvert}}" Header="Total" Width="100" />
            <GridViewColumn DisplayMemberBinding="{Binding Converter={StaticResource categoryAggregate}}" Header="Categories" Width="100" />
        </GridView>
    </ListView.View>
    <ListView.Resources>
        <Style TargetType="{x:Type ListViewItem}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type ListViewItem}">
                        <Border>
                            <StackPanel>
                                <Border Name="presenter"
                                        Background="{TemplateBinding Background}"
                                        BorderBrush="{TemplateBinding BorderBrush}"
                                        BorderThickness="{TemplateBinding BorderThickness}"
                                        Padding="{TemplateBinding Padding}">
                                    <GridViewRowPresenter />
                                </Border>
                                <Border Name="details" Visibility="Collapsed" Margin="5"
                                        BorderBrush="Black" BorderThickness="2">
                                    <StackPanel Margin="5">
                                        <ListView ItemsSource="{Binding Path=FK_History_HistoryItems}">
                                            <ListView.View>
                                                <GridView>
                                                    <GridViewColumn DisplayMemberBinding="{Binding Path=Ammount}" Header="Ammount" Width="100" />
                                                    <GridViewColumn DisplayMemberBinding="{Binding Path=Category}" Header="Category" Width="100" />
                                                </GridView>
                                            </ListView.View>
                                        </ListView>
                                    </StackPanel>
                                </Border>
                            </StackPanel>
                        </Border>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsSelected" Value="True">
                                <Setter TargetName="details" Property="Visibility" Value="Visible" />
                                <Setter TargetName="presenter" Property="Background" Value="Navy"/>
                                <Setter TargetName="presenter" Property="TextElement.Foreground" Value="White" />
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </ListView.Resources>
</ListView>

解决方案

If I understand your question correctly you need to bind to the SelectedItem of the original list:

<ListView ItemsSource="{Binding ElementName=lstHistory, Path=SelectedItem}">

And then set the datatemplate/view as needed. If you don't want to use ElementName for the binding you could also use RelativeSource but I find ElementName is easier to read and understand.

这篇关于分层数据与嵌套WPF列表视图绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆