WPF binding Ancestor
I have problems with bindings. I want to use a UserControl (Intellibox from codeplex) but I only get error messages in the output window. Basically I have
window grid ... stuff ... usercontrol (self written) ... stuff ... u开发者_Python百科sercontrol (IntelliBox)
In the Output window I get following stuff:
System.Windows.Data Error: 4 : Cannot find source for binding with reference
'RelativeSource FindAncestor, AncestorType='System.Windows.Controls.UserControl',
AncestorLevel='1''. BindingExpression:Path=ShowResults; DataItem=null;
target element is 'Popup' (Name='IntelliboxPopup1');
target property is 'IsOpen' (type 'Boolean')
The binding in the IntelliBox control is defined as follows:
{Binding Path=ShowResults, RelativeSource={RelativeSource FindAncestor,
AncestorType={x:Type UserControl}}}
I guess there is a problem, cause the nesting withing usercontrols - how to I get this error fixed?
Thanks!
I have two questions. First, is this a Binding that you wrote for your use, or is it something that came out-of-the-box inside the UserControl you're using? Second, are you trying to Bind to the Intellibox, or to your "self written" UserControl?
Assuming it's a Binding you wrote for your use (I don't have knowledge of Intellibox so I wouldn't know where to start for fixing it), there are a couple of solutions you might try.
First, when binding to an ancestor, try using the exact ancestor type. For instance, if you're binding to the Intellibox, use AncestorType={x:Type Intellibox}
. Otherwise use AncestorType={x:Type <YourType>}
. Your Binding will be less ambiguous this way.
Second, and perhaps the best answer in this case, is to bind to the control you want by name by setting x:Name="BindSource (or whatever)"
on the target and using the Binding syntax:
{Binding Path=ShowResults,
ElementName=BindSource}
-- HTH, Dusty
精彩评论