2

We can attach DockPanel.Dock to an Expander but we can't attach ToggleButton.IsChecked. Why?

<Expander DockPanel.Dock='Bottom'> <!--Compile-->
<Expander ToggleButton.IsChecked='True'> <!--Doesn't compile-->

I found the answer in the source:

From ToggleButton:

public static readonly DependencyProperty IsCheckedProperty =
            DependencyProperty.Register(
                    "IsChecked",
                    typeof(bool?),
                    typeof(ToggleButton),
                    new FrameworkPropertyMetadata(
                            BooleanBoxes.FalseBox,
                            FrameworkPropertyMetadataOptions.BindsTwoWayByDefault | FrameworkPropertyMetadataOptions.Journal,
                            new PropertyChangedCallback(OnIsCheckedChanged)));

Form DockPanel:

public static readonly DependencyProperty DockProperty =
            DependencyProperty.RegisterAttached(
                    "Dock", 
                    typeof(Dock), 
                    typeof(DockPanel),
                    new FrameworkPropertyMetadata(
                        Dock.Left, 
                        new PropertyChangedCallback(OnDockChanged)),
                    new ValidateValueCallback(IsValidDock));

Dock is registered with RegisterAttached method instead of Register.

jchristin
  • 7,352
  • 2
  • 15
  • 18
  • Are you trying to use an external toggle button to expand the Expander or are you simply trying to expand the Expander by default? – Isaiah Nelson May 25 '16 at 17:35
  • I am simply trying to add a "IsChecked" property to this expander. It is not intended to be bound to the "IsExpanded" property. – jchristin May 26 '16 at 16:51
  • It is unclear why you would want to do that since the Expander has a `ToggleButton` and it sets its own state by its very own `IsChecked` property as apart of `ToggleButton` `ControlTemplate`. Cognition's answer below answers your question. – Isaiah Nelson May 26 '16 at 16:58
  • I would like to have an `Expander` with `CheckBox` in the Header and I would like the state of this checkbox to be accessed from the `Expander` itself as an attached property. The `ToggleButton` you are referring is the one that control the `IsExanded` state. – jchristin May 26 '16 at 17:08

2 Answers2

3

IsChecked is not an attachable property. If you are looking to bind a ToggleButton and a Expander you can do the following:

<ToggleButton x:Name="toggle" IsChecked="True" />
<Expander IsExpanded="{Binding ElementName=toggle, Path=IsChecked}" />
  • I really don't see the point of the external ToggleButton. Expander already has one. Why not just declare and set `` ? I am hoping the OP understand this. – Isaiah Nelson May 25 '16 at 17:43
  • 1
    @IsaiahNelson In most cases the Expanders IsExpanded property should be sufficient. I'm supposing OP is looking for something different. – PlantPorridge May 25 '16 at 17:46
  • Then my question would be: What make "Dock" attachable? From source code point of view the two properties (Dock and IsChecked) seems similar. I tried to explain my intention in the comment of the question. – jchristin May 26 '16 at 17:12
0

Both are dependency properties but, as you pointed out, one is Register and one is RegisterAttached. That's the difference and why IsChecked can't be exposed onto other controls. See the different answers (especially Haris Hasan's) in this SO Post. for a better explanation of the differences between the two registration methods.

As per your comment above, if you just want a CheckBox to be added to the Expander (the Header) in addition to the ToggleButton that already exists then simply do this:

 <Expander>
    <Expander.Header>
        <CheckBox VerticalAlignment="Center" IsChecked="{Binding MyBooleanPropertySomewhere, Mode=TwoWay}"/>
    </Expander.Header>
</Expander>

Then there is no reason to have it exposed on the Expander - you can set or bind IsChecked right there.

Hope this helped some.

Community
  • 1
  • 1
Isaiah Nelson
  • 2,450
  • 4
  • 34
  • 53
  • I wanted to put the `CheckBox` for all `Expander` of a specific style key. So I wanted to bind `IsChecked` to the mentioned attached property. – jchristin May 26 '16 at 18:46