Click here to Skip to main content
       

Silverlight / WPF

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralGroupbox visibility problem in WPF after debugging and changing the combobox valuememberkarthik bandaru8 Aug '12 - 23:24 
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

GeneralRe: Groupbox visibility problem in WPF after debugging and changing the combobox value Pinmemberkarthik bandaru9 Aug '12 - 0:23 
Thanks for ur reply but i have so many items in combobox by which groupbox visibily will depend.. nearly 10 items in combobox
GeneralRe: Groupbox visibility problem in WPF after debugging and changing the combobox value PinmemberEd Hill _5_9 Aug '12 - 0:31 
Same sample but with three optins in the combo box working on visibility of two group boxes.
<StackPanel>
    <ComboBox Name="Options">
        <sys:String>One</sys:String>
        <sys:String>Two</sys:String>
        <sys:String>Three</sys:String>
    </ComboBox>
    <GroupBox Header="OneOrThree">
        <GroupBox.Style>
            <Style TargetType="GroupBox">
                <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>
            </Style>
        </GroupBox.Style>
    </GroupBox>
    <GroupBox Header="Two">
        <GroupBox.Style>
            <Style TargetType="GroupBox">
                <Setter Property="Visibility" Value="Collapsed"/>
                <Style.Triggers>
                    <DataTrigger Binding="{Binding ElementName=Options, Path=SelectedItem}" Value="Two">
                        <Setter Property="Visibility" Value="Visible"/>
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </GroupBox.Style>
    </GroupBox>
</StackPanel>

GeneralRe: Groupbox visibility problem in WPF after debugging and changing the combobox value Pinmemberkarthik bandaru9 Aug '12 - 0:42 
ok Thank you but my form contains so many groupboxes which will be changed as per selecting the combo items.. I appreciate your help. Smile | :)
 
Any other method of writing it in codebehind by using a switch case will be helpful.
GeneralRe: Groupbox visibility problem in WPF after debugging and changing the combobox value PinmemberEd Hill _5_9 Aug '12 - 0:56 
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.
<StackPanel>
    <StackPanel.Resources>
        <Style TargetType="GroupBox" x:Key="OneAndThree">
            <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>
        </Style>
        <Style TargetType="GroupBox" x:Key="Two">
            <Setter Property="Visibility" Value="Collapsed"/>
            <Style.Triggers>
                <DataTrigger Binding="{Binding ElementName=Options, Path=SelectedItem}" Value="Two">
                    <Setter Property="Visibility" Value="Visible"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </StackPanel.Resources>
    <ComboBox Name="Options">
        <sys:String>One</sys:String>
        <sys:String>Two</sys:String>
        <sys:String>Three</sys:String>
    </ComboBox>
    <GroupBox Name="group1" Header="OneOrThree" Style="{StaticResource OneAndThree}">
    </GroupBox>
    <GroupBox Name="group2" Header="Two" Style="{StaticResource Two}">
        <!--<GroupBox.Style>
            
        </GroupBox.Style>-->
    </GroupBox>
</StackPanel>

GeneralRe: Groupbox visibility problem in WPF after debugging and changing the combobox value PinmemberEd Hill _5_9 Aug '12 - 0:47 
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:
<StackPanel>
    <ComboBox Name="Options" SelectionChanged="SelectionChanged">
        <sys:String>One</sys:String>
        <sys:String>Two</sys:String>
        <sys:String>Three</sys:String>
    </ComboBox>
    <GroupBox Name="group1" Header="OneOrThree">
        
    </GroupBox>
    <GroupBox Name="group2" Header="Two">
 
    </GroupBox>
</StackPanel>
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
GeneralRe: Groupbox visibility problem in WPF after debugging and changing the combobox value Pinmemberkarthik bandaru9 Aug '12 - 0:50 
thank u .. i will try and reply u
GeneralRe: Groupbox visibility problem in WPF after debugging and changing the combobox value Pinmemberkarthik bandaru9 Aug '12 - 2:10 
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.
GeneralRe: Groupbox visibility problem in WPF after debugging and changing the combobox value [modified] PinmemberEd Hill _5_9 Aug '12 - 2:24 
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.

modified 9 Aug '12 - 8:32.

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   


Advertise | Privacy | Mobile
Web02 | 2.6.130523.1 | Last Updated 24 May 2013
Copyright © CodeProject, 1999-2013
All Rights Reserved. Terms of Use
Layout: fixed | fluid