简体   繁体   中英

WPF | Multibinding not working

Im trying to use multibinding as i have two textboxes to validate for 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>

<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.

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

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

Making a Button IsEnabled state depend on two textboxes

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.

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

Did changes as per described in answer but getting new errors..

在此处输入图片说明

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

Update 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:

<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.

Content="Confirm" Grid.Column="1" HorizontalAlignment="Left" Margin="10,160,0,0"

Grid.Row="1" VerticalAlignment="Top" Height="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<ComboBox>("CALL GetShopKeeperList()", cboShopKeeperID);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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