我创建了一个窗口,其中包含一个绑定到 ObservableCollection 的 DataGrid:
<GroupBox Header="Kunden" Grid.Column="0">
<DataGrid AutoGenerateColumns="False"
Height="Auto"
HorizontalAlignment="Stretch"
x:Name="customersDataGrid"
VerticalAlignment="Top" Width="Auto"
ItemsSource="{Binding Path=Customers, Mode=TwoWay}"
IsReadOnly="True"
CanUserResizeColumns="False"
ClipboardCopyMode="IncludeHeader"
CanUserAddRows="False"
SelectionMode="Single"
ColumnHeaderStyle="{DynamicResource ResourceKey=DataGridColumnHeaderBold}"
GridLinesVisibility="None"
Background="White"
IsSynchronizedWithCurrentItem="True"
FontFamily="Century Gothic"
SelectedItem="{Binding Path=SelectedCustomer,
Mode=TwoWay,
UpdateSourceTrigger=PropertyChanged}">
<!--Trigger-Verhalten-->
<i:Interaction.Triggers>
<i:EventTrigger EventName="SelectionChanged">
<catel:EventToCommand Command="{Binding CustomerSelectionChangedCmd}"
DisableAssociatedObjectOnCannotExecute="False"
PassEventArgsToCommand="True" />
</i:EventTrigger>
</i:Interaction.Triggers>
<DataGrid.Columns>
<DataGridTextColumn Header="Id"
Binding="{Binding Path=CustomerId}"
FontSize="14" Width="Auto" />
<DataGridTextColumn Header="Name"
Binding="{Binding Path=CustomerName}"
FontSize="14" Width="Auto"/>
</DataGrid.Columns>
</DataGrid>
</GroupBox>
客户=ObservableCollection(客户属性:CustomerId、CustomerName)
当我在 ViewModel 中将 SelectedCustomer 设置为 null 时,将取消选择数据网格。但我需要数据网格在窗口启动后保持未选中状态。我试图在 ViewModel 的构造函数中设置 SelectedCustomer 但它不起作用。仅当我在后面的代码中执行此操作时才有效:customersDatagrid.SelectedItem=null。
是否有任何解决方案以 MVVM 方式执行此操作?
在此先感谢并致以最诚挚的问候,
明