如何更改System.Windows.Control.Label的可见性? [英] How do I change the Visibility of System.Windows.Control.Label ?

查看:102
本文介绍了如何更改System.Windows.Control.Label的可见性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<StackPanel Orientation="Horizontal">
    <Label Name="status" Content="" Width="62" Background="Green" Height="38"></Label>
</StackPanel>





我想从C#代码中更改'status'标签的可见性,如果满足某些条件,那么我想显示'status'元素,否则我想隐藏它。 br />


我能够在MainWindow类的任何方法中访问'status'变量,但我想在同一名称空间下的其他类中访问它。



I would like to change the visibility of the 'status' Label from my C# code, if certain conditions are satisfied then I want to display the 'status' element else I want to hide it.

I am able to access the 'status' variable inside any method of MainWindow class, but I want to access it in some other class under the same namespace.

推荐答案

尝试:

Try:
status.Visibility = Visibility.Hidden;

或:

Or:

status.Visibility = Visibility.Visible;


参见example [ ^ ]给出。



-KR
See the example[^] given.

-KR


这个问题的解决方案是通过利用事件,将信息从一个类发送到另一个类来实现的, OriginalGriff在之前的评论中正确指出,请看看OriginalGriff撰写的这篇文章:



在两种形式之间传递信息,第2部分:儿童到父母 [ ^ ]



向我们介绍事件概念的另一篇有用的文章是:

代表 - 15分钟快速入门教程 [ ^ ]



这个问题的解决方案可归纳如下:



所有变量都是MainWindow.xaml文件中定义的只能在MainWindow.xaml.cs中的MainWindow类中访问,因此要在同一项目中的任何其他类中访问这些变量,我们必须使用Events。我们可以在该类中触发我们想要访问MainWindow.xaml变量的事件,并在MainWindow类中处理该事件。多数民众赞成。
The solution to this problem is achieved by making use of Events, to send information from one class to another, as was rightly pointed out by OriginalGriff in one of the previous comments, have a look at this article authored by OriginalGriff :

Transferring information between two forms, Part 2: Child to Parent[^]

Another useful article which introduces to us the concept of Events is :
Delegates - a 15 minutes quick start tutorial[^]

The solution to this problem can be summarized as follows:

All the variables which are defined in the MainWindow.xaml file are accessible only in MainWindow class which is present in MainWindow.xaml.cs, so to access these variables in any other class in the same project,we have to make use of Events. We can trigger the Event in that class where we want to access the variables of MainWindow.xaml and handle that Event in MainWindow class. Thats it.


这篇关于如何更改System.Windows.Control.Label的可见性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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