バックグラウンド:
次の XAML スニペットを検討してください。
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="100"/>
</Grid.RowDefinitions>
<DataGrid
Grid.Row="0"
x:Name="dataGrid"
AutoGenerateColumns="False"
ItemsSource="{Binding RelativeSource={RelativeSource AncestorType={x:Type Window}}, Path=Bobs}"
>
<DataGrid.Columns>
<DataGridTextColumn
Header="Name"
Binding="{Binding Name}"
MinWidth="75"
/>
</DataGrid.Columns>
</DataGrid>
<ListBox
Grid.Row="1"
x:Name="listBox"
DisplayMemberPath="Name"
ItemsSource="{Binding ElementName=dataGrid, Path=SelectedItem.Regions}"
SelectionMode="Multiple"
>
<Interactivity:Interaction.Behaviors>
<ListBoxBehaviors:SynchronizeSelectedItemsBehavior Selections="{Binding ElementName=dataGrid, Path=SelectedItem.SelectedRegions}"/>
</Interactivity:Interaction.Behaviors>
</ListBox>
</Grid>
トグルすると、Grid の IsEnabled プロパティを false に設定する別のコントロールがあるとします。これを行うと、グリッド内の DataGrid コントロールと ListBox コントロールが無効になります。
.NET 4.0 では、DataGrid を無効にすることの残念な副作用は、DataGrid がすべてのセルの選択を解除するため、その SelectedItem が失われることです。これには、ListBox の ItemsSource バインディングを起動するという副作用があり、dataGrid の SelectedItem が null になったため、ItemsSource がクリアされます。
質問:
dataGrid の SelectedItem が null になったときに ListBox の項目コレクションがクリアされないように、バインディングを変更したり、XAML を変更したりするにはどうすればよいですか。
補遺:
- .NET 4.5 で DataGrid の選択解除の動作が削除されたことを認識していますが、現在アップグレードはオプションではありません。
- 選択解除の DataGrid 動作を回避する解決策があることは知っています。それらを使用せずに問題を解決できるかどうかを知りたいです。