首页 文章

Datagrid:背景颜色禁用选择取决于单元格值 - 使用触发器

提问于
浏览
1

在SO上有很多关于WPF数据网格的问题,但我仍然要问我的原因我无法从那些人那里得到我想要的东西......所以不要生我的气,试着通过回答漂亮来帮助我请 :] .

hint: 主要问题是:为什么我的触发器不起作用? :|

  • Is there a datagrid property which disables selecting cell without value? 我想我知道有类似的东西,但我可以't find it now. If there isn' t这样的事情你怎么解决这个问题?我正在考虑关于selectedCellsChanged或类似事件的事件 . 但我不确定如何解决这个问题 .

  • How can I set background property of cell depending on the value inside? 正在寻找DatagridCell(http://msdn.microsoft.com/en-us/library/system.windows.controls.datagridcell.aspx)的一些text / content / value属性,但没有任何对我有用...我知道有一些值转换器,但我正在考虑使用触发器来解决这个问题 .

一些信息:我已经设置了 SelectionMode="Extended" SelectionUnit="Cell" .

我尝试使用触发器设置背景,但它没有工作:

<DataGrid.CellStyle>
    <Style TargetType="DataGridCell">
        <Style.Triggers>
            <Trigger Property="HasContent"  Value="False">
                <Setter Property="Background" Value="DarkGray"/>
            </Trigger>
            <Trigger Property="IsSelected" Value="True">
                <Setter Property="Background" Value="Red"/>
            </Trigger>
        </Style.Triggers>
    </Style>
</DataGrid.CellStyle>

属性 IsSelected 工作正常,但没有内容的东西没有 . 难道只是在思考(错误)“”或 null 不是内容吗?还试过了 <Trigger Property="Content" Value=""><Trigger Property="Content" Value="null"> ,但是这些东西只是不对我错了???

Edit: 我发现这个Q / A - How do I change the background color of a cell using WPF Toolkit Datagrid所以我想我会用第二个Q工作,但是我的触发器仍然是错误的...而且如果我的触发器工作,我可以用 HasContent="False" 设置单元格为不可选如果有类似的东西 . 但我只需要让我的扳机工作:D

Edit2: 当我设置 <Trigger Property="HasContent" Value="True"> 时,它适用于我的所有单元格..所以我猜它需要null /“”作为值 . 这让我有疑问:

How should I solve this if I want special background for nulls and disable their selection?

Edit3: 禁用选择应如下所示: <Setter Property="Focusable" Value="false"/> 感谢WPF ListView turn off selection ..其中ain 't working :D :'(

现在我只需要解决关于 null 细胞内容的触发器...任何提示?

2 回答

  • 0

    我创建了一个简单的 DataGrid 并试图找出为什么 HasContent 总是返回 true . 我查了 Content 属性,里面有 TextBlock . 所以这可能就是为什么它总是如此 .

    要处理此问题,您可以修改 Trigger 以使用转换器:

    <DataTrigger Binding="{Binding DataContext, RelativeSource={RelativeSource Self}, Converter={StaticResource CellConverter}}"  Value="False" >
         <Setter Property="Background" Value="Green"/>
    </DataTrigger>
    

    并在转换器中检查适当的属性if是否为null . 要知道哪个属性转换器应该检查,您可以使用 ConverterParameter .

    它不是一个优雅的解决方案......但它的工作原理;)

  • 1

    这就是我通过选择空单元格最终解决问题的方法 . 我知道这不是最好的解决方案,但它对我有用:D感谢这个Q / A:How Can Determine Selected Cell's Value In DataGrid? (WPF)它有帮助:) .

    private void mydatagrid_SelectedCellsChanged(object sender, SelectedCellsChangedEventArgs e)
            {
                foreach (var item in e.AddedCells)
                {
                    var col = item.Column as DataGridColumn;
                    var fc = col.GetCellContent(item.Item);
    
                    if (fc is TextBlock)
                    {
                        if (((fc as TextBlock).Text == (""))||((fc as TextBlock).Text == null))
                        {
                            mydatagrid.SelectedCells.Remove(item);
                            fc.Focusable = false; // not sure if neccesarry/working after the previous line
                        }
                    }
                }
            }
    

    关于背景颜色的部分在这里解决:How to set background of a datagrid cell during AutoGeneratingColumn event depending on its value?

    如果您对我的解决方案有任何抱怨/改进,请添加评论:) .

相关问题