-1

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

网格绑定到 Bid 对象的集合。我要应用于颜色的列是 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 应该显示的正确颜色的 getter。

基本上我想要做的应该是这样开始的:

                <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”上找不到模板属性“背景”

4

1 回答 1

-1

此代码实际上从一列 (BidText) 获取数据以使用设置另一 (BidValue) 列的颜色 - 这是它自己使用 xceed DataGrids 的平均壮举。

如上所述,必须在列的模板中设置一个控件(在本例中为文本块)并绑定到已经显示的数据。用于引用另一个 xceed Datagrid 列的内容以传递到 ColorConverter 的 XAML 显示在 Background 和 Foreground 属性分配中。该参考列不需要像此处那样可见,并将 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>
于 2019-01-18T08:34:15.533 回答