访问ListView网格单元格值

时间:2013-07-17 10:15:36

标签: c# wpf listview gridview

我有一个带有gridview的简单列表视图,我正在尝试获取某个列的值。

我的XAML

<ListView SelectionMode="Single" Margin="10,10,0,0" x:Name="activos" Height="211" VerticalAlignment="Top" HorizontalAlignment="Left" Width="555" SizeChanged="activos_SizeChanged" SelectionChanged="activos_SelectionChanged" IsSynchronizedWithCurrentItem="True">
    <ListView.Resources>
        <ResourceDictionary>
            <Style x:Key="hiddenStyle" TargetType="GridViewColumnHeader">
                <Setter Property="Visibility" Value="Collapsed"/>
            </Style>
            <Style TargetType="{x:Type CheckBox}" x:Key="DataGridCheckBox">
                <Setter Property="HorizontalAlignment" Value="Center" /> <Setter Property="HorizontalContentAlignment" Value="Center" /> <Setter Property="IsEnabled" Value="False" /> <Setter Property="Margin" Value="11 3" /> <Setter Property="VerticalAlignment" Value="Center" /> <Setter Property="VerticalContentAlignment" Value="Center" /> <Setter Property="Width" Value="{Binding RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type GridViewColumn}},Path=ActualWidth}" />
            </Style>
        </ResourceDictionary>
    </ListView.Resources>
    <ListView.View>
        <GridView x:Name="act_">
            <GridViewColumn Header="Code" Width="0" DisplayMemberBinding="{Binding coddiag}" HeaderContainerStyle="{StaticResource hiddenStyle}" />
            <GridViewColumn Header="Diagnóstico" Width="400" DisplayMemberBinding="{Binding diag}" />
            <GridViewColumn Header="Princip." Width="50">
                <GridViewColumn.CellTemplate>
                    <DataTemplate>
                        <CheckBox Style="{StaticResource DataGridCheckBox}" IsChecked="{Binding princi}" Focusable="False" IsHitTestVisible="False" Name="Complete" IsThreeState="False" BorderBrush="Black" Foreground="Black"></CheckBox>
                    </DataTemplate>
                </GridViewColumn.CellTemplate>
            </GridViewColumn>
            <GridViewColumn Header="Fecha" Width="{x:Static System:Double.NaN}" DisplayMemberBinding="{Binding fecha, StringFormat='dd/MM/yyyy'}" />
        </GridView>
    </ListView.View>
</ListView>

基本上我要做的是每当我更改所选行并存储其值时,都可以访问GridViewColumn Code值。

我认为它会像Grid [Row,Column]或类似的东西更容易......但经过一些研究后看起来我需要使用其他方法......

1 个答案:

答案 0 :(得分:1)

您可以在视图模型的属性上添加ListView.SelectedItem绑定。您的视图模型将跟踪从该点选择更改。因此,当更改选择的项目时,您将在此属性中获得新值。因此,您可以访问视图模型代码中所选项目的任何属性(列值)。