如何将附加依赖项 属性 的值设置为 属性?

How to set value to property from attached dependency property?

我正在尝试在文本块中显示树视图的选定项目。这是我的 XAML 代码

<Style TargetType="{x:Type TreeViewItem}">
   <Style.Triggers>
     <Trigger Property="IsSelected" Value="true">
       <Setter Property="vm:HLViewModel.SelectedNode" Value="{Binding ElementName="tree",Path=SelectedItem}"/>
     </Trigger>
   </Style.Triggers>
</Style>

这是我试图显示所选项目的文本块

<TextBlock Text="{Binding myText}"/>

我已经创建了附加依赖项属性,它将在触发 Treeview 的 IsSelected 属性 时设置。如何在回调函数中设置 myText 的值?

public class HLViewModel : DependencyObject
    {
        public myText{get;set;}

        public static object GetSelectedNode(DependencyObject obj)
        {
            return (object)obj.GetValue(SelectedNodeProperty);
        }

        public static void SetSelectedNode(DependencyObject obj, object value)
        {
            obj.SetValue(SelectedNodeProperty, value);
        }

        public static readonly DependencyProperty SelectedNodeProperty =
            DependencyProperty.RegisterAttached("SelectedNode", typeof(object), typeof(HLViewModel), new PropertyMetadata("def",SelectedNode_changed));

        private static void SelectedNode_changed(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            // wanna set of myText property value here
        }

下面是让视图模型使用 TreeView 中选定项目的简单方法:

XAML:

    <TreeView
        x:Name="MyTreeView"
        SelectedItemChanged="MyTreeView_SelectedItemChanged"

代码隐藏:

private void MyTreeView_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
{
    (DataContext as MyViewModel).SelectedRoomLevelItem = e.NewValue;
}

这是做同样事情的更复杂的方法。此示例演示如何在 WPF 中使用附加属性。注意 setting TreeViewAttached.SelectedItem 不会设置树视图的选中项——如果你想这样做,这是可行的,但很麻烦。所有这些附加属性的作用是允许您编写绑定, receive 选择时,从树视图中选择的项目。

public static class TreeViewAttached
{
    #region TreeViewAttached.SelectedItem Attached Property
    public static Object GetSelectedItem(TreeView obj)
    {
        return (Object)obj.GetValue(SelectedItemProperty);
    }

    public static void SetSelectedItem(TreeView obj, Object value)
    {
        obj.SetValue(SelectedItemProperty, value);
    }

    public static readonly DependencyProperty SelectedItemProperty =
        DependencyProperty.RegisterAttached("SelectedItem", typeof(Object), typeof(TreeViewAttached),
            new FrameworkPropertyMetadata(null) {
                BindsTwoWayByDefault = true
            });
    #endregion TreeViewAttached.SelectedItem Attached Property

    #region TreeViewAttached.MonitorSelectedItem Attached Property
    public static bool GetMonitorSelectedItem(TreeView obj)
    {
        return (bool)obj.GetValue(MonitorSelectedItemProperty);
    }

    public static void SetMonitorSelectedItem(TreeView obj, bool value)
    {
        obj.SetValue(MonitorSelectedItemProperty, value);
    }

    public static readonly DependencyProperty MonitorSelectedItemProperty =
        DependencyProperty.RegisterAttached("MonitorSelectedItem", typeof(bool), typeof(TreeViewAttached),
            new PropertyMetadata(false, MonitorSelectedItem_PropertyChanged));

    private static void MonitorSelectedItem_PropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        if ((bool)e.NewValue)
        {
            (d as TreeView).SelectedItemChanged += TreeViewAttached_SelectedItemChanged;
        }
        else
        {
            (d as TreeView).SelectedItemChanged -= TreeViewAttached_SelectedItemChanged;
        }
    }

    private static void TreeViewAttached_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
    {
        SetSelectedItem(sender as TreeView, e.NewValue);
    }
    #endregion TreeViewAttached.MonitorSelectedItem Attached Property
}

XAML:

<TreeView
    local:TreeViewAttached.MonitorSelectedItem="True"
    local:TreeViewAttached.SelectedItem="{Binding SelectedRoomLevelItem}"
    ItemsSource="{Binding Items}"
    >
    <TreeView.ItemTemplate>
        <HierarchicalDataTemplate ItemsSource="{Binding Items}">
            <Label Content="{Binding HeaderText}" />
        </HierarchicalDataTemplate>
    </TreeView.ItemTemplate>
</TreeView>
<Label Content="{Binding Path=SelectedRoomLevelItem.HeaderText}" />

我的示例使用具有 ItemsHeaderText 属性的快速树视图项目数据上下文 class。您对该代码的使用必须适应项目中的特定视图模型 classes。