I have 2 UserControls in my MainWindow,one is with list of some names and the other is with a ListBox,TextBox and Button.The problem is when i run the application my mainwindow should be only with usercontrol1 and when i click on the name the usercontrol2 should open on my mainwindow.How can i achieve this,I'm new to this please help me
-
Set the Visibility property of the usercontrol2 To hidden, and then change when tou want it to appear again, more information http://msdn.microsoft.com/en-us/library/system.windows.uielement.visibility.aspx – Alexandre Oct 29 '13 at 13:27
-
Can you please post your xaml? You could do this: on the click event of the `userControl1` you change `userControl2.Visibility`. i think it should work. – Alexandre Oct 29 '13 at 13:38
-
well you could try the selectedValueChanged event to do what I told before http://msdn.microsoft.com/en-us/library/system.windows.forms.listcontrol.selectedvaluechanged(v=vs.110).aspx – Alexandre Oct 29 '13 at 14:50
2 Answers
There are several ways that we could achieve your requirements. However, none of them have anything to do with Visibility
, so perhaps your title could do with an edit?
Either way, I just answered a similar question just a few hours ago (always a good reason to search for an answer before you ask a question here) which would answer your question. Please take a look at the WPF MVVM navigate views question here on StackOverflow for an answer to your question.
While it might not answer all of your requirements, I trust that you can successfully apply it to your situation. However, if you need further help, just leave me a comment.
-
Did you *actually* read the answer? I didn't mention any `Button`. Either way, the answer would be the same, just call `ViewModel = new PersonViewModel();` from a data bound `SelectedItem` property or `SelectionChanged` event handler. You can call that line from wherever (that is in scope) and it will update the view. – Sheridan Oct 29 '13 at 13:46
you can toggle between hiding and making control using VisiblityProperty
Bind the properties VisiblityProperty and change its value depending on your logic
refer the below links
Binding Visibility Converter in WPF C#
http://jeffhandley.com/archive/2008/10/27/binding-converters---visibilityconverter.aspx

- 1
- 1

- 1,311
- 8
- 8
-
While the word `Visibility` *is* in the title of this question, I believe that it is slightly miss-leading. It would *not* be good to implement the user's requirements using the `Visibility` property because that would mean that all of the views would be taking up resources, even when not visible. – Sheridan Oct 29 '13 at 13:43