【问题标题】:Binding a UserControl to a custom BusyIndicator control将 UserControl 绑定到自定义 BusyIndi​​cator 控件
【发布时间】:2012-02-09 22:56:07
【问题描述】:

我需要在加载新视图时关注特定的文本框。

解决方案是将这行代码添加到视图的 OnLoaded 事件中:

Dispatcher.BeginInvoke(() => { NameTextBox.Focus(); });

所以这适用于一种视图,但不适用于另一种视图。我花了一些时间调试问题,并意识到我正在处理的新视图有一个 BusyIndi​​cator,因为 BusyIndi​​cator 被设置为 true 和 false 是在 OnLoaded 事件之后发生的,所以它会将焦点从所有控件上移开。

所以解决方案是将焦点调用到NameTextBox在我的 BusyIndi​​cator 已设置为 false。我的想法是创建一个可重用的 BusyIndi​​cator 控件来处理这些额外的工作。但是,我在 MVVM 中遇到了麻烦。

我首先对工具包做了一个简单的扩展:BusyIndi​​cator:

public class EnhancedBusyIndicator : BusyIndicator
{
    public UserControl ControlToFocusOn { get; set; }

    private bool _remoteFocusIsEnabled = false;
    public bool RemoteFocusIsEnabled
    {
        get
        {
            return _remoteFocusIsEnabled;
        }
        set
        {
            if (value == true)
                EnableRemoteFocus();
        }
    }

    private void EnableRemoteFocus()
    {
        if (ControlToFocusOn.IsNotNull())
            Dispatcher.BeginInvoke(() => { ControlToFocusOn.Focus(); });
        else
            throw new InvalidOperationException("ControlToFocusOn has not been set.");
    }

我将控件添加到我的 XAML 文件中没有问题:

<my:EnhancedBusyIndicator
    ControlToFocusOn="{Binding ElementName=NameTextBox}"
    RemoteFocusIsEnabled="{Binding IsRemoteFocusEnabled}"
    IsBusy="{Binding IsDetailsBusyIndicatorActive}"
...
>    
...
    <my:myTextBox (this extends TextBox)
        x:Name="NameTextBox"
    ...
    />
...
</my:EnhancedBusyIndicator>

所以这个想法是当我的 ViewModel 中的 IsRemoteFocusEnabled 设置为 true 时(我在 ViewModel 中将 IsBusy 设置为 false 之后执行此操作),焦点将设置为 NameTextBox。如果它有效,其他人可以使用 EnhancedBusyIndicator 并绑定到不同的控件并在他们自己的 ViewModel 中适当地启用焦点,假设他们的视图有一个初始的 BusyIndicator 活动。

但是,加载视图时出现此异常:

设置属性 'foo.Controls.EnhancedBusyIndi​​cator.ControlToFocusOn' 引发异常。 [行:45 位置:26]

我正在尝试的这个解决方案会起作用吗?如果是这样,到目前为止我所拥有的有什么问题(无法设置ControlToFocusOn 属性)?


更新 1

我安装了适用于 Silverlight 5 的 Visual Studio 10 工具,并在导航到新视图时收到了更好的错误消息。现在我收到此错误消息:

“System.ArgumentException:System.Windows.Data.Binding 类型的对象无法转换为 System.Windows.Controls.UserControl 类型”

另外,我认为我需要更改此控件的 DataContext。在代码隐藏构造函数中,DataContext 设置为我的 ViewModel。我尝试将 DataContext 属性添加到 EnhancedBusyIndicator,但这不起作用:

<my:EnhancedBusyIndicator
    DataContext="{Binding RelativeSource={RelativeSource Self}}"
    ControlToFocusOn="{Binding ElementName=NameTextBox}"
    RemoteFocusIsEnabled="{Binding IsRemoteFocusEnabled}"
    IsBusy="{Binding IsDetailsBusyIndicatorActive}"
...
>

更新 2

我需要将UserControl 更改为Control,因为我希望将焦点设置为TextBox 对象(实现Control)。但是,这并不能解决问题。

【问题讨论】:

    标签: c# silverlight-4.0 mvvm busyindicator


    【解决方案1】:

    @Matt,不确定

    DataContext="{Binding RelativeSource={RelativeSource Self}}"
    

    可以在 Silverlight 5 中使用,您是否尝试将其绑定为静态资源?

    【讨论】:

    • 我实际上使用的是 Silverlight 4;通过使用适用于 Silverlight 5 的 Visual Studio 10 工具,我刚刚能够获得我在“更新 1”中发布的更好的错误消息,该工具也恰好适用于 Silverlight 4。我没有尝试将其绑定为静态资源。我试试看。
    【解决方案2】:

    如果视图中没有BusyIndicator,解决焦点问题的常见解决方案是添加代码

    Dispatcher.BeginInvoke(() => { ControlToFocusOn.Focus(); });
    

    到视图的Loaded 事件。即使有BusyIndicator 存在,这实际上也有效;但是,BusyIndicator 会立即将焦点从 Silverlight 控件的其余部分移开。解决方法是在BusyIndicator不忙后调用控件的Focus()方法。

    我可以通过制作这样的控件来解决它:

    public class EnhancedBusyIndicator : BusyIndicator
    {
        public EnhancedBusyIndicator()
        {
            Loaded += new RoutedEventHandler(EnhancedBusyIndicator_Loaded);
        }
    
        void EnhancedBusyIndicator_Loaded(object sender, RoutedEventArgs e)
        {
            AllowedToFocus = true;
        }
    
        private readonly DependencyProperty AllowedToFocusProperty = DependencyProperty.Register("AllowedToFocus", typeof(bool), typeof(EnhancedBusyIndicator), new PropertyMetadata(true));
    
        public bool AllowedToFocus
        {
            get { return (bool)GetValue(AllowedToFocusProperty); }
            set { SetValue(AllowedToFocusProperty, value); }
        }
    
        public readonly DependencyProperty ControlToFocusOnProperty = DependencyProperty.Register("ControlToFocusOn", typeof(Control), typeof(EnhancedBusyIndicator), null);
    
        public Control ControlToFocusOn
        {
            get { return (Control)GetValue(ControlToFocusOnProperty); }
            set { SetValue(ControlToFocusOnProperty, value); }
        }
    
        protected override void OnIsBusyChanged(DependencyPropertyChangedEventArgs e)
        {
            base.OnIsBusyChanged(e);
            if (AllowedToFocus && !IsBusy)
            {
                Dispatcher.BeginInvoke(() => { ControlToFocusOn.Focus(); });
                AllowedToFocus = false;
            }
        }
    }
    

    要使用它,请将 xaml 中的 BusyIndicator 标记替换为新的 EnhancedBusyIndicator 并添加适当的命名空间。

    在元素内添加一个新属性ControlToFocusOn,并将其绑定到视图中您希望在EnhancedBusyIndicator 消失后聚焦的现有元素:

    <my:EnhancedBusyIndicator
        ControlToFocusOn="{Binding ElementName=NameTextBox}"
        ...
    >
        ...
    </my:EnhancedBusyIndicator>
    

    在本例中,我关注的是一个名为 NameTextBox 的文本框。

    就是这样。每次我们导航到页面时,此控件都会获得焦点。当我们在页面上时,如果EnhancedBusyIndicator 变得忙碌而不是再次忙碌,焦点将不会转到控件;这只发生在初始加载时。

    如果您想让EnhancedBusyIndicator 再次聚焦到ControlToFocusOn,请添加另一个属性AllowedToFocus:

    <my:EnhancedBusyIndicator
        ControlToFocusOn="{Binding ElementName=NameTextBox}"
        AllowedToFocus="{Binding IsAllowedToFocus}"
        ...
    >
        ...
    </my:EnhancedBusyIndicator>
    

    当AllowedToFocus设置为true时,下次EnhancedBusyIndicator从忙切换到不忙时,焦点会转到ControlToFocusOn。

    AllowedToFocus 也可以在加载视图时设置为 false,以防止焦点转到控件。如果将 AllowedToFocus 绑定到 ViewModel 属性,则可能需要更改 BindingMode。默认为OneTime。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-16
      • 2013-01-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多