Click here to Skip to main content
15,895,779 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
XML
<StackPanel Orientation="Horizontal">
    <Label Name="status" Content="" Width="62" Background="Green" Height="38"></Label>
</StackPanel>


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.
Posted
Updated 25-Feb-14 17:13pm
v4
Comments
agent_kruger 21-Feb-14 3:50am    
i think so, the language you are using is ASP.Net?
Guru Swaroop 21-Feb-14 11:04am    
Sorry I am using .NET framework 4.0 and not ASP.NET
agent_kruger 21-Feb-14 11:31am    
no the language you are developing the software in?
Guru Swaroop 21-Feb-14 22:10pm    
I am developing the software in C#, on Visual Studio 2010 using WPF Application
Guru Swaroop 21-Feb-14 22:10pm    
I am developing the software in C#, on Visual Studio 2010 using WPF Application

Try:
C#
status.Visibility = Visibility.Hidden;
Or:
C#
status.Visibility = Visibility.Visible;
 
Share this answer
 
Comments
Guru Swaroop 21-Feb-14 4:02am    
Well this is not working since I am not accessing these variables inside MainWindow class, but rather I want to access them in some other class under same namespace.
OriginalGriff 21-Feb-14 4:18am    
Then you have to add properties or methods to your MainWindow class to manipulate them, and access those via the MainWindow instance.
Guru Swaroop 21-Feb-14 5:52am    
Yeah.. I just tried it, but It is not getting modified.
OriginalGriff 21-Feb-14 5:57am    
What exactly did you try?
Show the code, please!
Guru Swaroop 21-Feb-14 10:36am    
In the MainWindow.xaml.cs file :

namespace FaceTrackingBasics
{ ...
...
public partial class MainWindow : Window
{ ...
...
public void ChangeVisibility()
{
this.status.Visibility = System.Windows.Visibility.Hidden;
}
}
...
...
}

In the ExpressionRecognition.cs file :

namespace FaceTrackingBasics
{ ...
...
public class SkeletonFaceTracker : IDisposable
{ private static MainWindow win = new MainWindow();
...
...
private void displayExpression(EnumIndexableCollection<animationunit,float> animUnits)
{
int expression = -1;

expression = exp.NearestNeighbourClassifier(new AnimationCoefficient(animUnits.ToArray()), 3);

win.ChangeVisibility();
}
}
}

Well I think the way I am trying to instantiate the MainWindow in SkeletonFaceTracker is incorrect, though visual studio 2010 is not showing any error.
See the example[^] given.

-KR
 
Share this answer
 
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.
 
Share this answer
 
status.Visibility=System.Windows.Visibility.Hidden;
 
Share this answer
 
v2
Comments
Guru Swaroop 21-Feb-14 5:07am    
same problem as mentioned earlier... cannot access status.Visibility
OriginalGriff 21-Feb-14 5:59am    
Reason for my vote of one: duplication of existing answer, posted an hour earlier...
This can be taken as rep hunting and abuse, so I would strongly suggest that you try hard to ensure it doesn't happen again...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900