WPF读取Checkbox.Content [英] WPF read Checkbox.Content

查看:241
本文介绍了WPF读取Checkbox.Content的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有几个带有TextBlock作为内容的CheckBoxes。
现在,我想从每个复选框中读取TextBlock.Text。

I have a couple of CheckBoxes with a TextBlock as content. Now I want to read out the TextBlock.Text from each Checkbox.

如果我读取了 checkBox.Content之类的内容。 ToString(); 我只得到 System.Windows.Controls.TextBlock ,这很有道理。

If I read out the content like checkBox.Content.ToString(); I only get System.Windows.Controls.TextBlock which kinda makes sense.

我还尝试创建一个新的TextBlock并为其提供内容,但是它不起作用。

I also tried to create a new TextBlock and give it the content but it didn't work.

  TextBlock _tempTBL = new TextBlock();
  _tempTBL = checkBox.Content;

任何帮助都是令人感激的。

Any help is much appreciated.

推荐答案

var _tempTBL = (TextBlock) checkBox.Content; //Get handle to TextBlock
var text = _tempTBL.Text; //Read TextBlock's text

编辑:

在旁注中,您可以将所需文本直接设置为 CheckBox的内容。

On a side note, you can directly set desired text as CheckBox's content.

checkBox.Content = Hello World;

当您要访问文本时,不进行类型转换是必需的

And when you want to access the text, no type cast is needed

字符串text = checkBox.Content;

这篇关于WPF读取Checkbox.Content的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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