如何在 WPF 中从 C# 控制 xaml 按钮 [英] How to control a xaml button from C# in WPF

查看:30
本文介绍了如何在 WPF 中从 C# 控制 xaml 按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有这样按钮的 xaml 文件

I have a xaml file with buttons like this

<Button Grid.Column="4" BorderThickness="0">3</Button>
<Button Grid.Column="5" BorderThickness="0">4</Button> 

问题是我无法控制 C# 中的按钮.我已经阅读了有关模板和资源的内容并进行了尝试,但我不确定我所做的是否正确.

The Problem is I can't manage to control the buttons in C#. I've read about templates and resources and tried it but I am unsure if I did was correct.

大多数在线示例只有一个点击功能,但我不能完全控制将所有这些按钮都放在一个 2D 数组中并在需要时更改它们的按钮.

Most examples online just have a click function but I wan't full control of the button having all of them in a 2D Array and changing them when I want to.

那么如何指向在 xaml 文件中创建的按钮以便我可以执行

So How do I point to a button created in the xaml file so that I could do

button.Content = "test"; 

什么的.

推荐答案

关于在 XAML 中命名按钮的所有答案都是正确的.

All the answers about naming buttons in XAML are right.

<Button x:Name="btn0"/>
<Button x:Name="btn1"/>

命名组件使它们公开,因此您可以在后面的代码中轻松访问它们.如果需要,您可以稍后创建按钮列表并一一添加.然后只需访问列表.

Naming components make them public, thus you can easily access to them in the code behind. If you want, you can later create List of buttons and add them one by one. And then just access to the list.

这篇关于如何在 WPF 中从 C# 控制 xaml 按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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