2个用户控件之间的多绑定

本文关键字:绑定 之间 用户 控件 2个 | 更新日期: 2023-09-27 18:27:03

我有两个用户控件。用户控制1:菜单栏,有添加、编辑、删除、保存和撤消等按钮。用户控制2:是一个用户可以在文本框和密码框中输入文本的屏幕

但当我想保存时,我习惯于在只有1个用户控件的情况下执行以下操作,该控件具有按钮和所有内容,而不是菜单栏和详细信息屏幕分开:

     <Button Style="{DynamicResource SaveButton}" Command="{Binding Path=SaveCommand}">
                <Button.CommandParameter>
                    <MultiBinding Converter="{StaticResource pwConverter}">
                        <Binding ElementName="txtPassword" />
                        <Binding ElementName="txtRepeatPassword" />
                    </MultiBinding>
                </Button.CommandParameter>
            </Button>

但现在元素名称"txtPassword"answers"txtRetutPassword"不存在于该作用域中。这是我单击保存按钮时的SaveCommand。它接收这两个参数,所以我可以检查这两个密码是否相同等等。

    private void SaveUserExecute(object passwords)
    {
        try
        {
            var passwordvalues       = (object[])passwords;
            PasswordBox passwordBox1 = (PasswordBox)passwordvalues[0];
            PasswordBox passwordBox2 = (PasswordBox)passwordvalues[1];
       ...

关于如何解决这个问题有什么想法吗?

2个用户控件之间的多绑定

因为我的两个用户控件共享相同的DataContext,所以我创建了两个表示我的PasswordBoxes的属性。当我初始化该视图时,我做了以下操作:

    public InputUserView()
    {
        InitializeComponent();
        this.DataContext = InputUserViewModel.Instance;
        InputUserViewModel.Instance.PasswordBox1 = txtPassword;
        InputUserViewModel.Instance.PasswordBox2 = txtRepeatPassword;
    }

所以现在我的视图模型已经了解了这两个密码框。我认为它不是很好,但它对我有效,我可以接受它

如果使用MVVM模式,这很容易。您可以有一个ViewModel,它可以是每个用户控件和主窗口的DataContext。然后只需绑定到每个属性上。

下面是ViewModel的一个例子,它有由我们可以绑定到的属性公开的字段:

public class ViewModel : INotifyPropertyChanged
{
    private readonly Command _command;
    public Command Command
    {
        get { return _command; }
    }
    public ViewModel()
    {
        _command = new Command(this);
    }
    private string _textBoxOnUserControlOne;
    private string _textBoxOnUserControlTwo;
    public string TextBoxOnUserControlOne
    {
        get { return _textBoxOnUserControlOne; }
        set
        {
            if (value == _textBoxOnUserControlOne) return;
            _textBoxOnUserControlOne = value;
            OnPropertyChanged("TextBoxOnUserControlOne");
        }
    }
    public string TextBoxOnUserControlTwo
    {
        get { return _textBoxOnUserControlTwo; }
        set
        {
            if (value == _textBoxOnUserControlTwo) return;
            _textBoxOnUserControlTwo = value;
            OnPropertyChanged("TextBoxOnUserControlTwo");
        }
    }
    public event PropertyChangedEventHandler PropertyChanged;
    [NotifyPropertyChangedInvocator]
    protected virtual void OnPropertyChanged(string propertyName)
    {
        var handler = PropertyChanged;
        if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
    }
}

这是命令类,我将在其中使用这两个属性:

public class Command : ICommand
{
    private readonly ViewModel _viewModel;
    public Command(ViewModel viewModel)
    {
        _viewModel = viewModel;
    }
    public void Execute(object parameter)
    {
        var dataOnControlOne = _viewModel.TextBoxOnUserControlOne;
        var dataOnControlTwo = _viewModel.TextBoxOnUserControlTwo;
        //Use these values
    }
    public bool CanExecute(object parameter)
    {
        return true;
    }
    public event EventHandler CanExecuteChanged;
}

现在,这是我的第一个用户控件1,它绑定到我的ViewModel上的一个字段,注意DataContext:

<UserControl ... DataContext="{StaticResource ViewModel}">
    <Grid>
        <TextBox Height="23" HorizontalAlignment="Left" Text="{Binding TextBoxOnUserControlOne}" Margin="12,12,0,0" Name="textBox1" VerticalAlignment="Top" Width="120" />
    </Grid>
</UserControl>

这是具有相同DataContext的第二个UserControl,并且文本框绑定到不同的属性:

<UserControl ... DataContext="{StaticResource ViewModel}">
    <Grid>
        <TextBox Height="23" HorizontalAlignment="Left" Text="{Binding TextBoxOnUserControlTwo}" Margin="12,12,0,0" Name="textBox1" VerticalAlignment="Top" Width="120" />
    </Grid>
</UserControl>

这是我的主窗口,它包含这两个用户控件,还有一个绑定到我的命令类的按钮:

<Window ... DataContext="{StaticResource ViewModel}">
    <Grid>
        <my:UserControl1 HorizontalAlignment="Left" Margin="160,69,0,0" x:Name="userControl11" VerticalAlignment="Top" Height="47" Width="155" />
        <my:UserControl2 HorizontalAlignment="Left" Margin="160,132,0,0" x:Name="userControl12" VerticalAlignment="Top" Height="48" Width="158" />
        <Button Content="Button" Command="{Binding Command}" Height="23" HorizontalAlignment="Left" Margin="199,198,0,0" Name="button1" VerticalAlignment="Top" Width="75" />
    </Grid>
</Window>

最后是我的App.Xaml类,将所有内容粘合在一起:

<Application ...>
    <Application.Resources>
         <wpfApplication4:ViewModel x:Key="ViewModel"/>
    </Application.Resources>
</Application>

在这里,我们有单独的用户控件,并且字段绑定到一个视图模型上的属性。该视图模型将自己传递到命令类中,然后命令类可以访问单独用户控件上的文本框绑定到的属性,并在按下按钮时使用这些属性。我希望这能有所帮助!