Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have created a Wpf application with mvvm architecture. In main window I have shown a first user control with tile in it. Now I need when I click on a tile of the first user control then it will hide and open second user control in the same Main window and if i close second usercontrol it has to go back to first user control. (Similar to Windows 8 when we click on tile it will open and if we close it will go back to main window)

Can any one tell me ho to achieve this.


i have tried in this way:

on click of tile event i am trying to display second user control.

 private void Tile_Click(object sender, EventArgs e)
        {           
            SecondUserControl.Visibility = Visibility.Visible;      

        }  

but not working :(
Posted
Updated 15-Nov-15 18:03pm
v2
Comments
Sergey Alexandrovich Kryukov 16-Nov-15 2:39am    
The question makes no sense. "Not working" is not informative. Setting visibility is not all the functionality. The control instance can be made non-functional (and even practically invisible) in millions of ways. After 74 questions you have (and I believe many more that have been removed from the site), you could have learned at least a bit about getting some help and asking reasonable questions.

Please understand: people familiar with your other posts may stop dealing with you, because they can see that you don't really use help, don't learn from it.

—SA
Sinisa Hajnal 16-Nov-15 5:52am    
I would raise an event to parent control of both tile and SecondUserControl and let it control what shows when and where. Trying to control SecondUserControl which is NOT part of your Tile control is creating dependencies where there is none. Either create custom control that will contain both tile and your second control or pass the responsibility for showing/hiding out of your tile. Good luck
[no name] 16-Nov-15 6:41am    
Thank you.

1 solution

In MVVM if you would like to hide and show user controls you need to set up 2 properties.

C#
public bool IsFirstVisible {get; set;}
public bool IsSecondVisible {get; set;}

public void OnTileClick()
{
    if (IsFirstVisble)
    {
        IsSecondVisible = true;
        IsFirstVisible = false;
    }
    else
    {
        IsSecondVisible = false;
        IsFirstVisible = true;
    }
}


Now, on your tile click, you will toggle the visibility on which user control is being shown. Next you need to reflect this on the xaml with a BoolToVisibility Converter like so.

XML
<window.resources>
    <booleantovisibilityconverter x:key="boolToVis"  />
</window.resources>

<local:firstusercontrol visibility="{Binding IsFirstVisible, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, Converter = {StaticResource boolToVis}}"  />

<local:secondusercontrol visibility="{Binding IsSecondVisible, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, Converter = {StaticResource boolToVis}}"  />;


This should reflect a visible state when the property is true, and a collapsed state when the property is false.
 
Share this answer
 
v3

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