gpt4 book ai didi

c# - 如何获取WPF中任意控件的 "Content"?

转载 作者:太空宇宙 更新时间:2023-11-03 20:25:50 25 4
gpt4 key购买 nike

我正在编写一个函数,它会将 Control Ctrl 作为 Arguement 并修改它的 Control.Content。有没有办法获取和设置任何 ControlContent??

代码:

void RemoveHotKey(Control Ctrl, int KeyIndex)
{
if (Ctrl.Content.ToString().Substring(KeyIndex, 1) == "_") // System.Windows.Controls.Control does not contain a definition for 'Content'
{
Ctrl.Content = Ctrl.Content.ToString().Remove(KeyIndex, 1); // System.Windows.Controls.Control does not contain a definition for 'Content'
}
}

最佳答案

试试这个:

void RemoveHotKey(ContentControl Ctrl, int KeyIndex) 
{
if (Ctrl.Content.ToString().Substring(KeyIndex, 1) == "_")
{
Ctrl.Content = Ctrl.Content.ToString().Remove(KeyIndex, 1);
}
}

看看here .

或者这个:

void RemoveHotKey(Control Ctrl, int KeyIndex)
{
ContentControl contentCtrl = Ctrl as ContentControl;
if (contentCtrl != null && contentCtrl.Content != null)
{
if (contentCtrl.Content.ToString().Substring(KeyIndex, 1) == "_")
{
contentCtrl.Content = contentCtrl.Content.ToString().Remove(KeyIndex, 1);
}
}
}

这比使用反射要便宜得多..

关于c# - 如何获取WPF中任意控件的 "Content"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10698320/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com