1

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? :|

  1. 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.

  2. 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.

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>

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???

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. But I just need to get my trigger work :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. That leaves me to question:

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

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 :'(

Now I just need to work out the trigger about null content of cell... any hints?

Community
  • 1
  • 1
Ms. Nobody
  • 1,219
  • 3
  • 14
  • 34
  • It's a pretty simple code. It should work. Try it in a simple project. I believe there is somthing wrong with your view or grid settings. – Oleg Ignatov May 17 '13 at 08:44
  • @OlegIgnatov I updated the Q.. It's kinda weird that null is actually some content. – Ms. Nobody May 17 '13 at 08:53
  • I don't have a code example for you, but I think this has to be done at the DataGridColumn level, e.g. use a DataGridTemplateColumn and in the DataTemplate specify bindings to value, isenabled and a background which can be shown/hidden if value is null – Dr. Andrew Burnett-Thompson Sep 09 '13 at 16:29

2 Answers2

1

I create a simple DataGrid and try to find out why HasContent return always true. I check Content property and it has TextBlock in it. So probably this is a reason why it's always true.

To handle this issue you can modify your Trigger to use converter:

<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. To know which property converter should check you can use ConverterParameter.

It isn't an elegant solution... but it works ;)

Rafal
  • 1,081
  • 8
  • 10
  • Well thx, I think I get why the HasContent ain't working for me, but still nto sure how to get the convertor to work and I would rather try it some easier way without convertor. – Ms. Nobody May 20 '13 at 08:23
  • Yes same comment - CellConverter needs to check for property X on the DataContext, however if this style is applied to all cells in all columns, you can't know which property to check for null. – Dr. Andrew Burnett-Thompson Sep 09 '13 at 15:41
0

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

The part about background colour is solved here: How to set background of a datagrid cell during AutoGeneratingColumn event depending on its value?

If u have any complaints/improvements to my solution just add comments :).

Community
  • 1
  • 1
Ms. Nobody
  • 1,219
  • 3
  • 14
  • 34
  • 1
    You can make you code more elegant if utilize "as" operator and string.IsNullOrEmpty function. var fc = col.GetCellContent(item.Item) as TextBlock; if (fc != null && string.IsNulOrEmpty(fc.Text)) ... – morincer May 21 '13 at 17:51