Storing ValueConverter Variable

I have a ValueConverter used to bind the "To" value in a StoryBoard animation, similar to answer - WPF animation: Bind to the "To" attribute of the storyboard animation .

The problem is that I am repeating the code snippet below for MultiBinding ValueConverter in several places.

  <MultiBinding Converter="{StaticResource multiplyConverter}"> <Binding Path="ActualHeight" ElementName="ExpanderContent"/> <Binding Path="Tag" RelativeSource="{RelativeSource Self}" /> </MultiBinding> 

I want to remove this duplicate code by storing the result of the ValueConverter in a resource variable so that I can bind this local variable directly to the message board.

 <system:Double x:Key="CalculatedWidth"> <MultiBinding Converter="{StaticResource multiplyConverter}"> <Binding Path="ActualHeight" ElementName="ExpanderContent"/> <Binding Path="Tag" RelativeSource="{RelativeSource Self}" /> </MultiBinding> </system:Double > 

I get the following error:

The "Double" type does not support direct content.

Cannot add content to a Double object.

I believe this is a common problem, but I am not able to find a solution to remove this redundancy.

Update

Thanks Rohit , your answer solved the problem. But I have one more related problem, so I am updating the question. This CalculatedWidth variable works fine in the normal case, but when it is used in RenderTransform, it does not get the value. those. if I use the usual way of using the converter, it works, but it does not pick up the variable.

 <StackPanel.RenderTransform> <TranslateTransform x:Name="SliderTransform"> <TranslateTransform.X> <Binding Converter="{StaticResource PanelConverter}" ElementName="SliderPanel" Path="ActualWidth" /> // Works <Binding Path="Width" Source="{StaticResource CalculatedWidth}"/> // Doesn't Work </TranslateTransform.X> </TranslateTransform> </StackPanel.RenderTransform> 

I saved the variable as part of a local resource. Does this mean that the variable is not created during the Render conversion?

+6
source share
2 answers

As you might expect, you cannot communicate with Double. Binding can only be done using dependency properties.

Instead, use the FrameworkElement resource and bind its width (DP) as follows:

 <FrameworkElement x:Key="CalculatedWidth"> <FrameworkElement.Width> <MultiBinding Converter="{StaticResource multiplyConverter}"> <Binding Path="ActualHeight" ElementName="ExpanderContent"/> <Binding Path="Tag" RelativeSource="{RelativeSource Self}" /> </MultiBinding> </FrameworkElement.Width> </FrameworkElement> 

and you can contact this resource, as in this example:

 <TextBlock Width="{Binding Width, Source={StaticResource CalculatedWidth}}"/> 
+3
source

A System.Double does not implement INotifyPropertyChange (and does not display the Value property for notification), and does not implement advanced dynamic property binding mechanisms. Therefore, he cannot notify of changes.

The problem with local resources is their instanciation: they have no visibility to place pointers, because they are outside of it. Therefore, it is not attached to anything, and the binding returns DependancyProperty.UnsetValue.

Regarding the FrameworkElement resource itself, it returns the value of the Tag: null property.

If you are using VS2013 with .NET 4.5 (it may also work with VS2012 / .NET 4.0), see the output window for data binding tracing:

System.Windows.Data Warning: 4: Cannot find the source for the binding with the link "ElementName = ExpanderContent". BindingExpression: Path = ActualHeight; DataItem = NULL; target element - "FrameworkElement" (Name = ''); target is "Width" (type "Double")

Various solutions are offered to you: you can move the FrameworkElement element beyond local resources (remember that you probably need to add HorizontalAlign = "Left" to allow width changes. Another solution is to add the dependency property to the code behind. Finally, you want to split the result of converting a factor between multiple controls (or properties) The easiest way is to associate it with the first property of the first control and associate the properties of other controls with it.

+3
source

All Articles