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的显示如下:

Wpf中通过绑定来更新textbox的值相关推荐

  1. WPF中UserControl 绑定样式(小白教程)

    1.绑定xaml页内样式 这个操作只需要对需要更改样式的窗体内进行,不涉及别的页面.适合只需要更改一个窗体内的控件样式. 这里的TargetType是为了绑定控件类型的,也就是当你引用这个样式后,窗体 ...

  2. WinForm中ComBoBox绑定显示值和实际值

    在ASP.NET中ComBoBox可以绑定显示值和实际值,但是在Winform中却是没有的(通过DataSource绑定数据源的时候是可以的,但是有null值和空值的限制) (所有代码均从实际项目中截 ...

  3. WPF中RadioButton绑定数据的正确方法

    RadioButton一般用于单选的时候,也就是从一组值中选择一个值. 比如性别有"男"和"女"两种取值,而对于一个员工的实例来说,性别的取值要么是男,要么是女 ...

  4. WPF中DataGrid绑定数据显示

    前端Xaml部分: <DataGrid x:Name="groupInformationShow" HorizontalAlignment="Left" ...

  5. 艾伟:WPF中,如何将绑定源设置到单件实例

    大概两个月前,曾有位朋友问我:如果我想在WPF中将绑定源设置到某个采用单件模式设计的实例上,应该怎么做呢?这是一个不错的问题.可能这段时间比较忙,呵呵,忘记回答这个问题了,昨天拿到伍迷大哥的<大 ...

  6. CleanAOP实战系列--WPF中MVVM自动更新

    CleanAOP实战系列--WPF中MVVM自动更新 作者: 立地 邮箱: jarvin_g@126.com QQ: 511363759 CleanAOP介绍:https://github.com/J ...

  7. WPF中TextBox更改完了之后进行操作

    WPF中TextBox的Text更改的相关方法有两种 TextChanged SourceUpdated TextChanged 事件 在 TextBox 控件中的文本发生更改时使用 TextChan ...

  8. WPF中DatePiker值绑定以及精简查询

    WPF中DatePiker值绑定以及精简查询 1.WPF中DatePiker值绑定 Xaml中值绑定使用Text <DatePicker Text="{Binding strMinDa ...

  9. WPF中ListBox的绑定

    WPF中列表式控件派生自ItemsControl类,继承了ItemsSource属性.ItemsSource属性可以接收一个IEnumerable接口派生类的实例作为自己的值(所有可被迭代遍历的集合都 ...

最新文章

  1. DM***+EZ***
  2. IDLE 策略算法 放置奇兵 小破船翻船记录(悬空岛——冒险(航海))
  3. 未获取root手机抓包方法
  4. lcp mysql cluster_Mysql Cluster 非root用户启动ndbd节点报错
  5. 二叉树 查找失败 asl_算法——二分搜索amp;折半查找
  6. vm虚拟服务器控制端,使用VMware虚拟机实现单主机双屏两人同时使用独立控制
  7. read-shrinkwrap This version of npm is compatible with lockfileVersion@1, but package-lock.json was
  8. Open Aspect Target Sentiment Classification with Natural Language Prompts
  9. iOS-申请邓白氏编码的超详细流程介绍
  10. 京东直租瞄准租房痛点,重新定义房屋租赁行业新标准!
  11. TCP创建多人聊天室
  12. java.lang.ClassNotFoundException: sun . jdbc . odbc . JdbcOdbcDriver
  13. 做城市规划设计,如何下载地形图?
  14. 小天带你轻松解决Mybatis延迟加载原理源码问题
  15. 《Google软件测试之道》读书笔记
  16. 点融网落地区块链应用项目,与富士康合作只是开端
  17. 认识恶意软件、病毒的传播方式、工作过程以及防御
  18. 【cf 1182 E】Product Oriented Recurrence
  19. 如何选择适合你的兴趣爱好(四十一),养猫
  20. 5种简单快速的方法解除PDF文件密码保护

热门文章

  1. Cocos2d-x3.1TestCpp之NewRenderTest Demo分析
  2. 免费流量监控软件,最大可同时监控1000台电脑
  3. 你不知道的Chrome调试技巧
  4. 容器编排技术 -- Google Computer Engine入门
  5. Linux系统 设置 cockpit 自动开机启动时异常问题处理(设置systemctl enable cockpit 异常【解决办法】)
  6. MAC下 Intellij IDEA GO语言插件安装及简单案例
  7. IntelliJ IDEA Community社区版集成Tomcat or Jetty教程
  8. 现代计算机模型要求程序在执行,大学计算机基础考试考点.doc
  9. c mysql 取错误信息_初始化 MYSQL 后为何得到一会错误信息?
  10. 安卓开发学习笔记(六):如何实现指定图片定时开屏功能?