简体   繁体   English

如何从WPF中的目标类型访问DataTemplate项目

[英]How to access DataTemplate item from target type in wpf

<TreeView x:Name="foldersItem">
        <TreeView.Resources>
            <Style TargetType="{x:Type TreeViewItem}">
                <Setter Property="HeaderTemplate">
                    <Setter.Value>
                        <DataTemplate>
                            <StackPanel Orientation="Horizontal">
                                <CheckBox Name="cbItem"></CheckBox>
                                <TextBlock Text="{Binding}" Margin="5,0" />
                            </StackPanel>
                        </DataTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </TreeView.Resources>
    </TreeView>

Basically what I have in the code above, is a TreeView, and each TreeViewItem will have a check box and a text block. 基本上我上面的代码中有一个TreeView,每个TreeViewItem将具有一个复选框和一个文本块。 How can I access, to check or uncheck, the checkbox for each TreeViewItem? 如何访问,选择或取消选中每个TreeViewItem的复选框? I'm guessing I'll need some sort of binding, but I can't wrap my mind around what or how. 我猜我将需要某种绑定,但是我无法决定要做什么或如何绑定。 The end result should have a Windows Forms type of TreeView with the checkboxes set to true false or null respectively. 最终结果应为Windows窗体类型的TreeView,其复选框分别设置为true false或null。

If I'm going about this all wrong, please, let me know. 如果我要解决所有这些错误,请告诉我。 If you need any more information, I'd be happy to provide. 如果您需要更多信息,我们将很乐意为您提供。

You can set or get the value of CheckBox by a typical data binding pattern for ItemsControl. 您可以通过ItemsControl的典型数据绑定模式设置或获取CheckBox的值。 The following is a minimum example. 以下是最小示例。

Code behind: 后面的代码:

using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Windows;

namespace WpfApplication1
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        public ObservableCollection<ItemViewModel> Items { get; } =
            new ObservableCollection<ItemViewModel>
            {
                new ItemViewModel { Name = "Item1", IsChecked = null },
                new ItemViewModel { Name = "Item2", IsChecked = true },
                new ItemViewModel { Name = "Item3", IsChecked = false }
            };
    }

    public class ItemViewModel : INotifyPropertyChanged
    {
        public string Name { get; set; }

        private bool? _isChecked;
        public bool? IsChecked
        {
            get { return _isChecked; }
            set
            {
                _isChecked = value;
                OnPropertyChanged(nameof(IsChecked));
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
        private void OnPropertyChanged(string propertyName) =>
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}

Xaml: Xaml:

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        x:Name="WindowRoot"
        Title="MainWindow" Height="300" Width="400">
    <Grid>
        <TreeView ItemsSource="{Binding ElementName=WindowRoot, Path=Items}">
            <TreeView.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal">
                        <CheckBox IsChecked="{Binding IsChecked}"/>
                        <TextBlock Text="{Binding Name}"/>
                    </StackPanel>
                </DataTemplate>
            </TreeView.ItemTemplate>
        </TreeView>
    </Grid>
</Window>

Access IsChecked property of ItemViewModel to set or get the value of CheckBox. 访问ItemViewModel的IsChecked属性以设置或获取CheckBox的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM