Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. WPF
  4. Using a static class to call child Usercontrols functions from a parent window button click event

Using a static class to call child Usercontrols functions from a parent window button click event

Scheduled Pinned Locked Moved WPF
5 Posts 3 Posters 6 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Stephen Holdorf
    wrote on last edited by
    #1

    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;
    }

    L 1 Reply Last reply
    0
    • S Stephen Holdorf

      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;
      }

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      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.

      S I 2 Replies Last reply
      0
      • L Lost User

        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.

        S Offline
        S Offline
        Stephen Holdorf
        wrote on last edited by
        #3

        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"/>
        
        L 1 Reply Last reply
        0
        • S Stephen Holdorf

          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"/>
          
          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          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.

          1 Reply Last reply
          0
          • L Lost User

            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 Offline
            I Offline
            incipire
            wrote on last edited by
            #5

            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

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups