更新Dependency Property /AttachedProperty中的Normal Property

本文关键字:Property 中的 Normal AttachedProperty Dependency 更新 | 更新日期: 2023-09-27 18:10:45

我正在尝试绑定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的类,但DependencyPropertyPropertyChangedCallback永远不会调用。

 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永远不会被调用,点击这里我已经发布了一个新的问题。

有什么帮助吗

更新Dependency Property /AttachedProperty中的Normal Property

我之前做过,在我的例子中,我需要创建一个新的按钮,有两个图像一个当按钮可用,另一个当它被禁用,要做到这一点,首先我创建了一个名为"MyButton"的新用户控件我的xaml是这样的

<Button ToolTip="{Binding ButtonLabel,RelativeSource={RelativeSource AncestorType=UserControl,Mode=FindAncestor},UpdateSourceTrigger=PropertyChanged}" 
                Command="{Binding ButtonCommand,RelativeSource={RelativeSource AncestorType=UserControl,Mode=FindAncestor},UpdateSourceTrigger=PropertyChanged}"
                Cursor="Hand" VerticalAlignment="Center" >
            <Button.Template>
                <ControlTemplate>
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="45"/>
                            <ColumnDefinition Width="*"/>
                        </Grid.ColumnDefinitions>
                        <Image Name="ButtonImage" IsEnabled="{Binding Path=IsEnabled,RelativeSource={RelativeSource AncestorType=Button,Mode=FindAncestor}}"  >
                            <Image.Style>
                                <Style TargetType="{x:Type Image}">
                                    <Style.Triggers>
                                        <Trigger Property="IsEnabled" Value="True">
                                            <Setter Property="Source" Value="{Binding ActiveImage,RelativeSource={RelativeSource AncestorType=UserControl,Mode=FindAncestor},UpdateSourceTrigger=PropertyChanged}"/>
                                        </Trigger>
                                        <Trigger Property="IsEnabled" Value="False">
                                            <Setter Property="Source" Value="{Binding DeactiveImage,RelativeSource={RelativeSource AncestorType=UserControl,Mode=FindAncestor},UpdateSourceTrigger=PropertyChanged}"/>
                                        </Trigger>
                                    </Style.Triggers>
                                </Style>
                            </Image.Style>
                        </Image>
                        <Label Name="LabelContent" Content="{Binding ButtonLabel,RelativeSource={RelativeSource AncestorType=UserControl,Mode=FindAncestor},UpdateSourceTrigger=PropertyChanged}" 
                       Grid.Column="1" IsEnabled="{Binding Path=IsEnabled,RelativeSource={RelativeSource AncestorType=Button,Mode=FindAncestor}}" VerticalContentAlignment="Center"  />
                    </Grid>
                </ControlTemplate>
            </Button.Template>
        </Button> 

然后我添加了ActiveImage和DeactiveImage的依赖属性使用以下代码

public static  DependencyProperty activeImage =
           DependencyProperty.Register("ActiveImage", typeof(type of this property like  "string"), typeof(type of the custom control that you need like  "MyButton"), new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault));
        public string ActiveImage
        {
            get { return (string)GetValue(activeImage); }
            set { SetValue(activeImage, value); }
        }

然后在我的项目

中使用这个新控件
<custom:MyButton ButtonCommand="{Binding DecreaseImagesCount}" ButtonLabel="ZoomIn" ActiveImage="/Images/ActiveImages/ZoomIn.png" DeactiveImage="/Images/GrayImages/ZoomIn.png" 
                                             Grid.Column="2" Margin="3,4" />

注意我现在可以绑定Button Image的路径了

如果仅仅从视图模型设置该属性就足够了,那么您可以使用附加行为。只需创建一个新类,并添加一个附加属性,就像这样(我没有真正测试这个,因为我实际上没有AvalonDock在手,但你应该得到的想法):

public class YourBehavior
{
    public static readonly DependencyProperty YourCanHideProperty = DependencyProperty.RegisterAttached(
                "YourCanHide",
                typeof(bool),
                typeof(LayoutAnchorable),
                new PropertyMetadata(YourCanHidePropertyChanged));
    private static void YourCanHidePropertyChanged(
        DependencyObject dependencyObject,
        DependencyPropertyChangedEventArgs e)
    {
        LayoutAnchorable control = dependencyObject as LayoutAnchorable;
        if (control != null)
        {
            control.CanHide = e.NewValue as bool;
        }
    }
    public static bool GetYourCanHideProperty(LayoutAnchorablewindow)
    {
        return window.GetValue(YourProperty) as bool?;
    }
    public static void SetYourCanHideProperty(LayoutAnchorable control, bool value)
    {
        window.SetValue(YourProperty, value);
    }
}

现在你应该可以使用这样的行为:

<xcad:LayoutAnchorable Title="Folder" namespacealias:YourBehavior.YourCanHideProperty="{Binding IsHideExplorerView}"/>

如果你想让它在两个方向上工作,只需查看附件的混合行为。

是的,你可以做到。您需要实现INotifypropertyChanged接口,并在属性setter中引发propertychanged事件。在将属性更改为DependencyProperty之后,您将获得通知机制,因此属性更改将传播到目标(在本例中为xcad)。

你可以找到很多实现INotifyPropertyChanged的例子。