Datagrid:背景颜色+根据单元格值选择禁用 - 使用触发器 [英] Datagrid: background colour + disable of selection depending on the cell value - using trigger
问题描述
在SO上有很多关于WPF datagrids的问题,但是我仍然要问我的原因,我根本无法从那些人那里得到我想要的东西,所以不要和我生气,并且试图帮助我通过回答,请:]。
There is a lot of questions about WPF datagrids on SO, but I still have to ask mine cause I just can't get what I want from those... so don't be mad with me and try to help me by answering pretty please :].
提示:主要问题是:为什么我的触发器不起作用?:|
hint: the main question is: Why my trigger won't work? :|
-
是否有一个datagrid属性禁用选择单元格的值?我想我知道有这样的东西,但我现在找不到。如果没有这样的事情你会怎么解决这个事情?我正在考虑selectCellsChanged或类似的事件。但是我不知道如何处理它。
Is there a datagrid property which disables selecting cell without value? I think I knew there was something like that, but I can't find it now. If there isn't such a thing how would u solve this thing? I was thinking about the event on selectedCellsChanged or something like that. But I'm not sure how to work it out.
如何设置单元格的背景属性? >正在寻找DatagridCell的一些text / content / value属性( http://msdn.microsoft.com/en-us/library/system.windows.controls.datagridcell.aspx )但没有为我工作...
我知道有一些值转换器但是我正在考虑使用触发器来解决这个问题。
How can I set background property of cell depending on the value inside? Was looking for some text/content/value property for DatagridCell(http://msdn.microsoft.com/en-us/library/system.windows.controls.datagridcell.aspx) but nothing worked for me... I know there is some value convertor but I was thinking of solving this using triggers.
有些信息:我已经设置了 SelectionMode =扩展
+ SelectionUnit =Cell
。
Some info: I have set SelectionMode="Extended"
+ SelectionUnit="Cell"
.
背景使用触发器,但它没有工作:
I have tried setting the background using trigger, but it didnt work:
<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 =ContentValue =>
和< Trigger Property =ContentValue = null>
,但这些东西只是不想为我工作。我有什么问题?
The property IsSelected
works ok, but the thing with no content doesn't. Is it just me thinking (wrong) that "" or null
is no content?
Also tried <Trigger Property="Content" Value="">
and <Trigger Property="Content" Value="null">
, but these things just don't want to work for me. What's wrong with me???
I found this Q/A - How do I change the background color of a cell using WPF Toolkit Datagrid so I guess I will work the second Q with that, but still I don't see what's wrong with my trigger...
Also if my trigger worked I could somehow set the cell with HasContent="False"
as not selectable if there is something like that. But I just need to get my trigger work :D
Edit2:当我设置< Trigger Property = HasContentValue =True>
它适用于所有的单元格..所以我猜这是以空/为值。这让我有疑问:
When I set the <Trigger Property="HasContent" Value="True">
it works for all of my cells.. So I guess it takes null/"" as a value. That leaves me to question:
如果我想要特殊的空白背景并禁用他们的选择,应该如何解决?
编辑3:禁用选择应该这样工作:< Setter Property =FocusableValue =false/> / code>感谢 WPF ListView关闭选择 ..哪个不工作:D:'(
Disabling the selection should work like this: <Setter Property="Focusable" Value="false"/>
thanks to WPF ListView turn off selection .. which ain't working :D :'(
现在我只需要找出关于 null
单元格...任何提示?
Now I just need to work out the trigger about null
content of cell... any hints?
推荐答案
这是我如何设法最终解决我的问题,选择空单元
我知道这不是最好的解决方案,但它适用于我:D
感谢这个问题:如何在DataGrid中确定所选单元格的值?(WPF)它帮助:)。
This is how I managed to finally solve my problem with selecting empty cells. I know it ain't the best solution, but it works for me :D Thanks to this Q/A: How Can Determine Selected Cell's Value In DataGrid? (WPF) it helped :).
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
}
}
}
}
有关背景颜色的部分在此解决:如何在自动生成列事件期间设置datagrid单元格的背景?
The part about background colour is solved here: How to set background of a datagrid cell during autogeneratingcolumn event depending on it's value?
如果你有任何投诉/改进我的解决方案只是添加评论:)。
If u have any complaints/improvements to my solution just add comments :).
这篇关于Datagrid:背景颜色+根据单元格值选择禁用 - 使用触发器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!