Wesley Heron
Wesley Heron

Reputation: 423

Create WPF Function to Hidden Images Controls

I'm currently developing a WPF C# Application that contains some textbox validations. If field is valid it must show a ok validation image if not valid it must show a wrong validation image, like an image below. enter image description here

My Problem is how to set visibility = visibility.Hidden for all images if I click on cancelar button or another button. I know set img1.visibility = visibility.Hidden;, img2.visibility = visibility.Hidden;, img3.visibility = visibility.Hidden;... Works but i need to create a function to do it. I believe that I create a List of Images and pass this List of parameter to a function works fine and I can use this function for other validations. So how can I do it?

Upvotes: 0

Views: 109

Answers (2)

kb9
kb9

Reputation: 359

Please check this article : Data Binding

If you implement data binding then you have just to bind properties:

<Image Source="..." Visibility="{Binding Img1Visibility}"/>

Implement ViewModel class via INotifyPropertyChanged

And then simply work with your Properties in code.

UPD

If you want to simply create function to work with your images then move your img1.visibility = visibility.Hidden;, img2.visibility = visibility.Hidden;, img3.visibility = visibility.Hidden; in separate function inside your MainWindow.xaml.cs file, you don't have to pass it as arguments as you work in one MainWindow class.

So simply:

private void Fun() 
{
    img1.visibility = visibility.Hidden;
    img2.visibility = visibility.Hidden;
    img3.visibility = visibility.Hidden;
}

And request your Fun() method from ClickButton handler.

Upvotes: 1

Eldar Dordzhiev
Eldar Dordzhiev

Reputation: 5135

Create an array of the image controls and iterate over it.

List<Image> _images = new List<Image>
{
    img1,
    img2,
    ...
};

void Cancelar()
{
    foreach (var image in _images)
    {
        image.Visibility = Visibility.Hidden;
    }
}

But still, the code is awful. Witness me SO.

Upvotes: 0

Related Questions