Home > Back-end >  How to bind to ContentControl.Content's attached property
How to bind to ContentControl.Content's attached property

Time:10-13

I have a modular WPF app - Shell, Module1, Module2, Module3. Each module has views defined like this:

<UserControl>

    <TextBox x:Name="DefaultControl"/>
    
    <UserControl.Tag>
        <Binding ElementName="DefaultControl"/>
    </UserControl.Tag>
</UserControl>

Shell has the MainView, which uses WPFToolkit BusyIndicator control. MainView is defined like this:

<Window>
    <xctk:BusyIndicator IsBusy="{Binding ElementName=MainRegion, Path=Content.DataContext.IsBusy}"
                        FocusAfterBusy="{Binding Path=Content.Tag, ElementName=MainRegion}">
                        
        <ContentControl x:Name="MainRegion">
        </ContentControl>
        
    </xctk:BusyIndicator>
</Window>

BusyIndicator.FocusAfterBusy is meant to set focus on any view's element named DefaultControl, when IsBusy becomes False. It works fine when using Tag property for binding. But now I want to use a simple attached property called DefaultControl (object) for that. View becomes like:

<UserControl>

    <TextBox x:Name="DefaultControl"/>
    
    <ext:FocusExtension.DefaultControl>
        <Binding ElementName="DefaultControl"/>
    </ext:FocusExtension.DefaultControl>
</UserControl>

How should I change the Path in BusyIndicator.FocusAfterBusy="{Binding Path=Content.Tag, ElementName=MainRegion}", to bind to MainRegion.Content's attached property (DefaultControl)? Attached property:

public static readonly DependencyProperty DefaultControlProperty = DependencyProperty.RegisterAttached(
    "DefaultControl",
    typeof(object),
    typeof(FocusExtension),
    new PropertyMetadata(null));

public static object GetDefaultControl(DependencyObject obj)
{
    return (object)obj.GetValue(DefaultControlProperty);
}

public static void SetDefaultControl(DependencyObject obj, object value)
{
    obj.SetValue(DefaultControlProperty, value);
}

CodePudding user response:

Try this:

FocusAfterBusy="{Binding Path=Content.(ext:FocusExtension.DefaultControl), 
    ElementName=MainRegion}"
  • Related