简体   繁体   中英

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 :].

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?

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 ;)

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 :).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM