【问题标题】:Multibinding Error多重绑定错误
【发布时间】:2013-08-09 12:18:59
【问题描述】:

我之前发布了我的问题: Multi-Forms Binding data

我通过构建转换器解决了这个问题。

XAML:

<Window x:Class="Test_MultiBinding.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="621"
        xmlns:c="clr-namespace:Test_MultiBinding">
    <Window.Resources>
        <c:myConverter x:Key="TestConverter"/>
    </Window.Resources>

    <Grid>
        <TextBox TextWrapping="Wrap" AcceptsReturn="True" Height="269" HorizontalAlignment="Left" Margin="376,22,0,0" Name="textBox1" VerticalAlignment="Top" Width="211" >
            <TextBox.Text>
                <MultiBinding Converter="{StaticResource TestConverter}">
                    <Binding ElementName="textBox2" Path="Text"/>
                    <Binding ElementName="textBox3" Path="Text"/>
                    <Binding ElementName="textBox4" Path="Text"/>
                    <Binding ElementName="textBox5" Path="Text"/>
                    <Binding ElementName="textBox6" Path="Text"/>
                </MultiBinding>
             </TextBox.Text>
        </TextBox>
        <TextBox Height="40" HorizontalAlignment="Left" Margin="130,24,0,0" Name="textBox2" VerticalAlignment="Top" Width="222"/>
        <Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="12,22,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />
        <TextBox Height="40" HorizontalAlignment="Left" Margin="130,70,0,0" Name="textBox3" VerticalAlignment="Top" Width="222" />
        <TextBox Height="40" HorizontalAlignment="Left" Margin="130,116,0,0" Name="textBox4" VerticalAlignment="Top" Width="222" />
        <TextBox Height="40" HorizontalAlignment="Left" Margin="130,162,0,0" Name="textBox5" VerticalAlignment="Top" Width="222" />
        <TextBox Height="91" HorizontalAlignment="Left" Margin="130,208,0,0" Name="textBox6" VerticalAlignment="Top" Width="222" />
    </Grid>
</Window>

主窗口:

namespace Test_MultiBinding
{
    public partial class MainWindow : Window
    {
        ......
    }
    [ValueConversion(typeof(string), typeof(string))]
    public class myConverter : IValueConverter
    {
        public Object Convert(object[] value, Type targettype, object parameter, System.Globalization.CultureInfo cultreinfo)
        {
            return str1 + value[0].ToString() + str2 + value[1].ToString() + str3 + value[2].ToString() + str4 + value[3].ToString() + str5 + value[4].ToString() + str6;
        }

        public Object ConvertBack(object value, Type targettype, object parameter, System.Globalization.CultureInfo cultreinfo)
        {
            throw new NotImplementedException();
        }
    }

}

其中str1,2,3,...是字符串。当我运行它时,我得到了错误:

An object of the type "Test_MultiBinding.myConverter" cannot be applied to a property that expects the type "System.Windows.Data.IMultiValueConverter"

请帮忙!

【问题讨论】:

    标签: c# wpf multibinding


    【解决方案1】:

    对于MultiBinding,您必须实现IMultiValueConverter Interface 而不是IValueConverter

    public class myConverter : IMultiValueConverter
    

    【讨论】:

    • 好的,我编辑了它并再次运行,我得到了这个错误:Test_MultiBinding.myConverter' does not implement interface member 'System.Windows.Data.IMultiValueConverter.ConvertBack(object, System.Type[], object , System.Globalization.CultureInfo)'
    • 如果你要转换回来,你也需要实现 ConvertBack 部分。
    • @user2627651 当然可以。您必须实施成员。 ConvertConvertBack 的签名与 IValueConverter 中的签名不同。请参阅链接中的示例。
    • @user2627651 只是Object[] ConvertBack 而不是Object ConvertBack
    • 好的,谢谢。如果我不需要 ConvertBack?我的意思是我想要 OneWay 绑定。我在 XAML 中尝试过:Mode="OneWay" 但我仍然需要 ConvertBack
    【解决方案2】:
    public class MultiStringConverter : IMultiValueConverter
    {
        public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
        {
            string result = "";
    
            foreach(object value in values)
                result += value.ToString();
    
            return result;
        }
    
        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-20
      • 1970-01-01
      • 1970-01-01
      • 2011-05-02
      • 2018-08-07
      相关资源
      最近更新 更多