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>
Customers=ObservableCollection (顧客のプロパティ: CustomerId、CustomerName)
ViewModel で SelectedCustomer を null に設定すると、データグリッドが選択解除されます。しかし、ウィンドウが開始された後、データグリッドを選択しないでおく必要があります。ViewModel のコンストラクターで SelectedCustomer を設定しようとしましたが、うまくいきませんでした。コードビハインドでこれを行う場合にのみ機能します:customersDatagrid.SelectedItem = null。
MVVM の方法でこれを行うソリューションはありますか?
よろしくお願いいたします。
ミン