【发布时间】: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