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. 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.
The AccountDropdown.asmx is simple, it contains开发者_C百科 a 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.
When I use this control, I need to bind the SelectedAccount to the property of a ViewModel for another view, e.g.:
<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. I can't even debug it, the exception is thrown from 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. 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. Simply specifying the DataContext to use the DataContext of the control where it was being used (and therefore using the intended ViewModel binding):
<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.
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.
If you need help post the method there.
精彩评论