如何将2个PasswordBox作为CommandParameter传递给Command?

时间:2014-04-19 09:42:55

标签: c# wpf mvvm multibinding passwordbox

在WPF中,PasswordBox.Password不是DP,因此我们无法直接在ViewModel中进行绑定。我已经完成了在线搜索,使用这种方法,但是在加载视图时只调用转换器一次,因此ExecuteMyCommand中的参数为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>

转换器\ 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();
        }
    }
}

视图模型\ 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

为什么转换器不能完美运行?我该怎么办?

2 个答案:

答案 0 :(得分:3)

不是从转换器返回数组,而是将数组转换为新列表并从转换器传递。

<强>转换器

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

命令方法

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

答案 1 :(得分:1)

实际上问题出在您的转换器中。您不需要实现MarkupExtension。只是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();
    }
}

然后将您的转换器声明为XAML中的资源,并将其用作简单的{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>