Reputation: 57
So I have three views created with usercontrol in wpf. And I created 3 buttons to change views.
MainWindow.xaml.cs
private void view1_clicked(object sender, RoutedEventArgs e)
{
DataContext = new view1model1();
}
private void view2_clicked(object sender, RoutedEventArgs e)
{
DataContext = new viewmodel2();
}
private void view3_clicked(object sender, RoutedEventArgs e)
{
DataContext =new view1model3();
}
And Lets say I have textblock in view2 and its visibility is collapsed on purpose when I clicked view2 at the first time.
public Classification()
{
InitializeComponent();
if(condition == null)
{
someControl.Visibility = Visibility.Collapsed;
}
private void condition_changed(object sender, SelectionChangedEventArgs e)
{
if(someControl.Visibility == Visibility.Collapsed)
{
someControl.Visibility = Visibility.Visible;
}
}
And In view2, There is a condition control that triggers someControl.visibility = collapsed to visible.
But the problem is when I clicked view3 or view1 and then come back to view2 , somecontrol is changed to collapsed.
I know it is because I set 'someControl' to collapsed in the constructor. But I can't figure it out how to make it work as I wish.
To make it short, I want to set somecontrol.visibility to collapsed at the begining, but once it is changed to visible after triggering condition, I want to keep it visible until I clicked refresh button in view2 which I'm going to create later.
Upvotes: 2
Views: 444
Reputation: 70652
You have two options:
MainWindow
constructor, storing the references in a private field in the class, and setting the DataContext
to the appropriate reference when the button is clicked. For example:partial class MainWindow : Window
{
private readonly model1 = new view1model1();
private readonly model2 = new viewmodel2();
private readonly model3 = new view1model3();
private void view1_clicked(object sender, RoutedEventArgs e)
{
DataContext = model1;
}
private void view2_clicked(object sender, RoutedEventArgs e)
{
DataContext = model2;
}
private void view3_clicked(object sender, RoutedEventArgs e)
{
DataContext = model3;
}
}
DataContext
. For example:partial class MainWindow : Window
{
private bool showSomeControl = false;
private void view1_clicked(object sender, RoutedEventArgs e)
{
showSomeControl = condition;
DataContext = new view1model1();
}
private void view2_clicked(object sender, RoutedEventArgs e)
{
condition = showSomeControl;
DataContext = new viewmodel2();
}
private void view3_clicked(object sender, RoutedEventArgs e)
{
showSomeControl = condition;
DataContext =new view1model3();
}
Note that the above is based on the code you posted. There's not enough detail in the question, so you'll have to infer from the examples above what precisely the actual changes in your own code will look like.
Also, much more important is to note that more generally, based on the code you posted I'd say you are misusing WPF and are going to find it a lot harder to get things working. Instead of subscribing directly to the Click
event, for example, it would be better to abstract the operations as an ICommand
and update the state in the command's handler. Similarly, instead of modifying the Visibility
property directly in the Classification
class, the control's visibility should be bound directly to the view model property that controls it. Use a DataTrigger
or IValueConverter
to map the view model property value to an actual Visibility
value.
Upvotes: 1