7

I have a datagrid which is bound to a collection on my ViewModel. When the window loads the datagrid is populated and the SelectedItem is set. (I know this because I have a detail view bound to the selected item.) The row however is not highlighted. If I click on the row then it will be highlighted and works normally.

How do I make the selected row appear highlighted when its the default selection?

<DataGrid IsSynchronizedWithCurrentItem="True" SelectionUnit="FullRow"  RowHeaderWidth="0"  VerticalScrollBarVisibility="Visible" HorizontalScrollBarVisibility="Visible"  IsReadOnly="True" ItemsSource="{Binding Items}"  SelectedItem="{Binding Path=SelectedItem, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" AutoGenerateColumns="False">
    <DataGrid.Columns>                    
        <DataGridTextColumn Header="Run Date" Binding="{Binding Path=RunDate, StringFormat={}{0:MM-dd-yy HH:mm:ss} }"  />
        <DataGridTextColumn Header="Description" Binding="{Binding Description}" />
        <DataGridTextColumn Header="Duration" Binding="{Binding Duration}" />
        <DataGridTextColumn Header="Deviation" Binding="{Binding Deviation}" />
    </DataGrid.Columns>
</DataGrid>
Yael
  • 1,566
  • 3
  • 18
  • 25
jrandomuser
  • 1,510
  • 19
  • 50
  • 3
    I guess you set the selected item correctly, but don't focus the `DataGrid`. As far as I know, the `DataGrid` does not highlight the selected row, if the Grid itself or one of its childs is not focused. – Stefan Over Aug 25 '14 at 13:14
  • Is there an MVVM "compatible" way to say Set focus to datagrid after bind? Although I think I could I do that on the view code behind without it being necessarily against the pattern. – jrandomuser Aug 25 '14 at 13:24
  • 1
    You could take a look at [this answer](http://stackoverflow.com/a/6742741/2132796) about setting the focused element via MVVM. However, setting in your views code-behind would be much easier. Also, if you set the `SelectedItem` property of the `DataGrid`, it should be at least highlighted in light-grey. Once you need to have to highlighted in blue, you should reconsider the way to update the `SelectedItem` property of your viewmodel. You might want to set the `SelectedItem` and the focus to the `UIElement` from the same method. – Stefan Over Aug 25 '14 at 13:38
  • 1
    @jrandomuser I think that set focus in the code-behind is absolutely compatible with the MVVM pattern, because it is something which is related only with view. – Artholl Feb 16 '17 at 10:21
  • @Artholl: Code-behind is compatible to MVVM, but it reduces reusing of functionality, I would implement it as a behavior. – Rekshino Mar 21 '17 at 12:58

2 Answers2

0

You just need a style to tell what to do with the SelctedItem

<Style x:Key="SomeStyle" TargetType="{x:Type DataGridRow}" >
        <Style.Triggers>
            <Trigger Property="DataGridRow.IsSelected" Value="True">
                <Setter Property="Background" Value="Red" />
            </Trigger>
        </Style.Triggers>
    </Style>

and then apply it to your datagrid in xaml

<DataGrid RowStyle="{StaticResource SomeStyle}"...
AZ_
  • 21,688
  • 25
  • 143
  • 191
-1

As mentioned in my comment, there is the possibility to focus the grid on selection changed with a behavior. So you will get, that selection will be highlighted:

using System.Windows.Controls;
using System.Windows.Interactivity;

public class FocusGridOnSelectionChanged : Behavior<DataGrid>
{
    protected override void OnAttached()
    {
        base.OnAttached();

        AssociatedObject.SelectionChanged += AssociatedObject_SelectionChanged;
    }

    private void AssociatedObject_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        AssociatedObject?.Focus();
    }

    protected override void OnDetaching()
    {
        AssociatedObject.SelectionChanged -= AssociatedObject_SelectionChanged;

        base.OnDetaching();
    }

}


xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
<DataGrid ... >

    <i:Interaction.Behaviors>
        <yourbehaviorsns:FocusGridOnSelectionChanged/>
    </i:Interaction.Behaviors>

    ...

</DataGrid>

But I am afraid, it is not a full solution you are after, cause if the grid will lose the focus, the selected items will lose they highlighting.

So if you want, that selection is also be highlighted after grid have lost the focus, you should rewrite the DataGridRow control template, namely visual style 'UnfocusedSelected'.

Rekshino
  • 6,954
  • 2
  • 19
  • 44