Click here to Skip to main content
15,878,959 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Okay, I'm getting highly frustrated right now, and keep in mind that I am brand new to WPF and binding so..

I have a

Datagrid (that is within a tabcontrol). Within the Datagrid is an expander. I have a checkbox at the top of the Window called chkExpandAll. I want to bind the IsExpanded of the expander to the IsChecked of the checkbox. In other words I wish to expand all of the expanders in the datagrid when I select the checkbox.

In my Windows Resources section I have:

XML
<local:BooleanToVisibilityConverter x:Key="boolConvertNormal" />


The checkbox is listed as:

XML
<CheckBox Content="Expand All" Name="chkExpandAll" VerticalAlignment="Center" Margin="5,0,5,0" />


And then on my expander I have this:

XML
<Expander Header="{Binding}" IsExpanded="{Binding IsChecked, ElementName=chkExpandAll, Converter={StaticResource ResourceKey=boolConvertNormal}, Mode=TwoWay}"  >


When I run the app and then click the checkbox nothing happens.

Oh, and here is the code for the resource:

C#
public sealed class BooleanToVisibilityConverter : IValueConverter
   {
       public bool IsReversed { get; set; }
       public bool UseHidden { get; set; }
       public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
       {
           var val = System.Convert.ToBoolean(value, CultureInfo.InvariantCulture);
           if (this.IsReversed)
           {
               val = !val;
           }
           if (val)
           {
               return Visibility.Visible;
           }
           return this.UseHidden ? Visibility.Hidden : Visibility.Collapsed;
       }
       public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
       {
           throw new NotImplementedException();
       }
   }
Posted
Comments
[no name] 18-Jul-12 13:02pm    
First thought, try taking IsChecked, out of your binding. Does your converter run?
r83h 18-Jul-12 14:00pm    
How do I take the IsChecked out? I removed it to where it now says "IsExpanded={Binding ElementName=chkExpandAll..........} If I do this I get "Provide value on 'System.Windows.Data.Binding' threw an exception"
[no name] 18-Jul-12 14:43pm    
Sorry too much heroin in my coffee. try IsExpanded="{Binding ElementName=chkExpandAll, Path=IsChecked, Converter={StaticResource ResourceKey=boolConvertNormal}, Mode=TwoWay}"
r83h 18-Jul-12 14:48pm    
Ah, I prefer bath salts in my coffee, At any rate I tried what you said and no the converter is not getting executed when I click the checkbox.
[no name] 18-Jul-12 15:16pm    
Okay... I could not get it to work either. Until I figured out that the checkbox was not being checked. Is yours? The expander was overlaying the checkbox so that the checkbox was never getting checked. So make sure that the checkbox is actually outside of the expander boundary.

1 solution

Well I double checked that the checkbox was getting checked, and for the life of me I could not figure out why it wasn't working. So what I did instead was created a new property on my viewmodel called Expanded and then on the Checked/Unchecked events of the checkbox I set the property of the viewmodel's to true/false, I then bound the IsExpanded of the expander to the viewmodel property.

XML
<expander header="{Binding}" isexpanded="{Binding Path=isRowExpanded}"></expander>


C#
public bool isRowExpanded
        {
            get;
            set;
        }


Honestly I don't like this solution and I feel it will be to slow as I have to reiterate over the rows and update the property. But it works for now so I'm leaving it.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900