- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
DataGrid.ItemSource
在代码后面设置为 dataview
。列名和计数需要即时更改,因此对象列表不能很好地工作。
我的表格显示完全正常。这很好,但是我的 GridCellStyle 将 DataRowView 传递给转换器,而不是我期望传递的 DataGridCell。
有没有办法获取 DataGridCell 内容或指示当前传递给我的转换器的列的内容?
<UserControl x:Class="TestUI.SilveusTable"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:TestUI"
mc:Ignorable="d"
>
<UserControl.Resources>
<local:CbotValueConverter x:Key="CbotValueConverter" />
<Style x:Key="GridCellStyle" TargetType="{x:Type DataGridCell}">
<Setter Property="Background" Value="Yellow"/>
<Setter Property="Foreground" Value="{Binding Converter={StaticResource CbotValueConverter}}"/>
</Style>
</UserControl.Resources>
<Grid>
<DataGrid x:Name="DataGrid1" IsReadOnly="True" CellStyle="{StaticResource GridCellStyle}" />
</Grid>
</UserControl>
这是我的转换器类
[ValueConversion(typeof(DataGridCell), typeof(SolidColorBrush))]
public class CbotValueConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
var dgc = (DataGridCell) value;
var rowView = (DataRowView) dgc.DataContext;
var v = rowView.Row.ItemArray[dgc.Column.DisplayIndex];
decimal amount;
if (decimal.TryParse(v.ToString(), out amount))
{
if (amount >= 10) return Brushes.Red;
if (amount >= 5) return Brushes.Blue;
if (amount > 0) return Brushes.Green;
}
return Brushes.Black; //quantity should not be below 0
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
最佳答案
更改绑定(bind)的来源
使用当前绑定(bind)声明,DataGridCell 的 DataContext 是源
Value="{Binding Converter={StaticResource CbotValueConverter}}"
要使用 DataGridCell 本身,请添加 RelativeSource Self
部分(指您要设置绑定(bind)的元素)
Value="{Binding Converter={StaticResource CbotValueConverter},
RelativeSource={RelativeSource Self}}"
关于c# - 转换器获取 DataRowView 而不是 DataGridCell,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40868783/
我有 DataGrid 并且其中一个 DataGrid 列看起来像这样 问题是我被迫两次使用 BooleanToY
我如何知道 DataGridCell 当前是否处于编辑模式(不是 IsSelected),我的意思是,例如,单击 DataGridTextColumn 单元格它变成了 TextBox 而不是 Text
我有一个 DataGrid 并且在点击时我想找到被点击单元格的索引。我找到了获取 DataGridCell 和 DataGridCellInfo 的方法,但其中只有列索引。 如何获取行索引?我找不到获
如果左侧单元格的内容大于其宽度,我如何使用 DataGrid 中右侧的空单元格来显示连续文本? 如果右边单元格有数据,可以使用TextTrimming="CharacterEllipsis" ----
我有一个包含动态数据(自定义数据行的集合)的 DataGrid,我从服务器获取这些数据。 DataRow 有一个索引器和一个属性 Data,它返回绑定(bind)的整个数据行(您将在下面看到) 我以这
这是我之前的问题 you can find it right there 之后的一个问题 所以。现在我定义了一个 DataGrid带有特定的 ElementStyle对于每一列(它只是定义了 Text
我正在尝试检索当前 DataGridCell 对象的行索引。如果它是对角线单元格,我需要它来更改单元格的背景颜色。 我尝试了如下所示的两个选项,这两个选项都为我提供了行索引。但是当我尝试移动滚动条(当
如果单元格中的内容不消失,我似乎无法将工具提示绑定(bind)到 DataGridCell 的内容。换句话说,当我将鼠标悬停在单元格上时,工具提示会按预期出现,但单元格内容不再可见(永久可见,除非我向
我有一个 WPF DataGrid,它显示实现 IDataErrorInfo 的类型。正如预期的那样,当验证失败时,该行会显示红色感叹号,无效单元格会显示红色突出显示。 这一切都很好;但是,我希望验证
我在我的应用程序中使用 MahApps 库,datagrid 单元格的默认样式没有任何边框: 但我希望 DataGrid 看起来像这样: 所以我开始了:
我正在尝试根据其值有条件地控制列中的 DataGrid 单元格背景。不幸的是我得到这样的东西: 这不是很美观,我希望整个单元格都具有不同的颜色,而不仅仅是文本后面的部分。这是代码部分:
我知道我的问题已经在这里以多种方式陈述和回答。但我就是无法让它按照我想要的方式运行。如标题所述,我尝试根据其内容更改 datagridcell 的背景颜色。我对 WPF 比较陌生,但我想解决方案是将转
我有一个带有 4 列的 wpf 数据网格控件。所有这 4 列都绑定(bind)了 4 个 decimal 类型的属性。所以它出现在网格单元格中,如“43.00000”、“456.000000”等。它如
基本上,我绑定(bind)了数据网格,使其类似于科目时间表 - 每行代表一个学期的科目,该学期内的每个单元格代表一个科目。 我现在正在尝试添加拖放功能,以便您可以将其他主题拖到网格上,这将更新底层数据
假设我在 DataGridTemplateColumn 中有一个任意控件,我想知道如何获取该控件,因为我已检索到包含该控件的 DataGridCell。 我的包含 DataGrid 的 XAML 文件
问题是: 当我们要编辑一个DataGridCell时,我们必须首先选择DataGridRow。我的意思是要在 DataGridRow.Current 之外编辑 DataGridCell,我们需要 1.
我对 WPF 中的数据绑定(bind) DataGrid 有疑问。我使用的是 VS 2010 Beta 2,它有自己的 DataGrid,而不是 Toolkit,虽然我认为它几乎相同。 我想绑定(bi
我正在尝试设置 DataGridCells 的 Validation.ErrorTemplate,这是 xaml 代码:
当我们禁用( IsEnabled="False" )DataGridCell 时,它停止引发鼠标双击事件。当单元格被双击时(在禁用和启用状态下),我们仍然希望能够利用双击事件来做一些事情。 我们怎么做
DataGridColumnHeader.BorderThickness=0 对我有用,但不适用于 DataGridRow 或 DataGridCell,有什么想法吗?
我是一名优秀的程序员,十分优秀!