c# - 更新依赖属性/附加属性中的普通属性,

标签 c# wpf xaml binding dependency-properties

我正在尝试绑定(bind) 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是一个普通属性,如果尝试绑定(bind)会抛出异常

A 'Binding' can only be set on a DependencyProperty of a DependencyObject.

我的问题是,是否有可能以任何方式使普通属性覆盖DependencyProperty以使其可绑定(bind)

编辑

添加了一个继承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 ,它可以将其 Hook 到 LayoutAnchorablePropertyChangedCallback 永远不会被调用 click here对于我发布的新问题。

大家有什么帮助吗?

最佳答案

我之前做了一个例子,在我的例子中,我需要创建一个带有 2 个图像的新按钮,一个在按钮可用时,另一个在禁用时,为此,我首先创建了名为“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" />

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

关于c# - 更新依赖属性/附加属性中的普通属性,,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32317685/

相关文章:

c# - 隐藏 Windows Phone 8.1 命令栏

c# - 在 Windows Phone 8.1 中制作瘦 BottomAppBar

C#:在不相关的类之间共享属性、事件和方法

c# - ListBox 或 ListView 中类似对话的 View

c# - 无法使用 PostgreSQL 配置 AspNet.Identity

wpf - 如何更改 DataGridComboBoxColumn 中单元格值的前景色?

c# - 单元测试 - 如何测试仅插入日志消息的 `void` 方法 (Serilog)

wpf - 在 WPF 中创建类似 Firefox 4 的菜单

c# - WPF绑定(bind)(使用触发器)不能从文本框工作到标签

xaml - Windows 10 UWP 中具有自定义附加属性的自适应触发器