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
  1. Home
  2. General Programming
  3. WPF
  4. Groupbox visibility problem in WPF after debugging and changing the combobox value

Groupbox visibility problem in WPF after debugging and changing the combobox value

Scheduled Pinned Locked Moved WPF
helpcsharpwpf
10 Posts 2 Posters 0 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.
  • K Offline
    K Offline
    karthik bandaru
    wrote on last edited by
    #1

    I need to change my groupbox visibility and I should be able to get those controls in the groupbox1 ,when I change my combobox value in the combobox selectionchange event groupbox2 and its controls should be shown.. line by line execution shows the result i was looking but finally am not able to get the output . This is what I have tried but didn't work for me. Need ur help. if(stringgp1=="group1") { groupbox1.Visibility=Visibility.Visible; groupbox2.visibility=visibility.hidden; } else(something...){}

    karthik

    E 2 Replies Last reply
    0
    • K karthik bandaru

      I need to change my groupbox visibility and I should be able to get those controls in the groupbox1 ,when I change my combobox value in the combobox selectionchange event groupbox2 and its controls should be shown.. line by line execution shows the result i was looking but finally am not able to get the output . This is what I have tried but didn't work for me. Need ur help. if(stringgp1=="group1") { groupbox1.Visibility=Visibility.Visible; groupbox2.visibility=visibility.hidden; } else(something...){}

      karthik

      E Offline
      E Offline
      Ed Hill _5_
      wrote on last edited by
      #2

      Try this sample, its not working in the code behind like your example code, but insted relys on styles bindings and triggers to achieve what you are after.

                  One
                  Two
              
              <Setter Property="Visibility" Value="Collapsed"/>
                          <Style.Triggers>
                              <DataTrigger Binding="{Binding ElementName=Options, Path=SelectedItem}" Value="One">
                                  <Setter Property="Visibility" Value="Visible"/>
                              </DataTrigger>
                          </Style.Triggers>
                      
                  
              
              <Setter Property="Visibility" Value="Collapsed"/>
                          <Style.Triggers>
                              <DataTrigger Binding="{Binding ElementName=Options, Path=SelectedItem}" Value="Two">
                                  <Setter Property="Visibility" Value="Visible"/>
                              </DataTrigger>
                          </Style.Triggers>
      
      K 1 Reply Last reply
      0
      • E Ed Hill _5_

        Try this sample, its not working in the code behind like your example code, but insted relys on styles bindings and triggers to achieve what you are after.

                    One
                    Two
                
                <Setter Property="Visibility" Value="Collapsed"/>
                            <Style.Triggers>
                                <DataTrigger Binding="{Binding ElementName=Options, Path=SelectedItem}" Value="One">
                                    <Setter Property="Visibility" Value="Visible"/>
                                </DataTrigger>
                            </Style.Triggers>
                        
                    
                
                <Setter Property="Visibility" Value="Collapsed"/>
                            <Style.Triggers>
                                <DataTrigger Binding="{Binding ElementName=Options, Path=SelectedItem}" Value="Two">
                                    <Setter Property="Visibility" Value="Visible"/>
                                </DataTrigger>
                            </Style.Triggers>
        
        K Offline
        K Offline
        karthik bandaru
        wrote on last edited by
        #3

        Thanks for ur reply but i have so many items in combobox by which groupbox visibily will depend.. nearly 10 items in combobox

        E 1 Reply Last reply
        0
        • K karthik bandaru

          Thanks for ur reply but i have so many items in combobox by which groupbox visibily will depend.. nearly 10 items in combobox

          E Offline
          E Offline
          Ed Hill _5_
          wrote on last edited by
          #4

          Same sample but with three optins in the combo box working on visibility of two group boxes.

              One
              Two
              Three
          
          <Setter Property="Visibility" Value="Collapsed"/>
                      <Style.Triggers>
                          <DataTrigger Binding="{Binding ElementName=Options, Path=SelectedItem}" Value="One">
                              <Setter Property="Visibility" Value="Visible"/>
                          </DataTrigger>
                          <DataTrigger Binding="{Binding ElementName=Options, Path=SelectedItem}" Value="Three">
                              <Setter Property="Visibility" Value="Visible"/>
                          </DataTrigger>
                      </Style.Triggers>
                  
              
          
          <Setter Property="Visibility" Value="Collapsed"/>
                      <Style.Triggers>
                          <DataTrigger Binding="{Binding ElementName=Options, Path=SelectedItem}" Value="Two">
                              <Setter Property="Visibility" Value="Visible"/>
                          </DataTrigger>
                      </Style.Triggers>
          
          K 1 Reply Last reply
          0
          • E Ed Hill _5_

            Same sample but with three optins in the combo box working on visibility of two group boxes.

                One
                Two
                Three
            
            <Setter Property="Visibility" Value="Collapsed"/>
                        <Style.Triggers>
                            <DataTrigger Binding="{Binding ElementName=Options, Path=SelectedItem}" Value="One">
                                <Setter Property="Visibility" Value="Visible"/>
                            </DataTrigger>
                            <DataTrigger Binding="{Binding ElementName=Options, Path=SelectedItem}" Value="Three">
                                <Setter Property="Visibility" Value="Visible"/>
                            </DataTrigger>
                        </Style.Triggers>
                    
                
            
            <Setter Property="Visibility" Value="Collapsed"/>
                        <Style.Triggers>
                            <DataTrigger Binding="{Binding ElementName=Options, Path=SelectedItem}" Value="Two">
                                <Setter Property="Visibility" Value="Visible"/>
                            </DataTrigger>
                        </Style.Triggers>
            
            K Offline
            K Offline
            karthik bandaru
            wrote on last edited by
            #5

            ok Thank you but my form contains so many groupboxes which will be changed as per selecting the combo items.. I appreciate your help. :) Any other method of writing it in codebehind by using a switch case will be helpful.

            E 1 Reply Last reply
            0
            • K karthik bandaru

              I need to change my groupbox visibility and I should be able to get those controls in the groupbox1 ,when I change my combobox value in the combobox selectionchange event groupbox2 and its controls should be shown.. line by line execution shows the result i was looking but finally am not able to get the output . This is what I have tried but didn't work for me. Need ur help. if(stringgp1=="group1") { groupbox1.Visibility=Visibility.Visible; groupbox2.visibility=visibility.hidden; } else(something...){}

              karthik

              E Offline
              E Offline
              Ed Hill _5_
              wrote on last edited by
              #6

              if you are set on doing this in the code behind then i think i will need a better sample or your code to help you. The following is working for me:

                  One
                  Two
                  Three
              

              private void SelectionChanged(object sender, SelectionChangedEventArgs e)
              {
              var selected = (sender as ComboBox).SelectedItem as String;
              switch (selected)
              {
              case "One":
              group1.Visibility = Visibility.Visible;
              group2.Visibility = Visibility.Collapsed;
              break;
              case "Two":
              group1.Visibility = Visibility.Collapsed;
              group2.Visibility = Visibility.Visible;
              break;
              case "Three":
              group1.Visibility = Visibility.Visible;
              group2.Visibility = Visibility.Collapsed;
              break;
              default:
              group1.Visibility = Visibility.Collapsed;
              group2.Visibility = Visibility.Collapsed;
              break;
              }
              }

              It would be worth throwing some checks in on the (sender as ComboBox) etc incase you call the event from the wrong place but i'll leave that to you to sort out

              K 2 Replies Last reply
              0
              • E Ed Hill _5_

                if you are set on doing this in the code behind then i think i will need a better sample or your code to help you. The following is working for me:

                    One
                    Two
                    Three
                

                private void SelectionChanged(object sender, SelectionChangedEventArgs e)
                {
                var selected = (sender as ComboBox).SelectedItem as String;
                switch (selected)
                {
                case "One":
                group1.Visibility = Visibility.Visible;
                group2.Visibility = Visibility.Collapsed;
                break;
                case "Two":
                group1.Visibility = Visibility.Collapsed;
                group2.Visibility = Visibility.Visible;
                break;
                case "Three":
                group1.Visibility = Visibility.Visible;
                group2.Visibility = Visibility.Collapsed;
                break;
                default:
                group1.Visibility = Visibility.Collapsed;
                group2.Visibility = Visibility.Collapsed;
                break;
                }
                }

                It would be worth throwing some checks in on the (sender as ComboBox) etc incase you call the event from the wrong place but i'll leave that to you to sort out

                K Offline
                K Offline
                karthik bandaru
                wrote on last edited by
                #7

                thank u .. i will try and reply u

                1 Reply Last reply
                0
                • K karthik bandaru

                  ok Thank you but my form contains so many groupboxes which will be changed as per selecting the combo items.. I appreciate your help. :) Any other method of writing it in codebehind by using a switch case will be helpful.

                  E Offline
                  E Offline
                  Ed Hill _5_
                  wrote on last edited by
                  #8

                  I have provided you with a sample using the code behind, but i would say that it is worth getting used to all the things that you can do with WPF in the xaml files. As with any programming there are so many ways to do things, the styles i was suggesting can be created as resources and be reused easily by the group boxes. If you are using the MVVM approach what you may be looking to do is use templates to get your UI to change depending on Properties in the ViewModel. Just as a quick untested example if you are worried about having too much xmal, this is just a different appraoch. It is worth noting that the resources can be placed in different places, and the are in scope in any elements below where they are created, also they can be kept out of the way in resource dictionarys. Hopefully one of these solutions will work for you even if it is the code behind one.

                          <Setter Property="Visibility" Value="Collapsed"/>
                          <Style.Triggers>
                              <DataTrigger Binding="{Binding ElementName=Options, Path=SelectedItem}" Value="One">
                                  <Setter Property="Visibility" Value="Visible"/>
                              </DataTrigger>
                              <DataTrigger Binding="{Binding ElementName=Options, Path=SelectedItem}" Value="Three">
                                  <Setter Property="Visibility" Value="Visible"/>
                              </DataTrigger>
                          </Style.Triggers>
                      
                      <Setter Property="Visibility" Value="Collapsed"/>
                          <Style.Triggers>
                              <DataTrigger Binding="{Binding ElementName=Options, Path=SelectedItem}" Value="Two">
                                  <Setter Property="Visibility" Value="Visible"/>
                              </DataTrigger>
                          </Style.Triggers>
                      
                  
                  One
                      Two
                      Three
                  
                  1 Reply Last reply
                  0
                  • E Ed Hill _5_

                    if you are set on doing this in the code behind then i think i will need a better sample or your code to help you. The following is working for me:

                        One
                        Two
                        Three
                    

                    private void SelectionChanged(object sender, SelectionChangedEventArgs e)
                    {
                    var selected = (sender as ComboBox).SelectedItem as String;
                    switch (selected)
                    {
                    case "One":
                    group1.Visibility = Visibility.Visible;
                    group2.Visibility = Visibility.Collapsed;
                    break;
                    case "Two":
                    group1.Visibility = Visibility.Collapsed;
                    group2.Visibility = Visibility.Visible;
                    break;
                    case "Three":
                    group1.Visibility = Visibility.Visible;
                    group2.Visibility = Visibility.Collapsed;
                    break;
                    default:
                    group1.Visibility = Visibility.Collapsed;
                    group2.Visibility = Visibility.Collapsed;
                    break;
                    }
                    }

                    It would be worth throwing some checks in on the (sender as ComboBox) etc incase you call the event from the wrong place but i'll leave that to you to sort out

                    K Offline
                    K Offline
                    karthik bandaru
                    wrote on last edited by
                    #9

                    should I set all the groupboxes visible or hidden in pageload ? I have taken grid inside the groupboxes and arranged my controls like textboxes.. If I make groupboxes visible in pageload then while debugging it is showing all the controls in Groupboxes but combo selection change event not working.

                    E 1 Reply Last reply
                    0
                    • K karthik bandaru

                      should I set all the groupboxes visible or hidden in pageload ? I have taken grid inside the groupboxes and arranged my controls like textboxes.. If I make groupboxes visible in pageload then while debugging it is showing all the controls in Groupboxes but combo selection change event not working.

                      E Offline
                      E Offline
                      Ed Hill _5_
                      wrote on last edited by
                      #10

                      have you put a break point in to check that the SelectionChanged Event is being triggered and passed through to the event handler? If you don't set the vivibility explicitly in the PageLoad is the combo box working as expected? I'm not able to reproduce your problem here at all from the information you have provided, so include the WPF where you decalre your combo box, page load event and at least one group box. also include the code for your selection chanage event handler and page load event handler. Or send me your project via email(my profile > right click homepage >copy link location) email address is there for a short while.

                      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