【问题标题】:DataGrid- How to access Row.IsSelected from within a Column (in xaml)DataGrid-如何从列中访问 Row.IsSelected(在 xaml 中)
【发布时间】:2016-12-14 09:00:42
【问题描述】:

我有一个包含几列和几行的 DataGrid。

对于选定的行 - 我想为每列显示一个组合框(绑定到字符串列表)。

对于未选择的行,我想显示一个带有所选字符串的 TextBlock。

我的目标是使用 DataGridColumnTemplate 中的绑定来完成它(也许像这里的样式 How to display combo box as textbox in WPF via a style template trigger?)。我将如何从列的 CellTemplate 中转到“Row.IsSelected”?我想我需要从视觉树上到 Row 吗?

【问题讨论】:

    标签: wpf xaml data-binding wpfdatagrid


    【解决方案1】:

    我想我需要在视觉树上往上走?

    是的,您可以使用 RelativeSource 绑定到 CellTemplate 中父 DataGridRow 的任何属性:

    <TextBlock Text="{Binding Path=IsSelected, RelativeSource={RelativeSource AncestorType=DataGridRow}}" />
    

    所以这样的事情应该可以工作:

    <DataGridTemplateColumn>
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <Grid>
                <ComboBox x:Name="cmb">
                    <ComboBoxItem>1</ComboBoxItem>
                    <ComboBoxItem>2</ComboBoxItem>
                    <ComboBoxItem>3</ComboBoxItem>
                </ComboBox>
                <TextBlock x:Name="txt" Text="..." Visibility="Collapsed" />
            </Grid>
            <DataTemplate.Triggers>
                <DataTrigger Binding="{Binding Path=IsSelected, RelativeSource={RelativeSource AncestorType=DataGridRow}}" Value="True">
                    <Setter TargetName="cmb" Property="Visibility" Value="Collapsed" />
                    <Setter TargetName="txt" Property="Visibility" Value="Visible" />
                </DataTrigger>
            </DataTemplate.Triggers>
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
    </DataGridTemplateColumn>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-08-01
      • 2015-11-19
      • 1970-01-01
      • 1970-01-01
      • 2016-04-18
      • 1970-01-01
      • 2011-07-23
      • 2011-03-03
      相关资源
      最近更新 更多