MVVM Datagrid第一行默认选中但未突出显示 [英] MVVM Datagrid first row selected by default but not highlighted

查看:195
本文介绍了MVVM Datagrid第一行默认选中但未突出显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据网格,它绑定到ViewModel上的集合.当窗口加载时,将填充数据网格并设置SelectedItem. (我知道这一点是因为我有一个绑定到所选项目的详细视图.)但是,该行未突出显示.如果我单击该行,则它将突出显示并正常工作.

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>

推荐答案

您只需要一种样式来告诉您如何使用SelctedItem

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>

,然后将其应用于xaml

<DataGrid RowStyle="{StaticResource SomeStyle}"...

这篇关于MVVM Datagrid第一行默认选中但未突出显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆