Click here to Skip to main content
12,754,391 members (32,611 online)
Rate this:
Please Sign up or sign in to vote.
See more: WPF checkbox Binding
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:

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

The checkbox is listed as:

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

And then on my expander I have this:

<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:

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 18-Jul-12 7:35am
Wes Aday 18-Jul-12 13:02pm
First thought, try taking IsChecked, out of your binding. Does your converter run?
rchadhaney 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"
Wes Aday 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}"
rchadhaney 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.
Wes Aday 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

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

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.

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

public bool isRowExpanded

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.

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

    Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web01 | 2.8.170217.1 | Last Updated 18 Jul 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100