【发布时间】:2021-12-01 07:36:08
【问题描述】:
我有一个模块化 WPF 应用程序 - Shell、Module1、Module2、Module3。 每个模块都有这样定义的视图:
<UserControl>
<TextBox x:Name="DefaultControl"/>
<UserControl.Tag>
<Binding ElementName="DefaultControl"/>
</UserControl.Tag>
</UserControl>
Shell 有 MainView,它使用 WPFToolkit BusyIndicator 控件。 MainView 是这样定义的:
<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 旨在在 IsBusy 变为 False 时将焦点设置在名为 DefaultControl 的任何视图元素上。 使用 Tag 属性进行绑定时效果很好。 但现在我想为此使用一个名为 DefaultControl (object) 的简单附加属性。 视图变成这样:
<UserControl>
<TextBox x:Name="DefaultControl"/>
<ext:FocusExtension.DefaultControl>
<Binding ElementName="DefaultControl"/>
</ext:FocusExtension.DefaultControl>
</UserControl>
我应该如何更改 BusyIndicator.FocusAfterBusy="{Binding Path=Content.Tag, ElementName=MainRegion}" 中的路径,以绑定到 MainRegion.Content 的附加属性 (DefaultControl)? 附属性:
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);
}
【问题讨论】: