سؤال

In WPF, PasswordBox.Password is not a DP, so we can't directly bingding it in ViewModel. I have done an online search, use this methods, but the converter just is called one time when the view is loaded, so the parameter in ExecuteMyCommand is null:

MainWindow.xaml

    <DockPanel x:Name="WindowLayout" LastChildFill="False">
        <Grid DockPanel.Dock="Top" Margin="10">
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto" />
                <RowDefinition Height="Auto" />
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto" />
                <ColumnDefinition />
            </Grid.ColumnDefinitions>
            <TextBlock Text="PasswordBox1" VerticalAlignment="Center"/>
            <PasswordBox x:Name="PswBox" Grid.Column="1" Height="27"/>
            <TextBlock Text="PasswordBox2" VerticalAlignment="Center" Grid.Row="1"/>
            <PasswordBox x:Name="AgainPswBox" Grid.Column="1" Grid.Row="1" Height="27"/>
        </Grid>

        <Button IsDefault="True" Command="{Binding MyCommand}" DockPanel.Dock="Top" Content="Click" Width="60" Margin="10">
            <Button.CommandParameter>
                <MultiBinding Converter="{cvt:PlainMultiValueConverter}">
                    <Binding ElementName="PswBox"/>
                    <Binding ElementName="AgainPswBox"/>
                </MultiBinding>
            </Button.CommandParameter>
        </Button>
    </DockPanel>
</Window>

Converters\PlainMultiValueConverter.cs

namespace MVVM_Light_Base.Converters
{
    [MarkupExtensionReturnType(typeof(PlainMultiValueConverter))]
    //[ValueConversion(typeof(object[]), typeof(object))]
    public class PlainMultiValueConverter : MarkupExtension,
        IMultiValueConverter
    {
        public static PlainMultiValueConverter converter = null;

        public override object ProvideValue(IServiceProvider serviceProvider)
        {
            if (null == converter)
            {
                converter = new PlainMultiValueConverter();
            }
            return converter;
        }

        public object Convert(object[] values, Type targetType,
            object parameter, System.Globalization.CultureInfo culture)
        {
            return values;
        }

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

ViewModel\MainViewModel.cs

using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using System.Windows.Controls;
namespace MVVM_Light_Base.ViewModel
{
    public class MainViewModel : ViewModelBase
    {
        private RelayCommand<object> myCommand;

        public RelayCommand<object> MyCommand
        {
            get
            {
                return myCommand ?? (myCommand =
                    new RelayCommand<object>(ExecuteMyCommand));
            }
        }

        private void ExecuteMyCommand(object parameter)
        {
            var pswBoxes = parameter as object[];
            PasswordBox pb;
            string psw0;
            string psw1;

            try
            {
                pb = pswBoxes[0] as PasswordBox;
                psw0 = pb.Password;
                pb = pswBoxes[1] as PasswordBox;
                psw1 = pb.Password;
            }
            catch
            {
                return;
            }
        }
    }
}

enter image description here

Why the converter didn't work perfectly? And how should I do?

هل كانت مفيدة؟

المحلول

Instead of returning array from the converter, convert array into new list and pass from the converter.

Converter

public object Convert(object[] values, Type targetType,
        object parameter, System.Globalization.CultureInfo culture)
{
   return values.ToList();
}

Command Method

private void ExecuteMyCommand(object parameter)
{
   var pswBoxes = parameter as List<object>;
   .....
}

نصائح أخرى

Well actually the problem is in your Converter. You don't need to implement MarkupExtension. Just IMultivalueConverter.

public class PlainMultiValueConverter : IMultiValueConverter
{
    public object Convert(object[] values, Type targetType,
        object parameter, System.Globalization.CultureInfo culture)
    {
        return values;
    }

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

Then declare your converter as a resource in your XAML and use it as a simple {StaticResource MyConverter}.

<DockPanel x:Name="WindowLayout" LastChildFill="False">
    <DockPanel.Resources>
        <local:PlainMultiValueConverter x:Key="PlainMultiValueConverter"/>
    </DockPanel.Resources>
<!--...-->
    <Button IsDefault="True" Command="{Binding MyCommand}" DockPanel.Dock="Top" Content="Click" Width="60" Margin="10">
        <Button.CommandParameter>
            <MultiBinding Converter="{StaticResource PlainMultiValueConverter}">
                <Binding ElementName="PswBox"/>
                <Binding ElementName="AgainPswBox"/>
            </MultiBinding>
        </Button.CommandParameter>
    </Button>
</DockPanel>
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top