WPF后台访问XAML元素
当我们需要从后台访问xaml文件时,我们可以通过这样的方式来操作:
private void button1_Click(object sender, RoutedEventArgs e)
{ System.Windows.MessageBox.Show(this.textBox1.GetValue(TextBox.TextProperty).ToString());
} private void button2_Click(object sender, RoutedEventArgs e)
{
TextBox t = this.FindName("textBox1") as TextBox; System.Windows.MessageBox.Show("第二种方式:" + t.Text);
} private void button3_Click(object sender, RoutedEventArgs e)
{
var v = this.Content;
Grid g = v as Grid;
UIElementCollection uc = g.Children; Control[] us = new Control[uc.Count];
g.Children.CopyTo(us, );
string str = (us.First(a => a.Name == "textBox1") as TextBox).Text;
System.Windows.MessageBox.Show("第三种方式:" + str);
}
xaml文件:
<Window x:Class="WPF后台访问XAML元素.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="" Width="">
<Grid Background="BlanchedAlmond">
<TextBox Height="" HorizontalAlignment="Left" FontSize="" Margin="98,29,0,0" Name="textBox1" VerticalAlignment="Top" Width="" >HELLO WORD!</TextBox>
<Button Content="方式一" Height="" HorizontalAlignment="Left" Margin="107,109,0,0" Name="button1" VerticalAlignment="Top" Width="" Click="button1_Click" />
<Button Content="方式二" Height="" HorizontalAlignment="Left" Margin="107,146,0,0" Name="button2" VerticalAlignment="Top" Width="" Click="button2_Click" />
<Button Content="方式三" Height="" HorizontalAlignment="Left" Margin="107,182,0,0" Name="button3" VerticalAlignment="Top" Width="" Click="button3_Click" />
</Grid>
</Window>
具体效果演示:
demo小示例下载:http://files.cnblogs.com/BABLOVE/WPF%E5%90%8E%E5%8F%B0%E8%AE%BF%E9%97%AEXAML%E5%85%83%E7%B4%A0.rar
WPF后台访问XAML元素的更多相关文章
- WPF后台设置xaml控件的样式System.Windows.Style
WPF后台设置xaml控件的样式System.Windows.Style 摘-自 :感谢 作者: IT小兵 http://3w.suchso.com/projecteac-tual/wpf-zhi ...
- 【广州.NET社区推荐】【译】Visual Studio 2019 中 WPF & UWP 的 XAML 开发工具新特性
原文 | Dmitry 翻译 | 郑子铭 自Visual Studio 2019推出以来,我们为使用WPF或UWP桌面应用程序的XAML开发人员发布了许多新功能.在本周的 Visual Studio ...
- 【译】Visual Studio 2019 中 WPF & UWP 的 XAML 开发工具新特性
原文 | Dmitry 翻译 | 郑子铭 自Visual Studio 2019推出以来,我们为使用WPF或UWP桌面应用程序的XAML开发人员发布了许多新功能.在本周的 Visual Studio ...
- Windows Store App 全球化:在XAML元素中引用文件资源
上一小节讲解了如何在XAML元素中使用x:Uid属性引用资源文件中的字符串资源,通过改变语言首选项显示不同的运行结果.如果把字符串资源变成文件资源,就不能使用x:Uid属性来引用这些文件资源.本小节将 ...
- Windows Store App 全球化:在XAML元素中引用字符串资源
在应用程序中可以通过XAML元素和后台代码两种方式引用资源文件中的字符串资源.本小节先讲述如何在XAML元素中引用字符串资源的相关知识点. 在XAML元素中可以通过使用x:Uid属性来引用资源文件中的 ...
- 学习WPF——了解WPF中的XAML
XAML的简单说明 XAML是用于实例化.NET对象的标记语言,主要用于构建WPF的用户界面 XAML中的每一个元素都映射为.NET类的一个实例,例如<Button>映射为WPF的Butt ...
- WPF后台动画DoubleAnimation讲解
WPF后台动画,使用DoubleAnimation做的. 1.移动动画 需要参数(目标点离最上边的位置,目标点离最左边的位置,元素名称) Image mImage = new Image(); Flo ...
- WPF 后台C#设置控件背景图片
原文:WPF 后台C#设置控件背景图片 以前的程序中有做过,当时只是记得uri很长一大段就没怎么记.今天有人问了也就写下来. 这是一个Button,设置了Background后的效果. 前台的设置 ...
- How do I duplicate a resource reference in code behind in WPF?如何在WPF后台代码中中复制引用的资源?
原文 https://stackoverflow.com/questions/28240528/how-do-i-duplicate-a-resource-reference-in-code-behi ...
随机推荐
- Linux防火墙iptables学习笔记(三)iptables命令详解和举例[转载]
Linux防火墙iptables学习笔记(三)iptables命令详解和举例 2008-10-16 23:45:46 转载 网上看到这个配置讲解得还比较易懂,就转过来了,大家一起看下,希望对您工作能 ...
- PAT---1050. String Subtraction (20)
#include<iostream> #include<string.h> #include<stdio.h> using namespace std; #defi ...
- [TypeScript] Function Overloads in Typescript
It's common in Javascript for functions to accept different argument types and to also return differ ...
- [rxjs] Throttled Buffering in RxJS (debounce)
Capturing every event can get chatty. Batching events with a throttled buffer in RxJS lets you captu ...
- hadoop错误org.apache.hadoop.mapred.MapTask$NewOutputCollector@17bda0f2
错误: org.apache.hadoop.mapred.MapTask$NewOutputCollector@17bda0f2 java.io.IOException: Spill failed 错 ...
- 【转】jsoncpp在xcode中的使用
http://blog.csdn.net/ashqal/article/details/8573392 考虑到cocos2dx需要使用jsoncpp做关卡的设置, 尝试用源代码直接放到项目以方便后期生 ...
- django开发框架之jumpserver
发现一个不错的开源堡垒机 jumpserver: https://github.com/ibuler/jumpserver 最开始看的是jumpserver2.0.0 版本,具体的实现方式是: 1. ...
- POSIX字符类型
[:alnum:] 字母与数字 [:alpha:] 字母 [:blank:] 空格与制表符 [:cntrl:] 控制字符 [:digit:] 数字 [:graph:] 可打印的与可见的(不包括空格)字 ...
- (转)一个form表单实现提交多个action
方法一: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4 ...
- winform windowsmediaplayer的属性
首先将C:\WINDOWS\system32下的wmp.dll应用到项目中: WMPLib.WindowsMediaPlayerClass player = new WMPLib.WindowsMed ...