Groupbox visibility problem in WPF after debugging and changing the combobox value
-
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
-
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
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>
-
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>
Thanks for ur reply but i have so many items in combobox by which groupbox visibily will depend.. nearly 10 items in combobox
-
Thanks for ur reply but i have so many items in combobox by which groupbox visibily will depend.. nearly 10 items in combobox
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>
-
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>
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.
-
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
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
-
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
thank u .. i will try and reply u
-
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.
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
-
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
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.
-
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.
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.