J'ai créé une propriété attachée, AttachedBehaviorsManager.Behaviors, qui doit être utilisée comme une classe d'assistance MVVM qui lie les événements aux commandes. La propriété est de type BehaviorCollection (un wrapper pour ObservableCollection). Mon problème est que la commande de liaison pour le comportement finit toujours par être nulle. Lorsqu'il est utilisé sur les boutons, il fonctionne très bien.Éléments de la collection attachée Perdre le contexte de données
Ma question est pourquoi est-ce que je perds mon DataContext sur les éléments à l'intérieur de la collection, et comment puis-je le réparer?
<UserControl x:Class="SimpleMVVM.View.MyControlWithButtons"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:behaviors="clr-namespace:SimpleMVVM.Behaviors"
xmlns:con="clr-namespace:SimpleMVVM.Converters"
Height="300" Width="300">
<StackPanel>
<Button Height="20" Command="{Binding Path=SetTextCommand}" CommandParameter="A" Content="Button A" />
<Button Height="20" Command="{Binding Path=SetTextCommand}" CommandParameter="B" Content="Button B"/>
<TextBox x:Name="tb" Text="{Binding Path=LabelText}">
<behaviors:AttachedBehaviorsManager.Behaviors>
<behaviors:BehaviorCollection>
<behaviors:Behavior Command="{Binding Path=SetTextCommand}" CommandParameter="A" EventName="GotFocus"/>
</behaviors:BehaviorCollection>
</behaviors:AttachedBehaviorsManager.Behaviors>
</TextBox>
</StackPanel>