I need to know when a parent in the VisualTree or LogicalTree changes of a Control. I need this feature, since anytime a parent changes I need to reevaluate the controls' window class, so that I can attached Command- and InputBindings.
I have a dirty way, which means I need to attach to each parent element and check for parent changes with events, but I was hoping there is another solution.
Example:
I have a UserControl
<UserControl x:Class="WpfApplication21.UserControl1">
<UserControl.CommandBindings>
<CommandBinding Command="ApplicationCommands.Cut" Executed="SomeHandler"></CommandBinding>
</UserControl.CommandBindings>
</UserControl>
and I have a Window that contains one or many UserControls
<Window x:Class="WpfApplication21.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApplication21"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Window.CommandBindings>
<CommandBinding Command="ApplicationCommands.New" Executed="SomeHandler"></CommandBinding>
</Window.CommandBindings>
<StackPanel>
<local:UserControl1></local:UserControl1>
<local:UserControl1></local:UserControl1>
</StackPanel>
</Window>
Although in the example I have put the same command, it's only here to show in the example. In reality each control has different commands. The commands of each UserControl need to be merged into the CommandBindings of the Window (the same is true for InputBindings - not shown here). Each UserControl is a plugin that is created from a ViewModel dynamically, so each ViewModel has a different View (the example only shows UserControls, but in reality these a derivation from a UserControl).
For that reason I created a behavior that attaches to a CommandProvider that is implemented by the ViewModel (created by me) so that it's done without code-behind.
Since I have many ViewModels and therefore also Views I need to manage the Command-/InputBindings and attach them to the Window. One problem is that not every View can get a Focus, and somehow for UserControl event if they are focused the Command-/InputBindings don't work.
Is it a little clearer now? I now the situation is a bit complex.