【问题标题】:Update Normal Property in Dependency property/AttachedProperty,更新依赖属性/附加属性中的普通属性,
【发布时间】: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


    【解决方案1】:

    我之前做过示例,在我的情况下,我需要创建带有 2 个图像的新按钮,一个在按钮可用时,另一个在禁用时,首先我创建了名为“MyButton”的新用户控件像这样

    <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" />
    

    请注意,我现在可以绑定按钮图像的路径

    【讨论】:

    • 你能解释一下如何继续使用 AvalonDock,我是否需要创建一个新类来扩展 LayoutAnchorable,然后添加一个 Dependency Property。
    • 您可以在此用户控件中创建一个新的用户控件,在 xaml 中添加 AvalonDock,然后添加此依赖项属性,如果您遇到任何问题,我可以发送到示例
    • 感谢您将示例添加到答案中。
    • 我明白你做了什么,但我的问题是我有一个名为CanHide 的Normal Property,我无法绑定该属性,因为它是普通属性,我怎样才能使它Bindable .我不清楚你的例子是在哪里做的。
    • 尝试创建新控件并在其中使用 avalondock 并赋予此控件名称然后创建新的依赖项属性,当它更改时,您可以从后面的代码中修改“canHide”值,在外部表单中您将使用您的可绑定的新属性,我希望你很清楚:)
    【解决方案2】:

    如果您只需从视图模型中设置该属性就足够了,那么您可以使用附加行为。 只需创建一个新类并添加一个像这样的附加属性(我没有真正测试这个,因为我实际上手头没有 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}"/>
    

    如果您想让它双向工作,只需查看附加的 Blend 行为。

    【讨论】:

    • 好的,我会看看它,让你知道它是否有效。谢谢。
    • 我已经尝试使用您提到的AttachedProperty,但是PropertyChangedCallback 永远不会被调用,我已将行为附加到DockPanel 它有效,甚至我已附加到DockingManager AvalonDock 的它可以工作,但在 LayoutAnchorable 没有任何建议?
    【解决方案3】:

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

    您可以找到很多实现 INotifyPropertyChanged 的​​示例。

    【讨论】:

    • 我无法控制 xcad 作为外部 dll。
    猜你喜欢
    • 2016-01-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-22
    • 1970-01-01
    相关资源
    最近更新 更多