i created a window with a DataGrid in it which is bounded to an ObservableCollection:
<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>
Customers=ObservableCollection (Properties of Customer: CustomerId, CustomerName)
When i set SelectedCustomer to null in the ViewModel, the datagrid will be unselected. But i need the datagrid to stay unselected after the window is started. I tried to set the SelectedCustomer in the contructor of the ViewModel but it did not work. It only works if i do this in the code behind: customersDatagrid.SelectedItem=null.
Is there any solution doing this in a MVVM way?
Thanks in advance and best regards,
Minh