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

最後通過點擊獲取數據:

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章