Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何按名称隐藏可见对象?_C#_Wpf - Fatal编程技术网

C# 如何按名称隐藏可见对象?

C# 如何按名称隐藏可见对象?,c#,wpf,C#,Wpf,在循环中创建textBlock之后 TextBlock tb = new TextBlock(); tb.Text = dataShows.name[i]; tb.TextAlignment = TextAlignment.Center; tb.Foreground = new SolidColorBrush(Colors.Black); tb.Name = "tb" + i; tb.Visibility = Visibility.Hidden; 然后我想向textBlock显示名称为tb3同

在循环中创建textBlock之后

TextBlock tb = new TextBlock();
tb.Text = dataShows.name[i];
tb.TextAlignment = TextAlignment.Center;
tb.Foreground = new SolidColorBrush(Colors.Black);
tb.Name = "tb" + i;
tb.Visibility = Visibility.Hidden;
然后我想向textBlock显示名称为tb3同一代码
tb3.Visibility=Visibility.Visibility
如何在代码中自动查找对象名称


编辑:对不起,我的语法不好。我想知道C语言中的哪些代码与javascript中的document.getElementById('#name')相同,例如,将TextBlock对象存储在一个列表中:
list
,您可以迭代该列表

更新:

我认为你从错误的方向开始:你可以做一些事情,比如javascripts
document.getElementByID()
,它被称为反射;但是如果你是初学者,我不能推荐你这样做

通过搜索
myControl.Controls
array,并检查其名称/类型,可以获取表单/控件中的所有控件

但最简单的方法是使用列表保存TextBlock对象:

List<TextBlock> a = new List<textBlock>();

// in the creating loop:
a.Add(tb);

// access using Linq:
textBlock res = a.Find(c=>c.Name=="thename");
// is roughly the same as
foreach(TextBlock b in a)
    if(b.Name=="thename") {
        res = b;
        break;
    }
List a=新列表();
//在创建循环中:
a、 增加(tb);
//使用Linq访问:
textBlock res=a.Find(c=>c.Name==“thename”);
//大致与
foreach(a中的文本块b)
如果(b.Name==“thename”){
res=b;
打破
}
另一种方式与JavaScript不同

您使用了code
document.getElementById('#name')
来显示您的意图。让我们分析一下JS在执行这一行时到底做了什么。它在整个DOM中搜索ID名称为的对象,并返回对该对象的引用

C#不是那样运作的。在某个地方没有一堆易于搜索的对象,您只需要执行查询并获取所需的对象。如果你想保留一个对象列表,你必须自己制作这个列表

我假设您正在创建几个
TextBlock
-对象。当您这样做时,您需要将它们添加到一个列表中,然后您可以查询该列表。下面是一个示例实现:

List<TextBlock> textBlocks = new List<TextBlock>();

private TextBlock GetTextBlockByName(string name)
{
    if(name == null) return null; //I assume that a TextBlock needs to have a name.
    // Returns the TextBlock if it was found or null if not.
    // Throws an Exception if more than one TextBlock has the same name
    return this.textBlocks.Find(t => t.Name == name).SingleOrDefault();

}
List textBlocks=new List();
私有TextBlock GetTextBlockByName(字符串名称)
{
if(name==null)返回null;//我假设TextBlock需要有一个名称。
//如果找到文本块,则返回该文本块;如果未找到,则返回null。
//如果多个TextBlock具有相同的名称,则引发异常
返回此.textBlocks.Find(t=>t.Name==Name).SingleOrDefault();
}


给你一个友好的提示,因为我假设你对C语言还不是非常精通:当你接触到一种新的编程语言时,不要假设它遵循与你所知道的其他语言完全相同的范式,即使它们偶尔有相似之处。我们有这么多不同语言的全部原因是,有些东西在一种语言中很容易,在另一种语言中很难。

然后我想向textBlock显示名称是tb3相同的代码
什么?我真的不知道你说这句话是什么意思是的,我无法用我的语法解释你所理解的。我想知道c#相同文档中的命令javascript@KittinunPongsukjai实际上,从您的评论可以理解,您现在只想在窗体上按名称查找控件。@EpicKip我使用的wpf窗口不是“按名称隐藏可见对象”,而是至少“按名称查找”部分你甚至没有展示如何在列表中找到它(你可以使用linq或其他明显的工具)完成了,谢谢你的观点。我倾向于极简主义…谢谢,我之所以问这个问题,唯一的原因是因为我觉得OP和编程没什么关系。你可以通过名称找到一个控件,不要让它比解释问题更复杂更难。啊,我不知道,因为我不太经常使用WinForms。请随意编辑我的答案,并建议一个更简单的例子它不是winforms,它的wpf和Casiosmu建议的答案()很好,如果我对wpf有更多的了解,我会编辑(并且可以回答比当前答案更好)。但OP已经通过谷歌搜索“如何通过名称wpf获得控制权”