WPF 通过获取DataTemplate模板中的其他数据

通过CheckBox的状态,获取对应的模板名称。代码如下:

MainWindow.xaml:

<Window x:Class="DataContent_Control.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:DataContent_Control"
        mc:Ignorable="d"
        Title="MainWindow" Height="200" Width="300">
    <Window.Resources>
        <DataTemplate x:Key="dataTempalte">
            <Border BorderBrush="Blue" BorderThickness="1">
                <StackPanel Orientation="Horizontal" Height="30" Width="100">
                    <CheckBox IsChecked="{Binding Status}" Margin="2" HorizontalAlignment="Center" VerticalAlignment="Center" Click="CheckBox_Checked"></CheckBox>
                    <TextBlock Text="{Binding Name}" Name="TB"  FontSize="10" HorizontalAlignment="Center" VerticalAlignment="Center"></TextBlock>
                </StackPanel>
            </Border>
        </DataTemplate>
    </Window.Resources>
    <ListBox ItemTemplate="{StaticResource dataTempalte}" ItemsSource="{Binding TypeList}">
        
    </ListBox>
</Window>

MainWindow.xmal.cs

using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Controls;

namespace DataContent_Control
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            TypeVM typeVM = new TypeVM();
            this.DataContext = typeVM;
            InitializeComponent();
        }

        private void CheckBox_Checked(object sender, RoutedEventArgs e)
        {
            TypeModel tb = (sender as CheckBox).DataContext as TypeModel;
    
            if(tb != null && string.Equals(tb.Status,"True"))
                Console.WriteLine(tb.Name);
        }
    }

}

TypeVM.cs

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

namespace DataContent_Control
{
    public class TypeVM : INotifyPropertyChanged
    {
        private ObservableCollection<TypeModel> typeList;

        public TypeVM()
        {
            TypeList = new ObservableCollection<TypeModel>();
            typeList.Add(new TypeModel() { Status = "true",Name = "张三"});
            typeList.Add(new TypeModel() { Status = "true",Name = "李四"});
        }

        public ObservableCollection<TypeModel> TypeList
        {
            get { return typeList; }
            set { typeList = value; this.NotifyPropertyChange("TypeList"); }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        public void NotifyPropertyChange(string propertyName)
        {
            if (null != this.PropertyChanged)
            {
                this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }

    public class TypeModel : INotifyPropertyChanged
    {
        #region 字段
        private string _status;
        private string _name;
        #endregion

        #region 属性
        public string Status
        {
            get { return _status; }
            set { _status = value; this.NotifyPropertyChange("Status"); }
        }

        public string Name
        {
            get { return _name; }
            set { _name = value; this.NotifyPropertyChange("Name"); }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        public void NotifyPropertyChange(string propertyName)
        {
            if (null != this.PropertyChanged)
            {
                this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyName));
            }
        }
        #endregion
    }
}

最后通过点击获取数据:

©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页