简体   繁体   English

WPF | 多重绑定不起作用

[英]WPF | Multibinding not working

Im trying to use multibinding as i have two textboxes to validate for Text.Length . 我试图使用多重绑定,因为我有两个文本框来验证Text.Length

Here is my code. 这是我的代码。

<StackPanel HorizontalAlignment="Left" Height="28" Margin="107,238,0,0" VerticalAlignment="Top" Width="104" Orientation="Horizontal" Grid.Column="1">
    <Button x:Name="btnAddBenefeciary" Content="Add Beneficiary" HorizontalAlignment="Left" VerticalAlignment="Top" Width="99" RenderTransformOrigin="0.587,0.65" Height="28" Click="btnAddBenefeciary_Click">
        <Button.IsEnabled>
            <MultiBinding Converter="{StaticResource IsEnabledConverter}">
                <Binding ElementName="tbFatherName" Path="Text.Length"/>
                <Binding ElementName="tbFatherName" Path="Text.Length" />
            </MultiBinding>
        </Button.IsEnabled>
    </Button>
</StackPanel>

In the Same File i added this line under <Window.Resources> 在同一文件中,我在<Window.Resources>下添加了这一行

<local:IsEnabledConverter x:Key="IsEnabledConverter" />

But i am not sure where to put this piece of code so i did put in its own xaml.cs file of which this window belongs. 但是我不确定将这段代码放在哪里,所以我确实将其放入了该窗口所属的xaml.cs文件。

public class IsEnabledConverter : IMultiValueConverter
{
    public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        foreach (var isValid in values)
            if (isValid as bool? == false)
                return false;
        return true;
    }

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

Did i needed to create another file or is it fine to use this piece of code in this same file as no one said anything in description of creating new file.. 我是否需要创建另一个文件,还是可以在同一文件中使用这段代码,因为在创建新文件的描述中没有人说什么。

I tried to follow these guide lines. 我试图遵循这些准则。

https://social.msdn.microsoft.com/Forums/vstudio/en-US/5b9cd042-cacb-4aaa-9e17-2d615c44ee22/can-i-bind-a-controls-isenabled-to-the-selectedindex-of-one-or-more-comboboxes?forum=wpf https://social.msdn.microsoft.com/Forums/vstudio/zh-CN/5b9cd042-cacb-4aaa-9e17-2d615c44ee22/can-i-bind-a-controls-isenabled-to-the-selectedindex-of-一个或多个组合框?forum = wpf

http://developingfor.net/2009/01/21/multibinding-in-wpf/ http://developingfor.net/2009/01/21/multibinding-in-wpf/

Making a Button IsEnabled state depend on two textboxes 使Button IsEnabled状态取决于两个文本框

So if i have to create new file, how will i reference this to the new file.. Sorry again as i am new to WPF and .NET. 因此,如果我必须创建新文件,我将如何引用该文件到新文件。再次抱歉,我是WPF和.NET的新手。

=-=-=-=-=-=-=-=-= =-=-=-=-=-=-=-=-=

Did changes as per described in answer but getting new errors.. 是否按照答案中的描述进行了更改,但收到了新的错误。

在此处输入图片说明

=-=-=-=-=-=-=- =-=-=-=-=-=-=-

Update 2: 更新2:

Tried like this.. 试过这样..

在此处输入图片说明

The converter class can be in any folder/file you want, the namespace of the converter is important. 转换器类可以位于所需的任何文件夹/文件中,转换器的名称空间很重要。 But it's a good habit to put converters - and any classes for that mather - in seperate files called the same as the class their contain. 但是,将转换器-以及该数学器的任何类-放在与它们包含的类相同的单独文件中是一个好习惯。 And putting converter classes in a seperate "Converters" folder. 并将转换器类放在单独的“转换器”文件夹中。 As how to reference it, you need to declare the namespace of the converter in your xaml file where you want to use it: 关于如何引用它,您需要在xaml文件中要使用它的位置声明转换器的名称空间:

<Window 
....
             xmlns:converters="clr-namespace:YourProject.ConvertersNamespace"
....
>

Then you can reference the converter class in the resources: 然后,您可以在资源中引用转换器类:

<converters:IsEnabledConverter x:Key="IsEnabledConverter" />

And then use the converter in you multi binding as you described. 然后按照您所描述的在多绑定中使用转换器。 Altough you have in your multibinding two identical bindings to the tbFatherName element name, but I guess that's just a typo. 在多重绑定中,您到tbFatherName元素名称的绑定完全相同,但是我想那只是一个错字。

Content="Confirm" Grid.Column="1" HorizontalAlignment="Left" Margin="10,160,0,0" Content =“确认” Grid.Column =“ 1” Horizo​​ntalAlignment =“ Left” Margin =“ 10,160,0,0”

Grid.Row="1" VerticalAlignment="Top" Height="27" Width="80"> Grid.Row =“ 1” VerticalAlignment =“ Top”高度=“ 27” Width =“ 80”>

        <Button.CommandParameter>

            <MultiBinding Converter="{StaticResource CustomMultiValueConverterKey}">

                <Binding ElementName="cboCountry" Path="SelectedValue"/>
                    <Binding ElementName="cboShopKeeperID" Path="SelectedValue"/>

            </MultiBinding>

        </Button.CommandParameter>

    </Button>

and the back have to write 后面必须写

CommonControlHandlers.populateControls("CALL GetCountryList()", cboCountry); CommonControlHandlers.populateControls(“ CALL GetCountryList()”,cboCountry);

     CommonControlHandlers.populateControls<ComboBox>("CALL GetShopKeeperList()", cboShopKeeperID);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM