用户控件DataGrid
XAML:
<DataGrid x:Name="NamingConventionDataGrid" Width="{Binding ElementName=toolbar, Path=ActualWidth}" MinHeight="100"
HorizontalAlignment="Left" DockPanel.Dock="Bottom"
VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="auto"
CanUserResizeRows="False" CanUserAddRows="False" CanUserSortColumns="True" CanUserDeleteRows="False"
CanUserReorderColumns="False" CanUserResizeColumns="False" Background="White" BorderThickness="1"
VerticalGridLinesBrush="{StaticResource RowGrayBrush}" HorizontalGridLinesBrush="{StaticResource RowGrayBrush}"
RowBackground="White" AlternatingRowBackground="{StaticResource RowGrayBrush}" AlternationCount="2"
ColumnHeaderStyle="{StaticResource DataGridColumnHeaderStyle}"
SelectedItem="{Binding SelectedName, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
AutoGenerateColumns="False" SelectionMode="Single" SelectionUnit="FullRow"
RowHeaderWidth="15" RowHeaderStyle="{StaticResource DataGridRowHeaderStyle}"
ItemsSource="{Binding ReferenceValues, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
MaxHeight="230" ScrollViewer.CanContentScroll="True" ScrollViewer.HorizontalScrollBarVisibility="Auto" >
视图模型代码:
ReferenceValues = tempRefs;
SelectedName = ReferenceValues.FirstOrDefault();
_view.NamingConventionDataGrid.SelectedItem = SelectedName;
_view.NamingConventionDataGrid.UpdateLayout();
_view.NamingConventionDataGrid.ScrollIntoView(SelectedName);
当我ScrollIntoView()
使用SelectedItem
对象 {Binding} 调用时,它会选择对象(我知道这一点是因为 WPF 应用程序上的其他内容发生了变化),但它不会滚动到对象所在的位置,并且SelectedItem
对象不会像当你点击DataGrid
. 我是否在某处遗漏了一些代码,这些代码可以让我滚动到当前位置SelectedItem
并突出显示它,就像用户滚动到该项目并单击该行一样?