Reputation: 4037
Please help me with databound properties check.
I'd like to check was binding used in xaml for certain property or not So I've wrote code like
var uielement = something as FrameworkElement;
if (uielement != null)
{
var sizeBinding = new[] { FrameworkElement.WidthProperty, FrameworkElement.HeightProperty }.Select(_ => uielement.GetBindingExpression(_)).ToList();
if (sizeBinding.Count(_ => _ != null) > 0)
{
Trace.WriteLine(string.Format("found {0} bindings", sizeBinding.Count()));
}
}
I've checked this on xaml element with Height
and Width
properties bound to the data context
<UserControl Width="{Binding Size.Width, Mode=TwoWay}"
Height="{Binding Size.Height, Mode=TwoWay}"
> ... </UserControl>
this should take framework element Width and Height properties and check (using GetBindingExpression
method) that binding was applied on those properties.
Size is a property of data context object that is of type System.Windows.Size.
C# code above works fine for the xaml code. However when binding expression has the simpliest form, like
<UserControl Width="{Binding Size.Width}"
Height="{Binding Size.Height, Mode=TwoWay}"
> ... </UserControl>
then sizeBinding has first element null (for Width) and binding expression instance for Height binding.
I can live with mode=TwoWay set for binding however I'd like to know why binding instance is not created if Mode is not set to the TwoWay. I've thought that this is because expression requires to be more complex then just path. However I've tried Width="{Binding Size.Width, Mode=OneWay}"
but GetBindingExpression
returns null for binding like this.
I've tried BindingOperations
GetBinding* methods but they behave exactly the same as FrameworkElement.GetBindingExpression
.
The only one question I googled is this one. But it is not clear for me
Upvotes: 1
Views: 1097
Reputation: 128061
The reason is most certainly that you explicity set the UserControl's Width
property somewhere, after the Binding was established.
Doing so removes a OneWay Binding (i.e. replaces it by another local value), but keeps a TwoWay Binding in place (and updates its source property).
Upvotes: 4