Search code examples
wpfxmlxamlbindingmultibinding

putting multibinding on a single line in xaml


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?


Solution

  • 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>