wpf中要动态的改变textbox的值需要通过绑定来实现,下面我就一步一步讲解如何绑定。
首先在MainWindow中定义了两个控件,一个button和一个textbox。并将这两个控件放在viewbox中。
<Window x:Class="TextboxBind.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:TextboxBind"
mc:Ignorable="d"
Title="MainWindow" Height="300" Width="500">
<Window.Resources>
<local:Bean x:Key="textBean" FilePath="未知"/>
</Window.Resources>
<Grid>
<Viewbox DataContext="{StaticResource ResourceKey=textBean}">
<Grid HorizontalAlignment="Center" Margin="0,2,0,0">
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<TextBox Grid.Row="0" Width="300" Height="20" Margin="20,20,20,33"
Text="{Binding Path=FilePath}" FontSize="12"/>
<Button Grid.Row="1" Width="70" Height="40" Margin="20" Click="importButton">导入</Button>
</Grid>
</Viewbox>
</Grid>
</Window>
然后我们定义了一个类,Bean类,这个类继承自INotifyPropertyChanged,来监听变量是否发生改变,对变量进行监听。在这个类
中定义了两个变量,filePath和FilePath,并且为FilePath定义了set和get方法,并且在set方法中,判断FilePath的值是否发生
了改变,如果发生了改变则唤起改变响应事件。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace TextboxBind
{
class Bean : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged; //属性改变时触发的事件
private string filePath; //文件路径名
public string FilePath //实际中用到的变量名
{
get
{
return this.filePath;
}
set
{
if (this.filePath != value) //如果当前的变量值不等于先前的文件名,说明需要更新文件名
{
this.filePath = value; //更新文件名
if (PropertyChanged != null) //如果已经触发了改变事件
{
//通知绑定此变量的textbox在前台更新
PropertyChanged(this, new PropertyChangedEventArgs("FilePath"));
}
}
}
}
}
}
然后在MainWindow中定义了一个local resource,key为textBean,并且将这个textBean作为ViewBox的DataContext,从而实现了ViewBox
的内容和textBean绑定起来。然后将TextBox的text值绑定为textBean中的FilePath,当FilePath的值发生改变时,就会同步到TextBox的text值。
最后是在MainWindow.cs中定义了一个按钮响应函数importButton,在这个函数中选择打开文件就可以获得文件的完整路径,并通过改变textBean中的FilePath值,来更新到TextBox中。
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace TextboxBind
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
/*
*导入按钮响应事件函数
*/
public void importButton(object sender, RoutedEventArgs e)
{
OpenFileDialog fileDialog = new OpenFileDialog(); //弹出打开文件窗口类
Nullable<bool> result = fileDialog.ShowDialog(); //得到打开结果
if (result == true) //如果打开成功
{
string filePath = fileDialog.FileName; //打开文件的路径
Bean bean = (Bean)this.FindResource("textBean");
bean.FilePath = filePath; //改变FilePath,触发改变事件
}
}
}
}
点击导入按钮前TextBox的显示如下:
点击导入按钮并选择文件之后的TextBox的显示如下: