Xceed Datagrid细胞颜色

时间:2019-01-05 06:41:29

标签: c# wpf xaml xceed xceed-datagrid

我有一个xceed WPF Datagrid,我想以特定方式为每一行中的特定单元格着色。

网格绑定到“出价”对象集合。我要应用于颜色的列是BidValue。

    <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}}"...

为了简化该过程,存在Bid.BackgroundColor和Bid.ForegroundColor的目的是提供用于确定应该显示BidValue的正确颜色的吸气剂。

基本上我想做的事情应该是这样的:

                <xcdg:Column FieldName="BidValue" Title="Bid" CellHorizontalContentAlignment="Center" MaxWidth="75" AllowSort="False">
                    <xcdg:Column.CellContentTemplate>
                        <DataTemplate>
                            <DataTemplate.Triggers>

将其连接到Bid对象中的颜色字段的其余部分非常困难。我尝试使用以下方法在XAML(更常见)中实现着色逻辑:

                          <DataTemplate.Triggers>
                                <DataTrigger Binding="{Binding Path=BidValue}" Value="X" >
                                    <Setter Property="Background" Value="Red"/>
                                    <Setter Property="Foreground" Value="White"/>
                                </DataTrigger>

但是我得到以下信息:

  

错误MC4109:在“ System.Windows.Controls.ContentPresenter”类型上找不到模板属性“ Background”

1 个答案:

答案 0 :(得分:-1)

这段代码实际上是从一列(BidText)获取数据,然后使用它来设置另一列(BidValue)的颜色-这是使用xceed DataGrids本身的专长。

如上所述,必须在列的模板中设置一个控件(在这种情况下为文本块)并将其绑定到已经显示的数据。在Background和Foreground属性分配中显示了用于引用另一个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>
相关问题