如何从附加的依赖属性设置属性值?

时间:2017-10-05 17:46:47

标签: c# wpf xaml treeview dependency-properties

我正在尝试在textblock中显示所选的treeview项。这是我的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}"/>

我创建了附加的dependencyproperty,它将在触发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
        }

1 个答案:

答案 0 :(得分:0)

以下是让viewmodel使用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中使用附加属性。请注意,设置 TreeViewAttached.SelectedItem不会设置树视图的选定项目 - 如果您想这样做,它可行,但很麻烦。所有这些附加属性都允许您在选择更改时编写从树视图接收所选项目的绑定。

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属性的quickie treeview项目datacontext类。您对此代码的使用必须适应项目中的特定viewmodel类。