简体   繁体   English

MVVM WPF多重绑定不适用于命令参数

[英]MVVM WPF Multibinding is not working for command parameters

I cannot make multibinding working for two passwordbox. 我不能使多重绑定适用于两个密码箱。 I did read a bunch of articles on the net tried working examples, but none of them was the same scenario that I've tried. 我确实在网上阅读了很多尝试过的工作示例文章,但是没有一个是我尝试过的相同场景。 The problem is when I hit the login button then these password fields are not transfered to the command Execute method. 问题是当我按下登录按钮时,这些密码字段未传输到命令Execute方法。

XAML for the converter: 转换器的XAML:

<Grid.Resources>
    <converter:PasswordConverter x:Key="passwordConverter"/>
</Grid.Resources>

XAML for the button looks like this: 该按钮的XAML如下所示:

<Button x:Name="loginButton" 
                Content="Belépés" 
                Margin="494,430,0,0" 
                VerticalAlignment="Top" 
                FontSize="20" 
                RenderTransformOrigin="-2.624,8.99" 
                HorizontalAlignment="Left" 
                Width="172"
                Command="{Binding NavCommand}">
            <Button.CommandParameter>
                <MultiBinding Converter="{StaticResource passwordConverter}" Mode="TwoWay">
                    <Binding Path="Password" ElementName="userIDPasswordBox"/>
                    <Binding Path="Password" ElementName="leaderIDPasswordBox"/>
                </MultiBinding>
            </Button.CommandParameter>
        </Button>

Password converter code: 密码转换器代码:

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

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

Relay command: 中继命令:

public class RelayCommand : ICommand
{

    Action _TargetExecuteMethod;
    Func<bool> _TargetCanExecuteMethod;

    public RelayCommand(Action executeMethod)
    {
        _TargetExecuteMethod = executeMethod;
    }

    public RelayCommand(Action executeMethod, Func<bool> canExecuteMethod)
    {
        _TargetExecuteMethod = executeMethod;
        _TargetCanExecuteMethod = canExecuteMethod;
    }

    public void RaiseCanExecuteChanged()
    {
        CanExecuteChanged(this, EventArgs.Empty);
    }

    #region ICommand Members

    bool ICommand.CanExecute(object parameter)
    {

        if (_TargetCanExecuteMethod != null)
        {
            return _TargetCanExecuteMethod();
        }

        if (_TargetExecuteMethod != null)
        {
            return true;
        }

        return false;
    }

    public event EventHandler CanExecuteChanged = delegate { };

    void ICommand.Execute(object parameter)
    {
        if (_TargetExecuteMethod != null)
        {
            _TargetExecuteMethod();
        }
    }

    #endregion
}

and last huge piece of code for the view model: 最后是视图模型的大量代码:

public class LogonViewModel : BaseViewModel
{

private Action _loginActionComplete;
public LogonViewModel(Action loginActionComplete)
{
    _measureTimer = new Timer();
    _measureTimer.Interval = 500D;
    _measureTimer.Elapsed += measureTimer_Elapsed;
    _measureTimer.Start();
    _loginActionComplete = loginActionComplete;
    NavCommand = new RelayCommand(loginActionComplete);
    SerialPort = new SerailCommunicationNameSpace.SerialCommunication("COM3");
}

~LogonViewModel()
{
    SerialPort.Close();
}

public RelayCommand NavCommand { get; private set; }

private double _measuredWeight;
public double MeasuredWeight {
    get
    {
        return _measuredWeight;
    }
    set
    {
        SetProperty(ref _measuredWeight, value);
    }
}
private Timer _measureTimer;
public SerailCommunicationNameSpace.SerialCommunication SerialPort { get; set; }

private void measureTimer_Elapsed(object sender, ElapsedEventArgs e)
{
    var measuredWeight = 0D;
    if (string.IsNullOrWhiteSpace(SerialPort.DataReceived) == false) {
        var dataReceivedStartTrim = SerialPort.DataReceived.TrimStart();
        var dataReceivedNumbersOnly = dataReceivedStartTrim.Substring(0, dataReceivedStartTrim.IndexOf(' '));
        var enUSCultureInfo = new CultureInfo("en-US");
        measuredWeight = double.Parse(dataReceivedNumbersOnly, enUSCultureInfo);
    }
    SetProperty(ref _measuredWeight, measuredWeight);
    OnPropertyChanged("MeasuredWeight");
}

public string LeaderId { get; set; }

public string UserId { get; set; }

} }

The problem is that Password property of PasswordBox is neither a dependency property nor implements INotifyPropertyChanged . 问题是, Password的性质PasswordBox既不是依赖属性,也没有实现INotifyPropertyChanged This means, that the changes of password will not be applied to the binding. 这意味着密码更改将不会应用于绑定。
Eg if you add an event handler for PasswordChanged to the PasswordBox and set the password to the Tag property, then you can bind to the Tag and the binding will work. 例如,如果将PasswordChanged的事件处理程序添加到PasswordBox并将PasswordBox设置为Tag属性,则可以绑定到Tag ,绑定将起作用。

<Button x:Name="loginButton" 
        Content="Belépés" 
        Margin="494,430,0,0" 
        VerticalAlignment="Top" 
        FontSize="20" 
        RenderTransformOrigin="-2.624,8.99" 
        HorizontalAlignment="Left" 
        Width="172"
        Command="{Binding NavCommand}">
    <Button.CommandParameter>
        <MultiBinding Converter="{StaticResource passwordConverter}">
            <Binding Path="Tag" ElementName="userIDPasswordBox"/>
            <Binding Path="Tag" ElementName="leaderIDPasswordBox"/>
        </MultiBinding>
    </Button.CommandParameter>
</Button>

<PasswordBox Name="userIDPasswordBox" PasswordChanged="PasswordBox_PasswordChanged"/>
<PasswordBox Name="leaderIDPasswordBox" PasswordChanged="PasswordBox_PasswordChanged"/>

private void PasswordBox_PasswordChanged(object sender, RoutedEventArgs e)
{
    var pbx = sender as PasswordBox;
    if (pbx!=null)
    {
        pbx.Tag = pbx.Password;
    }
}

Of course to avoid code behind implementation you should move the event handler to the behavior. 当然,为了避免实现后的代码,您应该将事件处理程序移至该行为。

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

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