【发布时间】:2015-11-25 20:50:01
【问题描述】:
我正在尝试绑定 AvalonDock 的普通属性,
xmlns:xcad="http://schemas.xceed.com/wpf/xaml/avalondock"
<xcad:LayoutAnchorable Title="Folder" CanHide="{Binding IsHideExplorerView}">
<Views:ExplorerView DataContext="{Binding ExplorerViewModel}"/>
</xcad:LayoutAnchorable>
这里CanHide是Normal property,如果尝试绑定会抛出类似的异常
A 'Binding' can only be set on a DependencyProperty of a DependencyObject.
我的问题是,有没有办法让普通属性覆盖DependencyProperty 使其变为Bindable。
编辑
添加了一个继承 LayoutAnchorable 但 PropertyChangedCallback of DependencyProperty 从不调用的类。
public class ExtendedAnchorableItem : LayoutAnchorable
{
public static readonly DependencyProperty IsCanHideProperty =
DependencyProperty.RegisterAttached("IsCanHide", typeof(bool), typeof(ExtendedAnchorableItem),
new FrameworkPropertyMetadata((bool)false,
new PropertyChangedCallback(OnCanHideChanged)));
public bool IsCanHide
{
get { return (bool)GetValue(IsCanHideProperty); }
set { SetValue(IsCanHideProperty, value);
this.IsVisible = value; // 无效。
}
}
private static void OnCanHideChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
((ExtendedAnchorableItem)d).Hide();
}
}
XAML
<xcad:LayoutAnchorablePane>
<Utility:ExtendedAnchorableItem IsCanHide="{Binding IsHideExplorer}">
<Views:ExplorerView DataContext="{Binding ExplorerViewModel}"/>
</Utility:ExtendedAnchorableItem>
</xcad:LayoutAnchorablePane>
同样,我尝试创建一个AttachedProperty,它可以将其连接到LayoutAnchorable,但PropertyChangedCallback永远不会因为我发布的新问题而被称为click here。
有帮助吗?
【问题讨论】:
标签: c# wpf xaml binding dependency-properties