文本框上的多重绑定不起作用

时间:2013-01-31 11:36:44

标签: wpf binding

两个案例都已解决,请查看第一个回答评论以获取信息。

这段代码编译虽然在运行时出错。例外说:

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

当我尝试在MultiBinding中为第二个绑定设置源时,会发生Parse异常。我已经尝试了很多方法并挖掘了大约20篇文章,虽然我在这里找不到什么错误。

我最好的猜测是它以某种方式连接到转换器的错误返回类型。

而且,顺便说一句,当你将TextBox更改为TextBlock时,第一种情况有效。第二种情况仍然不起作用。

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>

代码背后:

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();
    }
}

提前致谢!

CASE2

同一问题的另一个案例:

  

        <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();
      }
  }

}

1 个答案:

答案 0 :(得分:5)

您必须在内部绑定上设置Mode="OneWay"

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

如果你调查了调试器中的XamlParseException,你会发现有一条InnerException有这条消息:

  

双向绑定需要Path或XPath。


现在针对您的第二个问题:当您在Visual Studio中查看输出窗口时,您可能会看到以下消息:

  

System.Windows.Data错误:5:BindingExpression产生的值是   对目标财产无效。值= '20'   MultiBindingExpression:目标元素是'TextBlock'(Name ='');目标   属性是'高度'(类型'双')

我猜这就是全部。

您应该注意传递给targetType方法的Convert参数。在您的情况下,它是System.Double