gpt4 book ai didi

c# - 如何将 2 个密码框作为命令参数传递给命令?

转载 作者:太空狗 更新时间:2023-10-30 01:19:22 24 4
gpt4 key购买 nike

在 WPF 中,PasswordBox.Password 不是 DP,所以我们不能直接在 ViewModel 中绑定(bind)它。我在网上搜索了一下,用的是这个方法,但是converter只是在view加载的时候调用了一次,所以ExecuteMyCommand的参数是null:

主窗口.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();
}
}
}

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

为什么转换器不能正常工作?我该怎么办?

最佳答案

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

转换器

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

关于c# - 如何将 2 个密码框作为命令参数传递给命令?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23168434/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com