在MainWindow上仅使用一个变量和一个事件来绑定多个TextBoxes的text属性

时间:2018-10-17 17:48:41

标签: c# wpf

我想使用单个SelectFile按钮的Select事件和outputFolderPath绑定来为WPF应用程序中的多个TextBox设置值。 我该怎么办?

这是MainWindow.xaml上的代码:

                    <StackPanel Grid.Column="0" Margin="10 10 0 0">
                        <TextBlock Margin="5 0 0 5" Text="First File:" FontWeight="Bold"/>
                        <TextBox Margin="0 0 0 5" Padding="2" x:Name="firstFilePath" Text="{Binding outputFolderPath}"/>

                        <TextBlock Margin="5 0 0 5" Text="Second File:" FontWeight="Bold"/>
                        <TextBox Margin="0 0 0 5" Padding="2" x:Name="secondfilePath" Text="{Binding outputFolderPath}"/>
                    </StackPanel>

                    <StackPanel Grid.Column="1" Margin="10 30 0 0">
                        <Button Margin="0 0 0 0" Content="Select" HorizontalAlignment="Left" Height="25" VerticalAlignment="Top" Width="80" Click="SelectFile"/>
                        <Button Margin="0 23 0 0" Content="Select" HorizontalAlignment="Left" Height="25" VerticalAlignment="Top" Width="80" Click="SelectFile"/>
                    </StackPanel>

还有一个在MainWindow.xaml.xs上:

public partial class MainWindow : Window, INotifyPropertyChanged
{
    public string outputFolderPath { get; set; }

    public MainWindow()
    {
        InitializeComponent();
        DataContext = this;
    }

    public event PropertyChangedEventHandler PropertyChanged;

    private void SelectFile(object sender, RoutedEventArgs e)
    {
        OpenFileDialog openFileDialog = new OpenFileDialog();

        if (openFileDialog.ShowDialog().Value)
        {
            outputFolderPath = openFileDialog.FileName;
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(outputFolderPath)));
        }
    }
}

WPF应用程序如下所示: [enter image description here

2 个答案:

答案 0 :(得分:0)

如下所示的实现界面INotifyPropertyChanged,并使用该事件通知属性更改。

您需要具有两个属性才能包含两个不同的值。

示例代码-

  public partial class MainWindow : Window, INotifyPropertyChanged
  {

    private string outputFolderPath;
    public string OutputFolderPath
    {
        get { return outputFolderPath; }
        set
        {
            outputFolderPath = value;
            OnPropertyChanged("OutputFolderPath");
        }
    }

    private string secondOutputFolderPath;
    public string SecondOutputFolderPath
    {
        get { return secondOutputFolderPath; }
        set
        {
            secondOutputFolderPath= value;
            OnPropertyChanged("SecondOutputFolderPath");
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
    public void OnPropertyChanged(string propertyName)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));

    }
  }

答案 1 :(得分:0)

出于样式和模板目的,我建议您提供两个依赖项属性,然后可以将TextBox es绑定到它们。

在您的MainWindow C#类上,定义以下只读依赖项属性:

#region FirstFile dependency property

public string FirstFile 
{
    get => (string)GetValue(FirstFileProperty);
    private set => SetValue(FirstFilePropertyKey, value);
}

private static readonly DependencyPropertyKey FirstFilePropertyKey = 
    DependencyProperty.RegisterReadOnly(nameof(FirstFile), 
        typeof(string), typeof(MainWindow));

public static readonly DependencyProperty FirstFileProperty = 
    FirstFilePropertyKey.DependencyProperty;

#endregion

#region SecondFile dependency property

public string SecondFile 
{
    get => (string)GetValue(SecondFileProperty);
    private set => SetValue(SecondFilePropertyKey, value);
}

private static readonly DependencyPropertyKey SecondFilePropertyKey = 
    DependencyProperty.RegisterReadOnly(nameof(SecondFile), 
        typeof(string), typeof(MainWindow));

public static readonly DependencyProperty SecondFileProperty = 
    SecondFilePropertyKey.DependencyProperty;

#endregion

现在,将您的文本框绑定到这些依赖项属性,如下所示:

<TextBox Margin="0 0 0 5" Padding="2" x:Name="firstFilePath" Text="{Binding FirstFile}"/>
<TextBox Margin="0 0 0 5" Padding="2" x:Name="secondfilePath" Text="{Binding SecondFile}"/>

依赖项属性为WPF提供了必要的通知机制,从而简化了我们的工作。如果可能,它们是绑定方案的首选。