C# Xceed Datagrid单元格着色
我有一个xceed WPF Datagrid,我希望以特定的方式为每行中的特定单元格着色 网格绑定到Bid对象的集合。我要应用于颜色的列是BidValueC# Xceed Datagrid单元格着色,c#,wpf,xaml,xceed,xceed-datagrid,C#,Wpf,Xaml,Xceed,Xceed Datagrid,我有一个xceed WPF Datagrid,我希望以特定的方式为每行中的特定单元格着色 网格绑定到Bid对象的集合。我要应用于颜色的列是BidValue <xcdg:DataGridCollectionViewSource x:Key="BidViewSource" Source="{Binding Bids}" d:DesignSource="{d:DesignInstance {x:Type mo
<xcdg:DataGridCollectionViewSource x:Key="BidViewSource" Source="{Binding Bids}"
d:DesignSource="{d:DesignInstance {x:Type models:Bid}, CreateList=True}">...
<xcdg:DataGridControl Name="BidGrid" DockPanel.Dock="Bottom" VerticalAlignment="Top" AutoCreateColumns="False"
ReadOnly="True" ItemsSource="{Binding Source={StaticResource BidViewSource}}"...
。。。
这段代码实际上是从一列(BidText)获取数据,以使用设置另一列(BidValue)的颜色——这是它自己使用xceed DataGrids的平均技巧
如上所述,必须在列的模板中设置控件(本例中为textblock),并将其绑定到已显示的数据。用于引用另一个xceed Datagrid列的内容以传递到ColorConverter的XAML显示在后台和前台属性分配中。当Visibility属性设置为False时,该引用列不需要像这里所做的那样可见
<xcdg:Column FieldName="BidText" Visible="False" AllowSort="False"/>
<xcdg:Column FieldName="BidValue" Title="Bid" CellHorizontalContentAlignment="Center" MaxWidth="50" AllowSort="False">
<xcdg:Column.CellContentTemplate>
<DataTemplate>
<TextBlock Name="TextBlock" Width="50" HorizontalAlignment="Stretch" VerticalAlignment="Top" TextAlignment="Center"
Text="{Binding}" FontWeight="Bold"
Foreground="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type xcdg:Cell}}, Path=ParentRow.DataContext.BidText, Converter={StaticResource FGColorConverter}}"
Background="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type xcdg:Cell}}, Path=ParentRow.DataContext.BidText, Converter={StaticResource BGColorConverter}}"/>
</DataTemplate>
</xcdg:Column.CellContentTemplate>
</xcdg:Column>
我没有使用xceed toolkit,但使用数据网格的混合来检查您的风格,这是因为content presenter没有任何属性背景。是的,我正在尝试找到背景属性存在的位置。您的数据模板需要定义为某个特定控件,例如文本块
。此外,您可能希望将BidValue
与某个范围(如BidValue>threshold
)进行比较,而不是与任何特定值进行比较。在这种情况下,您将需要一个转换器
,而不是数据触发器
。当我需要数据网格上的特定单元格时,我将引用什么控件?如果您需要直接引用数据网格中的单元格,那么您已经做错了。相反,您应该操作数据,让绑定和MVVM完成它们的工作。听着,你得到的例外是因为你的数据模板是空的,仅此而已。。。。向其添加控件,以便它可以显示您的出价。完全不需要额外的BidText
列。您可以简单地执行以下操作:Background=“{Binding Converter={StaticResource YourConverter}}”
。实际上不是。BidText对应于从数据库中输出的原始字符串数据,BidValue是友好的输出。我不想写一个转换器,它被传递到已经被i18n’d的数据中。我甚至不希望BidText出现在网格中(因此它的可见性=False),但我没有看到任何其他方法将它传递到转换器中。如果你有更好的办法剥那只猫的皮,我很乐意与你离线交谈。就像我说的,我在这里学习。如果“输出友好”是指“格式化”,那么您应该使用绑定的StringFormat
参数来实现这一目的。同样,您正在创建不必要的冗余。没关系,你正在学习。。。我们不都是吗?但是如果你要发布一个答案,人们会对此发表评论。。。这是我们的责任…:我的意思是我没有。我不想在我的转换器中与N种不同的语言进行比较。我认为这些评论应该是关于原始主题的,因为它们较少涉及信噪比。要想知道我是如何通过谷歌做到这一点已经够难了。
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding Path=BidValue}" Value="X" >
<Setter Property="Background" Value="Red"/>
<Setter Property="Foreground" Value="White"/>
</DataTrigger>
<xcdg:Column FieldName="BidText" Visible="False" AllowSort="False"/>
<xcdg:Column FieldName="BidValue" Title="Bid" CellHorizontalContentAlignment="Center" MaxWidth="50" AllowSort="False">
<xcdg:Column.CellContentTemplate>
<DataTemplate>
<TextBlock Name="TextBlock" Width="50" HorizontalAlignment="Stretch" VerticalAlignment="Top" TextAlignment="Center"
Text="{Binding}" FontWeight="Bold"
Foreground="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type xcdg:Cell}}, Path=ParentRow.DataContext.BidText, Converter={StaticResource FGColorConverter}}"
Background="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type xcdg:Cell}}, Path=ParentRow.DataContext.BidText, Converter={StaticResource BGColorConverter}}"/>
</DataTemplate>
</xcdg:Column.CellContentTemplate>
</xcdg:Column>