如何将属性绑定到 WPF 中 Treeview 中的选定节点 [英] How to bind the property to the selected node in the Treeview in WPF

查看:67
本文介绍了如何将属性绑定到 WPF 中 Treeview 中的选定节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在MVVM中将自定义属性绑定到选中节点的IsSelected属性上,我已在运行时将节点加载到树视图中.我正在使用 MVVM

How to bind the custom property to the selected node's IsSelected property in MVVM, I have loaded the nodes at runtime into the treeview. I am using MVVM

推荐答案

这是一个小例子:

public abstract class ViewModel : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    protected void OnPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

public class MyViewModel : ViewModel
{
    public ObservableCollection<Item> Items
    {
        get
        {
            return new ObservableCollection<Item>()
            {
                new Item() {DisplayValue = "Item1", IsSelected = false, Sample = "Sample: I am Item1"},
                new Item() {DisplayValue = "Item2", IsSelected = true, Sample = "Sample: I am Item2"},
                new Item() {DisplayValue = "Item3", IsSelected = false, Sample = "Sample: I am Item3"}
            };
        }
    }
}

public class Item : ViewModel
{
    public string DisplayValue { get; set; }

    private bool _isSelected = false;

    public bool IsSelected
    {
        get
        {
            return _isSelected;
        }
        set
        {
            _isSelected = value;
            OnPropertyChanged("IsSelected");
        }
    }

    private string _sample;

    public string Sample
    {
        get
        {
            return _sample;
        }
        set
        {
            _sample = value;
            OnPropertyChanged("Sample");
        }
    }
}

XAML:

<Window x:Class="WpfApplication93.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:WpfApplication93"
        Title="MainWindow" Height="350" Width="525">
  <Grid>
    <Grid.DataContext>
      <local:MyViewModel></local:MyViewModel>
    </Grid.DataContext>

    <StackPanel>
      <TreeView x:Name="myTreeView" ItemsSource="{Binding Items}">
        <TreeView.ItemContainerStyle>
          <Style TargetType="{x:Type TreeViewItem}">
            <Setter Property="IsSelected" Value="{Binding IsSelected}"></Setter>
            <Setter Property="Template">
              <Setter.Value>
                <ControlTemplate TargetType="TreeViewItem">
                  <TextBlock Text="{Binding DisplayValue}"></TextBlock>
                </ControlTemplate>
              </Setter.Value>
            </Setter>

            <Style.Triggers>
              <Trigger Property="IsSelected" Value="True">
                <!-- if you want to customize the appearance of a selected element do it here -->
                <Setter Property="FontWeight" Value="Bold" />
              </Trigger>
            </Style.Triggers>
          </Style>
        </TreeView.ItemContainerStyle>
      </TreeView>

      <Label Content="{Binding ElementName=myTreeView, Path=SelectedItem.Sample}"></Label>
    </StackPanel>
  </Grid>
</Window>

这篇关于如何将属性绑定到 WPF 中 Treeview 中的选定节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆