【问题标题】:Expander.IsExpanded Binding breaking after first clickExpander.IsExpanded 绑定在第一次单击后中断
【发布时间】:2014-06-30 20:12:30
【问题描述】:

我可能正在做某事,但无法弄清楚我的问题。任何帮助将不胜感激:

我有一个名为 Section 的 CustomControl。 Section 是可折叠的,因此它的 ControlTemplate 包含一个 Expander。 Expanders IsExpanded-Property 通过 TemplateBinding 绑定到 Section 的 IsExpanded 属性。

在 Section 上设置 IsExpanded 时,Expander 会折叠,但使用 Expander 中的 toggleButton 似乎会破坏该绑定。可能通过为 Expander 的 IsExpanded-Property 设置一个本地值。无论如何,在通过鼠标更改扩展器状态后,绑定中断并设置部分的 IsExpanded 不会做任何事情。

但是,当将 Expander 放入视图并将其 IsExpanded-Property 绑定到视图中的某个 DP 时,不会发生这种情况。

同样值得注意的是:Snoop 没有在 Expander 的 IsExpanded-Property 上显示任何绑定。它只显示 Value-Source 是 ParentTemplate。当我单击 ToggleButton 以更改 IsExpanded 时,Value-Source 更改为 Local(可能会破坏以前的 Binding?)

Section.cs:

public class Section : Control
{
    static Section()
    {
        DefaultStyleKeyProperty.OverrideMetadata(typeof(Section), new FrameworkPropertyMetadata(typeof(Section))); 
    }

    public static readonly DependencyProperty IsExpandedProperty = DependencyProperty.Register(
            "IsExpanded", typeof (bool), typeof (Section), new PropertyMetadata(default(bool)));

    public bool IsExpanded
    {
        get { return (bool) GetValue(IsExpandedProperty); }
        set { SetValue(IsExpandedProperty, value); }
    }
}

Generic.xaml 样式:

<Style TargetType="{x:Type local:Section}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type local:Section}">
                    <Expander Header="Test" IsExpanded="{TemplateBinding IsExpanded}" >
                        <Rectangle Fill="Aqua" Height="200" Width="200" />
                    </Expander>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

有什么想法吗?

【问题讨论】:

    标签: wpf wpf-controls expander


    【解决方案1】:

    所以我找到了答案,实际上是非常基本的知识:

    无论元数据状态如何,TemplateBindings 始终是 ONEWAY...

    使用:

    IsExpanded="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=IsExpanded}"
    

    解决了我的问题...

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-11-05
      • 1970-01-01
      • 2017-05-17
      • 1970-01-01
      • 2013-06-07
      • 2019-12-16
      • 1970-01-01
      • 2011-06-08
      相关资源
      最近更新 更多