Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Binding as a Resource

Tags:

Can I define a Binding as a Resource and then reuse it with different Controls properties?

Example:

Binding:

<Window.Resources>             <Binding x:Key="MyBinding" Path="MyProperty" Mode="TwoWay" /> </Window.Resources> 

Reuse in XAML:

<TextBox Text="{StaticResource MyBinding}" /> 

After declaring Binding as above I got the error:

"The name 'InitializeComponent' does not exist in the current context"

Is there any way to reuse the same Binding in different contexts?

like image 798
Kylo Ren Avatar asked Apr 28 '16 10:04

Kylo Ren


People also ask

What are binding sources?

The BindingSource component is designed to simplify the process of binding controls to an underlying data source. The BindingSource component acts as both a conduit and a data source for other controls to bind to.

What is binding source in WPF?

A binding source is usually a property on an object so you need to provide both the data source object and the data source property in your binding XAML. In the above example the ElementName attribute signifies that you want data from another element on the page and the Path signifies the appropriate property.

What is binding path in WPF?

Binding path syntax. Use the Path property to specify the source value you want to bind to: In the simplest case, the Path property value is the name of the property of the source object to use for the binding, such as Path=PropertyName . Subproperties of a property can be specified by a similar syntax as in C#.

What is binding in XAML?

Advertisements. Data binding is a mechanism in XAML applications that provides a simple and easy way for Windows Runtime Apps using partial classes to display and interact with data. The management of data is entirely separated from the way the data is displayed in this mechanism.


1 Answers

Direct answer to your question is "yes, you can define a binding as a resource". The problem here is how do you then make any use of it? One possibility is to create an extension class which would pull the binding from the resources and apply it:

public class BindingResourceExtension : StaticResourceExtension {     public BindingResourceExtension() : base() { }      public BindingResourceExtension(object resourceKey) : base(resourceKey) { }      public override object ProvideValue(IServiceProvider serviceProvider)     {         var binding = base.ProvideValue(serviceProvider) as BindingBase;         if (binding != null)             return binding.ProvideValue(serviceProvider);         else             return null; //or throw an exception     } } 

Usage example:

<Window.Resources>     <ResourceDictionary>         <Binding x:Key="MyBinding" Path="MyProperty" Mode="TwoWay" />     </ResourceDictionary> </Window.Resources>  (...)  <TextBox Text="{ns:BindingResource MyBinding}" /> 

Can this solution be used in MultiBinding?

Yes, it can:

<TextBlock>     <TextBlock.Text>         <MultiBinding StringFormat="First: {0}, Second: {1}">             <Binding Path="SomeProperty" />             <ns:BindingResource ResourceKey="MyBinding" />         </MultiBinding>     </TextBlock.Text> </TextBlock> 

There is however one drawback to this - although everything will work in run-time, the XAML Designer will complain that BindingResourceExtension is not of proper type to be put in the MultiBinding.Bindings collection. But, thankfully, there is a quick solution - simply use StaticResourceExtension instead! So this, while being functionally equivalent in run-time, will be accepted by the designer:

<TextBlock>     <TextBlock.Text>         <MultiBinding StringFormat="First: {0}, Second: {1}">             <Binding Path="SomeProperty" />             <StaticResource ResourceKey="MyBinding" />         </MultiBinding>     </TextBlock.Text> </TextBlock> 
like image 112
Grx70 Avatar answered Oct 21 '22 16:10

Grx70