English 中文(简体)
客户控制
原标题:Binding a UserControl to a custom BusyIndicator control

我要求,在装上新观点时,应侧重于具体的文本箱。

解决办法是,在“转轨”活动中加入这一法典,以便:

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

因此,这只是一种观点,而不是另一种观点。 我花了一些时间来解决这个问题,并认识到我正在工作的新观点是,由于布吉指标被设定为真实的,而且在奥洛达德事件之后发生了假造,因此,一个把注意力从所有控制的重点转向所有控制。

因此,解决办法是把侧重点放在<代码>上。 姓名:TextBox after/strong> my BusyIndicator has been set to false. 我的想法是建立一种可再使用的布公指标控制,处理这一额外工作。 然而,我却在多国机器公司中遇到麻烦。

我首先简单地扩大了工具包: 用户指标:

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>

因此,这个想法是<代码>。 在我的《观点》中(我是在I ve set IsBusy后在《观点》中作假的)将重点确定为NameTextBox。 如果操作的话,其他人可以使用<代码>。 强化指标,仅对不同的控制加以约束,并使重点能够适当地放在自己的观点中,假设其观点具有内在的<条码>。

然而,在我提出以下意见时,我就有了这一例外:

www.un.org/Depts/DGACM/index_spanish.htm Set property foo.Controls.EnhancedBusyIndicator.Control Tofocus 当时提出了例外。 [Line:45个职位:26]

Will this solution I am attempting work? If so, what is wrong with what I have thus far (cannot set the ControlToFocusOn property)?


<><>Update 1

I installed Visual Studio 10 Tools for Silverlight 5 and got a better error message when navigating to the new view. Now I gete this error message:

"System.ArgumentException: Object of type System.Windows.Data.Binding cannot be converted to type System.Windows.Controls.UserControl"

此外,我认为我需要改变这种控制的数据内容。 在密码背后的建筑商中,数据内容由我的观点Model确定。 我试图在<代码>上添加数据内容财产。 强化指标,但并不可行:

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

<><>Update 2

我需要修改<代码>UserControl至>Control,因为我想把重点放在TextBox的物体上(实施Control)。 然而,这并不能解决问题。

最佳回答

Without a BusyIndicator present in the view, the common solution to solve the focus problem is to add the code

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

参看<代码>Loaded。 即便有<条码>BusyIndicator,但<条码>BusyIndicator立即将注意力放在银灯控制的其他部分之外。 解决办法是,在<条码>BusyIndicator之后,使用<条码>控制法>。

我得以通过控制解决:

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;
        }
    }
}

用新的<代码>替换<代码>Busy Indicator tags in You xaml 添加适当的名称空间。

Add a new property, ControlToFocusOn inside the element, and bind it to an existing element in the view that you want focus to be on after the EnhancedBusyIndicator disappears:

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

In this case, I focused to a textbox called NameTextBox.

That s it. This control will get focus every time we navigate to the page. While we are on the page, if the EnhancedBusyIndicator becomes busy and not busy agiain, focus will not go to the control; this only happens on initial load.

请允许<代码> 专用于<代码>的强化指标/代码。 控制 另一时间,加上另一财产<代码>,代之以:

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

Allowed Tofocus被正式确定时,下一个时间是EnhancedBusyIndicator。 从公共汽车到非公共汽车,重点将达到<条码>。 控制

在提出观点时,也可将“Focus”定为虚假,以防止重点被控制。 如果你将<条码>Allowed Tofocus约束在一种观点变更的财产上,你可能需要修改<条码>。 否则,该编码为Onetime

问题回答

@Matt, 不详

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

你们是否试图把它当作固定资源来约束银灯5?





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签