Using a static class to call child Usercontrols functions from a parent window button click event
-
I have a situation where I am trying to change the visibility of StackPanels in a child UserControl from a button in the parent window. I created a custom class below that holds a static reference to the UserControl value and in the UserControl View constructor the user control value is assigned to the static EditWindow class uc static value. Now what I am trying to do is hide and show StackPanels in the child UserControl view the button in the parent window is clicked. When the button is clicked the StackPanels are not changing from Collapsed to Visible. All of the code is below: EditWindow.cs
public static class EditWindow
{
public static string buttonString;
public static Button button;// This is the UserControl public static View.ApplicationInfoView uc;
}
// UserControl as a View in the parent window:
ApplicationInfoView StackPanels
// UserControl - ApplicationInfoView in the constructor
EditWindow.uc = this;
// Button Click event in the Parent Window
private void btnEdit_Click(object sender, RoutedEventArgs)
{
EditWindow.uc.editAppInfo();
}// editAppInfo method in the UserControl - //ApplicationInfoView
editAppInfo()
{
EditWindow.uc.sp1.Visibility = Visibility.Collapsed;
EditWindow.uc.sp2.Visibility = Visibility.Visible;
EditWindow.uc.sp3.Visibility = Visibility.Visibile;
} -
I have a situation where I am trying to change the visibility of StackPanels in a child UserControl from a button in the parent window. I created a custom class below that holds a static reference to the UserControl value and in the UserControl View constructor the user control value is assigned to the static EditWindow class uc static value. Now what I am trying to do is hide and show StackPanels in the child UserControl view the button in the parent window is clicked. When the button is clicked the StackPanels are not changing from Collapsed to Visible. All of the code is below: EditWindow.cs
public static class EditWindow
{
public static string buttonString;
public static Button button;// This is the UserControl public static View.ApplicationInfoView uc;
}
// UserControl as a View in the parent window:
ApplicationInfoView StackPanels
// UserControl - ApplicationInfoView in the constructor
EditWindow.uc = this;
// Button Click event in the Parent Window
private void btnEdit_Click(object sender, RoutedEventArgs)
{
EditWindow.uc.editAppInfo();
}// editAppInfo method in the UserControl - //ApplicationInfoView
editAppInfo()
{
EditWindow.uc.sp1.Visibility = Visibility.Collapsed;
EditWindow.uc.sp2.Visibility = Visibility.Visible;
EditWindow.uc.sp3.Visibility = Visibility.Visibile;
}If the StackPanels are "empty", toggling visibility will make no difference unless you use an explicit minimum height, width and / or color in order to "see" them. Also, the layout (right or wrong) impacts what one sees (in-back versus in-front; etc); regardless of what the "code-behind" is doing.
-
If the StackPanels are "empty", toggling visibility will make no difference unless you use an explicit minimum height, width and / or color in order to "see" them. Also, the layout (right or wrong) impacts what one sees (in-back versus in-front; etc); regardless of what the "code-behind" is doing.
I have now tried changing the way I am performing my task. Now in my child user control constructor I am loading the UserControl as follows: // Remember EditWindow is a static class and uc is a static // public value of View.ApplicationInfoView which is the // UserControl that is a View with a base class of // UserControl. EditWindow.uc = this; And now calling the method the following on the parent window button click event: // Remember the StackPanel spGrid is Collapsed onload private void btn_click(object sender, RoutingEventArgs e) { EditWindow.uc.spGrid.Visibility = Visibility.Visible; } Now Here is my StackPanel on the child UserControl:
<Label Height="36" Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="2" FontSize="16" Background="#FF004A78" Content="Identifying Documents" FontWeight="Bold" Foreground="White" Margin="0,0,0,0"/>
-
I have now tried changing the way I am performing my task. Now in my child user control constructor I am loading the UserControl as follows: // Remember EditWindow is a static class and uc is a static // public value of View.ApplicationInfoView which is the // UserControl that is a View with a base class of // UserControl. EditWindow.uc = this; And now calling the method the following on the parent window button click event: // Remember the StackPanel spGrid is Collapsed onload private void btn_click(object sender, RoutingEventArgs e) { EditWindow.uc.spGrid.Visibility = Visibility.Visible; } Now Here is my StackPanel on the child UserControl:
<Label Height="36" Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="2" FontSize="16" Background="#FF004A78" Content="Identifying Documents" FontWeight="Bold" Foreground="White" Margin="0,0,0,0"/>
This is one of those situations where the XAML should be "re-considered". (The "depth" of this simple visual tree is sure ... "deep"...) Even your top "stack panel" is imbedded in a Grid; so you've still not got the whole picture. State what you're trying to do from a "user" / functional point-of-view, and you'll probably get some good design answers to get you off with a less "complicated" (XAML) design / approach.
-
If the StackPanels are "empty", toggling visibility will make no difference unless you use an explicit minimum height, width and / or color in order to "see" them. Also, the layout (right or wrong) impacts what one sees (in-back versus in-front; etc); regardless of what the "code-behind" is doing.
The appropriate way to do this is to create a Tunnelling Routed Event that the Child User control listens to. And make that event fire when you click the button on the parent control. This article on MSDN explains how to declare a Routed Event