C# 更新依赖项属性/AttachedProperty中的普通属性,

C# 更新依赖项属性/AttachedProperty中的普通属性,,c#,wpf,xaml,binding,dependency-properties,C#,Wpf,Xaml,Binding,Dependency Properties,我试图绑定AvalonDock的正常属性 xmlns:xcad=”http://schemas.xceed.com/wpf/xaml/avalondock" this.IsVisible=值//无效 } } private static void OnCanHideChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { ((ExtendedAnchor

我试图绑定AvalonDock的正常属性

xmlns:xcad=”http://schemas.xceed.com/wpf/xaml/avalondock"

this.IsVisible=值//无效

             }
    }
    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
,它可以将它挂接到
LayoutAccorable
上,但是
PropertyChangedCallback
从未被要求回答我发布的新问题


有什么帮助吗?

我做了,在我之前的例子中,我需要创建带有两个图像的新按钮,一个是当按钮可用时,另一个是当它被禁用时,要做到这一点,首先我创建了名为“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> 
然后我在我的项目中使用了这个新控件

<custom:MyButton ButtonCommand="{Binding DecreaseImagesCount}" ButtonLabel="ZoomIn" ActiveImage="/Images/ActiveImages/ZoomIn.png" DeactiveImage="/Images/GrayImages/ZoomIn.png" 
                                             Grid.Column="2" Margin="3,4" />


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

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


您可以找到许多实现INotifyPropertyChanged..

的示例,如果您只需从视图模型中设置该属性就足够了,那么您可以使用附加的行为。 只需创建一个新类并添加一个类似这样的附加属性(我没有真正测试它,因为我实际上手头没有AvalonDock,但您应该明白这一点):

现在,您应该能够像这样使用该行为:

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


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

我无法控制
xcad
作为一个外部dll。您能解释一下如何使用AvalonDock,我是否需要创建一个新类,该类将扩展
layoutunachrable
,然后添加一个
依赖项属性
。您可以在此用户控件中创建一个新的用户控件,在xaml中添加AvalonDock,然后添加此依赖项属性,如果您遇到任何问题,我可以发送给示例。如果您能在答案中添加示例,我将不胜感激。我理解您所做的,但我的问题是,我有一个名为
CanHide
Normal属性,我无法绑定该属性,因为它是Normal属性,如何使其
Bindable
。我不清楚从你的例子中你在哪里做了那个场景。尝试创建新的控件,在它里面使用avalondock,给这个控件命名,然后创建新的依赖属性,当它改变时,你可以修改“canHide”值,从后面的代码,从外面你将使用你的新属性,它是可绑定的,我希望你明白:)很好,我会看看它,让你知道它是否有效。谢谢。我试过像你提到的那样使用
AttachedProperty
,但是
属性ChangedCallback
从未被调用,我已经将该行为附加到
DockPanel
上,它可以工作,甚至我也附加到了AvalonDock的
DockingManager
,它可以工作,但不在
LayoutAnchorable
上有什么建议吗?
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" />
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}"/>