简体   繁体   English

为什么我不能将ToggleButton.IsChecked属性附加到扩展器

[英]Why can't I attach ToggleButton.IsChecked property to an Expander

We can attach DockPanel.Dock to an Expander but we can't attach ToggleButton.IsChecked. 我们可以将DockPanel.Dock附加到Expander,但是不能附加ToggleButton.IsChecked。 Why? 为什么?

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

I found the answer in the source: 我在源代码中找到了答案:

From ToggleButton : 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 : 表格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 . Dock是使用RegisterAttached方法而不是Register

IsChecked is not an attachable property. IsChecked不是可附加属性。 If you are looking to bind a ToggleButton and a Expander you can do the following: 如果要绑定ToggleButton和Expander,可以执行以下操作:

<ToggleButton x:Name="toggle" IsChecked="True" />
<Expander IsExpanded="{Binding ElementName=toggle, Path=IsChecked}" />

Both are dependency properties but, as you pointed out, one is Register and one is RegisterAttached. 两者都是依赖项属性,但是,正如您所指出的,一个是Register,另一个是RegisterAttached。 That's the difference and why IsChecked can't be exposed onto other controls. 这就是区别,为什么不能将IsChecked公开给其他控件。 See the different answers (especially Haris Hasan's) in this SO Post. 在此SO Post中查看不同的答案(尤其是Haris Hasan的答案) 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: 根据上面的评论,如果您只想将CheckBox除了已经存在的ToggleButton之外添加到Expander(标题)中,则只需执行以下操作:

 <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. 这样就没有理由在Expander上公开它了-您可以在此处设置或绑定IsChecked。

Hope this helped some. 希望这对一些人有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM