简体   繁体   English

绑定到自定义控件属性

[英]Binding to Custom Control Property

I am developing an Account List dropdown control which is to be used across my application. 我正在开发一个帐户列表下拉控件,该控件将在我的应用程序中使用。 The dropdown will make a call to a service and retrieve the list of available accounts, and will expose a property "SelectedAccount" for the selected item in the dropdown. 下拉菜单将调用服务并检索可用帐户列表,并将在下拉菜单中显示所选项目的属性“ SelectedAccount”。 The SelectedAccount is going to be a DependencyProperty as it has to be bound by the consumers of the AccountDropdown control, and it has to be two-way bindable so it reflects an existing SelectedAccount. SelectedAccount将成为DependencyProperty,因为它必须由AccountDropdown控件的使用者绑定,并且必须是双向绑定的,因此它反映了现有的SelectedAccount。

The AccountDropdown.asmx is simple, it contains a ComboBox: AccountDropdown.asmx很简单,它包含一个ComboBox:

<ComboBox SelectedItem="{Binding Path=SelectedAccount, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"...

The selected item of the combo box is bound to a property which is registered as a 组合框的所选项目绑定到一个属性,该属性注册为

DependencyProperty.Register("SelectedAccount", typeof(IAccount), typeof(AccountDropdown),
                                new UIPropertyMetadata(null));

... and the usual property: ...以及通常的属性:

    #region SelectedAccount

    /// <summary>
    /// Selected account
    /// </summary>
    public IAccount SelectedAccount
    {
        get { return (IAccount)GetValue(SelectedAccountProperty); }
        set { SetValue(SelectedAccountProperty, value); }
    }
    #endregion SelectedAccount

The properties are defined in the code-behind file... and the data context is set to "this"... so the binding is set correctly. 属性在代码隐藏文件中定义...,数据上下文设置为“ this” ...,因此绑定设置正确。

When I use this control, I need to bind the SelectedAccount to the property of a ViewModel for another view, eg: 使用此控件时,需要将SelectedAccount绑定到另一个视图的ViewModel属性,例如:

<Controls:AccountDropdown SelectedAccount="{Binding Path=SelectedAccount, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" />

When I run the above code, and I change the selection on the dropdown, I get a System.StackOverflow exception. 当我运行上面的代码并更改下拉列表中的选择时,我收到了System.StackOverflow异常。 I can't even debug it, the exception is thrown from Windows.Base.dll. 我什至无法调试它,该异常是从Windows.Base.dll引发的。

I've been struggling with this one for a day now... any help would be appreciated. 我已经为此苦苦挣扎了一天……任何帮助将不胜感激。

**Note: **I have written several WPF Controls with Dependency Properties, and they work fine, but when I use them, I'm providing the value explicitly in the .asmx file. **注意:**我已经编写了几个具有依赖项属性的WPF控件,它们可以正常工作,但是当我使用它们时,我在.asmx文件中显式提供了该值。 In this case I'm binding again when I use the property. 在这种情况下,当我使用该属性时,我将再次绑定。 I am guessing that exposing a bindable property may require some additional trick. 我猜想暴露一个可绑定属性可能需要一些其他技巧。

Ok, as is often the case, the solution was simple. 好的,通常情况下,解决方案很简单。 The DataContext of the AccountDropdown control was being implicitly set to itself, hence the infinite loop. AccountDropdown控件的DataContext被隐式设置为其自身,因此发生了无限循环。 Simply specifying the DataContext to use the DataContext of the control where it was being used (and therefore using the intended ViewModel binding): 只需指定DataContext即可在使用控件的DataContext处使用它(并因此使用预期的ViewModel绑定):

<Controls:AccountDropdown DataContext="{Binding}" SelectedAccount="{Binding Path=SelectedAccount, Mode=TwoWay}" />

I still have to figure out how to tell the control to use the data context of the control that is housing it... if I do figure it out, I will post it here... or if any of you know, please shed some light. 我仍然必须弄清楚如何告诉控件使用包含它的控件的数据上下文...如果我确定了它,我将其张贴在这里...或者如果您知道任何人,请放弃一点光。

Thanks again to those that responded. 再次感谢那些回应。

I guess the SelectedAccountChanged method updates the field and thereby causes infinite recursion which results in a stack overflow. 我猜想SelectedAccountChanged方法会更新该字段,从而导致无限递归,从而导致堆栈溢出。

To test: remove the SelectedAccountChanged from the DependencyProperty.Register call to make sure that this is the sources of the error and then examine the method itself. 要测试:从DependencyProperty.Register调用中删除SelectedAccountChanged,以确保这是错误的根源,然后检查方法本身。

If you need help post the method there. 如果您需要帮助,请在此处发布方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM