WPF 读取 Checkbox.Content
WPF read Checkbox.Content
我有几个以 TextBlock 为内容的 CheckBox。
现在我想从每个复选框中读出 TextBlock.Text。
如果我读出像 checkBox.Content.ToString();
这样的内容,我只会得到 System.Windows.Controls.TextBlock
这有点道理。
我也尝试创建一个新的 TextBlock 并为其提供内容,但没有成功。
TextBlock _tempTBL = new TextBlock();
_tempTBL = checkBox.Content;
非常感谢任何帮助。
您必须将类型转换为 TextBlock
:
// no need to 'new' it up if you're assigning an existing instance...
TextBlock _tempTBL = (TextBlock) checkBox.Content;
var _tempTBL = (TextBlock) checkBox.Content; //Get handle to TextBlock
var text = _tempTBL.Text; //Read TextBlock's text
编辑:
在旁注中,您可以直接将所需文本设置为CheckBox's
内容。
checkBox.Content = "Hello World";
并且当您想要访问文本时,不需要类型转换
string text = checkBox.Content;
我有几个以 TextBlock 为内容的 CheckBox。 现在我想从每个复选框中读出 TextBlock.Text。
如果我读出像 checkBox.Content.ToString();
这样的内容,我只会得到 System.Windows.Controls.TextBlock
这有点道理。
我也尝试创建一个新的 TextBlock 并为其提供内容,但没有成功。
TextBlock _tempTBL = new TextBlock();
_tempTBL = checkBox.Content;
非常感谢任何帮助。
您必须将类型转换为 TextBlock
:
// no need to 'new' it up if you're assigning an existing instance...
TextBlock _tempTBL = (TextBlock) checkBox.Content;
var _tempTBL = (TextBlock) checkBox.Content; //Get handle to TextBlock
var text = _tempTBL.Text; //Read TextBlock's text
编辑:
在旁注中,您可以直接将所需文本设置为CheckBox's
内容。
checkBox.Content = "Hello World";
并且当您想要访问文本时,不需要类型转换
string text = checkBox.Content;