双向绑定需要path或者xpath

发布于 2024-12-07 09:55:47 字数 1846 浏览 0 评论 0原文

我想根据两个文本框的文本增加进度栏值。 中执行 MultiBinding 时,出现错误“双向绑定需要路径或 xpath” :

<Window.Resources>
    <local:Class1 x:Key="ConverterM"/>
</Window.Resources>

<TextBox Height="23" HorizontalAlignment="Left" Margin="157,59,0,0"
         Name="textBox1"  VerticalAlignment="Top" Width="120" />
<TextBox Height="23" HorizontalAlignment="Left" Margin="157,108,0,0"
         Name="textBox2" VerticalAlignment="Top" Width="120" />
<ProgressBar Height="24" HorizontalAlignment="Left" Margin="120,160,0,0"
             Name="progressBar1" VerticalAlignment="Top" Width="243" >
    <ProgressBar.Value>
        <MultiBinding Converter="{StaticResource ConverterM}">
            <Binding />
            <Binding ElementName="textBox1" Path="Text" />
            <Binding ElementName="textBox2" Path="Text" />
        </MultiBinding>
    </ProgressBar.Value>
</ProgressBar>

我编写了这个 XAML,但当我在 ProgressBar.Value Value Converter

public class Class1 : IMultiValueConverter
{
    public object Convert(object[] values,
                          Type targetType,
                          object parameter,
                          System.Globalization.CultureInfo culture)
    {
        if (values[1] != null && values[2]!=null)
        {
            if (((string)values[1]).Length==((string)values[2]).Length)
            {
                return 5.0;
            }
        }
        else
        {
            return 0.0;
        }
    }

    public object[] ConvertBack(object value,
                                Type[] targetTypes,
                                object parameter,
                                System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

I want to increase Progress-bar value based on two textbox's Text. I wrote this XAML but there is an error "Two-way binding requires path or xpath" when I do MultiBinding in ProgressBar.Value

<Window.Resources>
    <local:Class1 x:Key="ConverterM"/>
</Window.Resources>

<TextBox Height="23" HorizontalAlignment="Left" Margin="157,59,0,0"
         Name="textBox1"  VerticalAlignment="Top" Width="120" />
<TextBox Height="23" HorizontalAlignment="Left" Margin="157,108,0,0"
         Name="textBox2" VerticalAlignment="Top" Width="120" />
<ProgressBar Height="24" HorizontalAlignment="Left" Margin="120,160,0,0"
             Name="progressBar1" VerticalAlignment="Top" Width="243" >
    <ProgressBar.Value>
        <MultiBinding Converter="{StaticResource ConverterM}">
            <Binding />
            <Binding ElementName="textBox1" Path="Text" />
            <Binding ElementName="textBox2" Path="Text" />
        </MultiBinding>
    </ProgressBar.Value>
</ProgressBar>

Value Converter:

public class Class1 : IMultiValueConverter
{
    public object Convert(object[] values,
                          Type targetType,
                          object parameter,
                          System.Globalization.CultureInfo culture)
    {
        if (values[1] != null && values[2]!=null)
        {
            if (((string)values[1]).Length==((string)values[2]).Length)
            {
                return 5.0;
            }
        }
        else
        {
            return 0.0;
        }
    }

    public object[] ConvertBack(object value,
                                Type[] targetTypes,
                                object parameter,
                                System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

北笙凉宸 2024-12-14 09:55:47

我认为 是没有必要的。尝试删除它并更改转换器中的索引。

I think that <Binding /> is not necessary. try to delete it and change indexes in converter.

中二柚 2024-12-14 09:55:47

双向绑定需要path或者xpath

当您未在 绑定 上设置 Path= 时,就会发生这种情况。默认情况下,WPF 绑定 将采用 defaultPath= 部分。

为了避免这种情况,您需要为在 MultiBinding 中指定的每个 Binding 提供 Path。在您的情况下,有一个空绑定,没有定义路径,这就是您遇到上述错误的原因。

我遇到了同样的问题,但接受的答案没有说明错误是什么,所以考虑分享这个。

Two-way binding requires path or xpath

This happens when you haven’t set the Path= on binding. By default WPF binding will take the Path= part by default.

To avoid this you need to give Path for each Binding you specify in MultiBinding. here in your case there was an empty binding which has no Path defined thats why you have experience with the above error.

I have came across the same issue but the accepted answer does not say what the error is, So thought of sharing this.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文