簡體   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>

基本上我上面的代碼中有一個TreeView,每個TreeViewItem將具有一個復選框和一個文本塊。 如何訪問,選擇或取消選中每個TreeViewItem的復選框? 我猜我將需要某種綁定,但是我無法決定要做什么或如何綁定。 最終結果應為Windows窗體類型的TreeView,其復選框分別設置為true false或null。

如果我要解決所有這些錯誤,請告訴我。 如果您需要更多信息,我們將很樂意為您提供。

您可以通過ItemsControl的典型數據綁定模式設置或獲取CheckBox的值。 以下是最小示例。

后面的代碼:

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:

<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>

訪問ItemViewModel的IsChecked屬性以設置或獲取CheckBox的值。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM