WPF DataGrid AutoGenerated列和CellStyle访问DataTable值 [英] WPF DataGrid AutoGenerated columns and CellStyle accessing DataTable value

查看:116
本文介绍了WPF DataGrid AutoGenerated列和CellStyle访问DataTable值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个WPF桌面应用程序,我正在尝试使用 DataGrid ,其中某些列的ToolTip来自存储在ViewModel对象上的属性 DataTable 的每个单元格,即 DataGrid ItemsSource $ C>。我是AutoGenerating列,因为数据中没有固定数量的列。



我之前询问过 RowStyle <的优先级/ code>和 DataGridColumn.CellStyle DataGrid.RowStyle是否覆盖DataGridColumn.CellStyle? [ ^ ])并确定它显示 CellStyle 优先。



我似乎无法弄清楚如何将 Style 的XAML用作 CellStyle 来绑定到单元格的实际源对象(ViewModel)上的属性。

A DataGridCell 似乎没有访问权限的路径源对象。



有没有更好的方法来完成我的尝试?

I'm developing a WPF desktop application and I'm trying to have a DataGrid where certain columns have a ToolTip that is from a property on a ViewModel object stored in each "cell" of the DataTable that is the ItemsSource for the DataGrid. I'm AutoGenerating the columns because there are not a fixed number of columns in the data.

I previously asked about the precedence of RowStyle and DataGridColumn.CellStyle (Does DataGrid.RowStyle override DataGridColumn.CellStyle?[^]) and determined that it appears the CellStyle takes precedence.

I can't seem to figure out how, from the XAML of the Style used as the CellStyle, to bind to a property on the actual source object (ViewModel) for the cell.
A DataGridCell doesn't appear to have a path to access to the source object.

Is there a better way to accomplish what I'm attempting?

推荐答案

据我所知,我遇到了类似的问题所以我希望这对你有很大的帮助



Flex工具提示 [ + ]



此处使用的示例是将数据网格显示为工具提示,因此对于什么可能是过度杀伤你想做,但我相信你能够通过这个代码来解决这个问题,就像我对我的项目所做的那样; - )
I have had a similar problem as far as I understand your question so I hope this will be of tremendous help for you

Flex Tooltip[+]

The example used here is to display a datagrid as a tooltip so might be overkill for what you want to do but I am sure you will be able to figure something out looking through this code as I did with my project ;-)


我最后使用值转换器来做这个获取工具提示:

I ended up doing this using a value converter for getting the tool tip:
using System;
using System.Data;
using System.Globalization;
using System.Windows.Controls;
using System.Windows.Data;

namespace PreQuant
{
  public class CellToolTipConverter : IMultiValueConverter
  {
    #region IMultiValueConverter Members
    public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
    {
      if (values == null)
        throw new ArgumentNullException("values");
      if (values.Length != 2)
        throw new InvalidOperationException("Incorrect number of values passed to CellToolTipConverter");
      DataRowView rowView = values[0] as DataRowView;
      if (rowView == null)
        throw new InvalidOperationException("Incorrect type of first (DataRowView) value passed to CellToolTipConverter");
      DataRow row = rowView.Row;
      if (row == null)
        throw new InvalidOperationException("DataRow must not be null");
      DataGridCell cell = values[1] as DataGridCell;
      if (cell == null)
        throw new InvalidOperationException("Incorrect type of second (DataGridCell) value passed to CellToolTipConverter");
      DataGridColumn column = cell.Column;
      if (column == null)
        throw new InvalidOperationException("DataGridColumn must not be null");
      string toolTip = null;
      CellViewModel vm = row[column.SortMemberPath] as CellViewModel;
      // because I also had renamed the columns from the DataTable column names, I used .SortMemberPath
      // It had to repeat the logic of the RowStyle ToolTip generation since the CellStyle supercedes the RowStyle
      // ...
      // Then it could use the CellViewModel to get the cell's toolTip (if the row hasn't already set one...
      toolTip = vm.Information;
      return toolTip;
    }

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
    {
      throw new NotImplementedException();
    }
    #endregion
  }
}



然后在XAML中获得 DataRowView DataGridCell 值:


And then got the DataRowView and DataGridCell values in the XAML as:

<Style x:Key="CellStyle"
       TargetType="{x:Type DataGridCell}">
  <Setter Property="ToolTip">
    <Setter.Value>
      <MultiBinding Converter="{StaticResource CellToolTip}">
        <Binding />
        <Binding RelativeSource="{RelativeSource Self}" />
      </MultiBinding>
    </Setter.Value>
  </Setter>
</Style>



我分配了 CellStyle 到.xaml.cs代码隐藏中的相应列。


I assigned the CellStyle to the appropriate columns in the .xaml.cs code-behind.


尝试这样的事情:

try something like this:
ToolTip="{Binding Path=Content.yourPropertyPath, RelativeSource={RelativeSource TemplatedParent}}"

这篇关于WPF DataGrid AutoGenerated列和CellStyle访问DataTable值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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