[英]Datagrid: background colour + disable of selection depending on the cell value - using trigger
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 :]. 关于SOPF上的WPF数据网格有很多问题,但是我仍然要问我的原因,因为我无法从这些数据中得到想要的东西...所以请不要生我的气,尝试通过回答漂亮的问题来帮助我请 :]。
hint: the main question is: Why my trigger won't work? 提示:主要问题是: 为什么我的触发器不起作用? :|
:|
Is there a datagrid property which disables selecting cell without value? 是否有一个datagrid属性可以禁止选择没有值的单元格? 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.
我在考虑selectedCellsChanged或类似事件。 But I'm not sure how to work it out.
但是我不确定如何解决。
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.
正在寻找DatagridCell的一些text / content / value属性( http://msdn.microsoft.com/en-us/library/system.windows.controls.datagridcell.aspx ),但对我没有任何帮助...我知道是一些值转换器,但我当时正在考虑使用触发器解决此问题。
Some info: I have set SelectionMode="Extended"
+ SelectionUnit="Cell"
. 一些信息:我已经设置了
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>
The property IsSelected
works ok, but the thing with no content doesn't. 属性
IsSelected
可以正常工作,但是没有内容的东西就不能工作。 Is it just me thinking (wrong) that "" or null
is no content? 只是我在想(错误)“”或
null
不存在吗? Also tried <Trigger Property="Content" Value="">
and <Trigger Property="Content" Value="null">
, but these things just don't want to work for me. 还尝试了
<Trigger Property="Content" Value="">
和<Trigger Property="Content" Value="null">
,但是这些东西都不希望对我有用。 What's wrong with me??? 我怎么了?
Edit: 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. 编辑:我发现了这个问题- 我如何使用WPF Toolkit Datagrid更改单元格的背景颜色,所以我想我将使用它来处理第二个问题,但是我仍然看不到触发器有什么问题...另外,如果我的触发器有效,我可以通过某种方式将
HasContent="False"
为不可选择,如果有这样的话。 But I just need to get my trigger work :D 但是我只需要进行触发工作即可:D
Edit2: 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. Edit2:当我设置
<Trigger Property="HasContent" Value="True">
它适用于我的所有单元格。所以我猜想它以null /“”作为值。 That leaves me to question: 这让我提出了疑问:
How should I solve this if I want special background for nulls and disable their selection? 如果我想为null提供特殊背景并禁用它们的选择,应该如何解决?
Edit3: Disabling the selection should work like this: <Setter Property="Focusable" Value="false"/>
thanks to WPF ListView turn off selection .. which ain't working :D :'( Edit3:禁用选择应该是这样的:
<Setter Property="Focusable" Value="false"/>
由于WPF ListView关闭了选择 ..,所以无效:D:'(
Now I just need to work out the trigger about null
content of cell... any hints? 现在,我只需要找出有关单元格
null
内容的触发器...有什么提示吗?
I create a simple DataGrid
and try to find out why HasContent
return always true
. 我创建一个简单的
DataGrid
并尝试找出HasContent
为什么HasContent
返回true
。 I check Content
property and it has TextBlock
in it. 我检查
Content
属性,它具有TextBlock
。 So probably this is a reason why it's always true. 因此,这可能就是为什么它总是正确的原因。
To handle this issue you can modify your Trigger
to use converter: 要解决此问题,您可以修改
Trigger
以使用转换器:
<DataTrigger Binding="{Binding DataContext, RelativeSource={RelativeSource Self}, Converter={StaticResource CellConverter}}" Value="False" >
<Setter Property="Background" Value="Green"/>
</DataTrigger>
And in converter check appropriate property if is null. 并在转换器中检查适当的属性是否为null。 To know which property converter should check you can use
ConverterParameter
. 要知道应该检查哪个属性转换器,可以使用
ConverterParameter
。
It isn't an elegant solution... but it works ;) 这不是一个优雅的解决方案...但是它可以工作;)
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?
我知道这不是最好的解决方案,但是它对我有用:D感谢此问题: 如何确定DataGrid中所选单元格的值? (WPF) it helped :).
(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
}
}
}
}
The part about background colour is solved here: How to set background of a datagrid cell during AutoGeneratingColumn event depending on its value? 有关背景颜色的部分在这里解决: 如何在AutoGeneratingColumn事件期间根据其值设置数据网格单元的背景?
If u have any complaints/improvements to my solution just add comments :). 如果您对我的解决方案有任何投诉/改进,请添加评论:)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.