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

sdd*_*sdd 1 wpf binding

两个案例都已解决,请查看第一个回答评论的信息.

这段代码编译但在运行时会出错.例外说:

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

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

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

而且,顺便说一句,当你将TextBox更改为TextBlock时,第一种情况就可以了.第二种情况仍然不起作用.

情况1

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>
Run Code Online (Sandbox Code Playgroud)

代码背后:

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();
    }
}
Run Code Online (Sandbox Code Playgroud)

提前致谢!

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>
Run Code Online (Sandbox Code Playgroud)
Code behind:
Run Code Online (Sandbox Code Playgroud)
  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();
      }
  }

}
Run Code Online (Sandbox Code Playgroud)

Cle*_*ens 5

你必须设置Mode="OneWay"内部绑定:

<MultiBinding Converter="{StaticResource myStringConverter}">
    <Binding Source="{StaticResource str1}" Mode="OneWay" />
    <Binding Source="{StaticResource str2}" Mode="OneWay" />
</MultiBinding>
Run Code Online (Sandbox Code Playgroud)

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

双向绑定需要Path或XPath.


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

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

我想这就说明了一切.

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