将文本框的文本属性绑定到 MainWindow-WPF 上定义的变量 [英] Binding text property of text box to variable defined on MainWindow-WPF

查看:40
本文介绍了将文本框的文本属性绑定到 MainWindow-WPF 上定义的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 WPF 的新手,我有文本框和按钮可以打开文件夹浏览器对话框.
当用户选择文件夹时,我希望文本框将包含所选路径.所以在 MainWindow 上我添加了两个变量:

I'm newbie on WPF and I have text box and button which open folder browser dialog.
When the user select folder I would like text box will contain the selected path. So on MainWindow I added two variables:

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

当用户选择文件夹路径时(在打开文件夹对话框之后),我通过执行(例如)更新了这些变量:

and when user selected folder path (after open folder dialog) I updated those variables by doing (for example):

outputFolderPath = dialog.SelectedPath

在 MainWindow.xaml 中:

In MainWindow.xaml:

<TextBox x:Name="outputFolder" Width ="200" Height="30" Grid.Row="1" Grid.Column="1" Margin="5 10">

如何将 TextBox.Text 绑定到 outputFolderPath 变量?
感谢您的帮助!

How can I bind TextBox.Text to outputFolderPath variable?
Thanks for your assitance!

推荐答案

您需要将窗口的 DataContext 设置为 this,以在 XAML 中访问您的属性,然后绑定到该属性.由于您绑定的不是 DependencyProperty,因此您应该通知您的绑定属性已更改,这可以通过在您的 Window 中实现 INotifyPropertyChanged 接口来完成.我提供了示例代码来展示这个概念.
但这很丑陋,最好使用 MVVM 模式代替.

You need to set DataContext of your window to this, to access your property in XAML, and after that bind to the property. As you are binding not to DependencyProperty, you should notify your binding that property has changed, which could be done by implementing INotifyPropertyChanged interface in your Window. I've provided sample code to show the concept.
But this is very ugly, much better to use MVVM pattern instead.

MainWindow.xaml.cs

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

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

    public event PropertyChangedEventHandler PropertyChanged;

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        outputFolderPath = "Some data";
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(outputFolderPath)));
    }
}

MainWindow.xaml

<Window x:Class="simplest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:simplest"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition/>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition />
            <RowDefinition />

        </Grid.RowDefinitions>

        <Button Click="Button_Click" Content="Go" />   
        <TextBox x:Name="outputFolder" Width ="200" Height="30" Grid.Row="1" Grid.Column="1" Margin="5 10" Text="{Binding outputFolderPath}"/>
    </Grid>
</Window>

这篇关于将文本框的文本属性绑定到 MainWindow-WPF 上定义的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆