使用ItemTemplate和GridViewColumn的WPF ListView是否可能?

时间:2013-05-31 07:27:39

标签: c# .net wpf listview

我有一个要求,我有一个listview,它通过使用GridViewColumn绑定到一个集合。但我有一个要求,根据列中的某个选定值,我修改列表视图的项目模板。

意味着列表视图只由一行组成,项目模板应该被修改。是否有可能。

<Window x:Class="ListViewSample.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:ListViewSample"
    Title="MainWindow" Height="350" Width="525"
    >
<Window.Resources>
    <local:ListViewConverter x:Key="listViewConverter"/>
    <DataTemplate x:Key="doubleRowListViewItem">
        <Grid>                
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="Auto"/>
            </Grid.RowDefinitions>
            <TextBlock Background="YellowGreen" Grid.Row="0" Grid.Column="0"/>
            <TextBlock Background="GreenYellow" Grid.Row="1" Grid.Column="0" />                
        </Grid>
    </DataTemplate>
</Window.Resources>
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>
    <ListView Grid.Row="1" ItemsSource="{Binding Persons}" >            
        <ListView.View>
            <GridView>                    
                <GridViewColumn Header="Country" DisplayMemberBinding="{Binding Country}"/>
                <GridViewColumn Header="Name" DisplayMemberBinding="{Binding Name}"/>
            </GridView>
        </ListView.View>
    </ListView>
</Grid>
</Window>

此处,一旦国家/地区的值设置为特定条目,项目模板应相应更改。项目模板应设置为上面声明的数据模板。

有没有办法实现这一目标。

感谢。 和Sandeep

1 个答案:

答案 0 :(得分:0)

不确定您要查找的内容,但如果您需要可视化动态行,则可以删除DisplayMemberBinding并使用CellTemplate。您可以通过触发器修改CellTemplate或多个CellTemplate,就像使用任何其他模板一样。