sdd
sdd

Reputation: 721

Multibinding on textbox doesn't work

Both cases were solved, look into 1st answer comments for info.

This piece of code compiles though gives an error at runtime. Exception says:

A first chance exception of type 'System.Windows.Markup.XamlParseException' occurred in PresentationFramework.dll.

Parse exception happens when I'm trying to set source for the second binding in MultiBinding. I've tried hell of a lot of ways and digged through ~20 articles, though I can't find out what`s wrong in here.

My best guess is that it`s somehow connected to the wrong return type of a converter.

And, btw, when you change TextBox to TextBlock, 1st case works. The second case doesn`t work still.

CASE1

XAML:

<UserControl x:Class="Draft.MainControl"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
        xmlns:draft="clr-namespace:Draft" 
        xmlns:s="clr-namespace:System;assembly=mscorlib" 
        Height="350" Width="352">

    <UserControl.Resources>

        <s:String x:Key="str1">HELLO</s:String>
        <s:String x:Key="str2">WORLD</s:String>

        <draft:StringConverter x:Key="myStringConverter"/>

     </UserControl.Resources>

    <Grid>

        <TextBox Name="tb1">
            <TextBox.Text>
                <MultiBinding Converter="{StaticResource myStringConverter}">
                    <Binding Source="{StaticResource str1}" />
                    <Binding Source="{StaticResource str2}" />
                </MultiBinding>
            </TextBox.Text>
        </TextBox>


    </Grid>
</UserControl>

Code Behind:

public class StringConverter : IMultiValueConverter
{
    public object Convert( object[] values, Type targetType, object parameter, CultureInfo culture )
    {
        return ( values[0].ToString() + values[1].ToString() );
    }

    public object[] ConvertBack( object values, Type[] targetType, object parameter, CultureInfo culture )
    {
        throw new NotImplementedException();
    }
}

Thanks in advance!

CASE2

And another case for the same problem:

        <Grid>
            <TextBlock TextWrapping="WrapWithOverflow">

                <TextBlock.Resources>
                    <s:Int32 x:Key="defaultHeight">2</s:Int32>
                    <s:Int32 x:Key="defaultNum">10</s:Int32>
                    <draft:MultiplierConverter x:Key="myConverter"/>
                </TextBlock.Resources>

                <TextBlock.Text>
                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa&#10;
                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa&#10;
                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa&#10;
                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa&#10;
                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa&#10;
                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa&#10;
                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa&#10;
                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa&#10;
                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa&#10;
                </TextBlock.Text>

                <TextBlock.Height>
                    <MultiBinding Converter="{StaticResource myConverter}">
                        <Binding Source="{StaticResource defaultNum}" Mode="OneWay" />
                        <Binding Source="{StaticResource defaultHeight}" Mode="OneWay" />
                    </MultiBinding>
                </TextBlock.Height>
            </TextBlock>
        </Grid>
    </UserControl>
Code behind:
  public class MultiplierConverter : IMultiValueConverter
  {
      public object Convert( object[] values, Type targetType, object parameter, CultureInfo culture )
      {
          if ( values.Count() == 2 && values[0] != DependencyProperty.UnsetValue && values[1] != DependencyProperty.UnsetValue )
          {
              var num = (Int32)values[0];
              var height = (Int32)values[1];

              return ( num * height );
          }

          return 0;
      }

      public object[] ConvertBack( object values, Type[] targetType, object parameter, CultureInfo culture )
      {
          throw new NotImplementedException();
      }
  }

}

Upvotes: 1

Views: 4473

Answers (1)

Clemens
Clemens

Reputation: 128136

You have to set Mode="OneWay" on the inner bindings:

<MultiBinding Converter="{StaticResource myStringConverter}">
    <Binding Source="{StaticResource str1}" Mode="OneWay" />
    <Binding Source="{StaticResource str2}" Mode="OneWay" />
</MultiBinding>

If you had investigated the XamlParseException in your debugger, you would have realized that there was an InnerException with this message:

Two-way binding requires Path or XPath.


Now for your second problem: When you look at the Output Window in Visual Studio, you might observe the following message:

System.Windows.Data Error: 5 : Value produced by BindingExpression is not valid for target property.; Value='20' MultiBindingExpression:target element is 'TextBlock' (Name=''); target property is 'Height' (type 'Double')

I guess that says it all.

You should perhaps pay attention to the targetType parameter passed to the Convert method. In your case it is System.Double.

Upvotes: 6

Related Questions