Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

putting multibinding on a single line in xaml

Tags:

Is there a way to take this multibinding:

<TextBox.IsEnabled>     <MultiBinding Converter="{StaticResource LogicConverter}">         <Binding ElementName="prog0_used" Path="IsEnabled" />         <Binding ElementName="prog0_used" Path="IsChecked" />     </MultiBinding> </TextBox.IsEnabled> 

and put is all on one line, as in <TextBox IsEnabled="" />?

If so, where can I learn the rules of this formattiong?

like image 392
Adam S Avatar asked Jun 02 '10 17:06

Adam S


People also ask

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 WPF MultiBinding?

Multibinding takes multiple values and combines them into another value. There are two ways to do multibinding, either using StringFormat or by a converter. The StringFormat is simple compared to a converter, so we will start with that first. <TextBlock> <TextBlock.Text>

What is MultiBinding in Xamarin forms?

In this article Multi-bindings provide the ability to attach a collection of Binding objects to a single binding target property. They are created with the MultiBinding class, which evaluates all of its Binding objects, and returns a single value through a IMultiValueConverter instance provided by your application.


2 Answers

A better (and simpler) approach would be to define a style as a resource which you can easily apply to any TextBox:

<Window.Resources>     <c:MyLogicConverter x:Key="LogicConverter" />      <Style TargetType="{x:Type TextBox}" BasedOn="{StaticResource {x:Type TextBox}}" x:Key="MultiBound">         <Setter Property="IsEnabled">             <Setter.Value>                 <MultiBinding Converter="{StaticResource LogicConverter}">                     <Binding ElementName="switch" Path="IsEnabled" />                     <Binding ElementName="switch" Path="IsChecked" />                 </MultiBinding>             </Setter.Value>         </Setter>     </Style> </Window.Resources>  <StackPanel Orientation="Horizontal">     <CheckBox Name="switch" />     <TextBox Name="textBox2" Text="Test" Style="{StaticResource MultiBound}" /> </StackPanel> 
like image 60
hemp Avatar answered Oct 15 '22 17:10

hemp


This can be done with a custom markup extension:

public class MultiBinding : System.Windows.Data.MultiBinding {     public MultiBinding (BindingBase b1, BindingBase b2)     {         Bindings.Add(b1);         Bindings.Add(b2);     }      public MultiBinding (BindingBase b1, BindingBase b2, BindingBase b3)     {         Bindings.Add(b1);         Bindings.Add(b2);         Bindings.Add(b3);     }      // Add more constructors if you need. } 

Usage:

<TextBox IsEnabled="{local:MultiBinding     {Binding IsEnabled, ElementName=prog0_used},     {Binding IsChecked, ElementName=prog0_used},     Converter={StaticResource LogicConverter}}"> 
like image 42
Athari Avatar answered Oct 15 '22 16:10

Athari