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

最后通过点击获取数据:

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
当然可以。以下是一个 DataGridComboBoxColumn 在 WPF 使用的示例,并绑定到数据源的代码: 1. 首先,你需要在 XAML 定义一个 DataGrid 控件,并在其添加模板列,以包含 DataGridComboBoxColumn: ```xml <DataGrid AutoGenerateColumns="False"> <DataGrid.Columns> <!-- other columns... --> <DataGridTemplateColumn Header="MyComboBox"> <DataGridTemplateColumn.CellTemplate> <DataTemplate> <ComboBox ItemsSource="{Binding ItemsSource}" DisplayMemberPath="DisplayMember" SelectedValuePath="SelectedValue" SelectedValue="{Binding SelectedItem, Mode=TwoWay}" /> </DataTemplate> </DataGridTemplateColumn.CellTemplate> </DataGridTemplateColumn> </DataGrid.Columns> </DataGrid> ``` 2. 接下来,你需要在代码获取 DataGridComboBoxColumn,并将其绑定到数据源: ```csharp private void SetupDataGrid() { // get the DataGridComboBoxColumn var comboBoxColumn = dataGrid.Columns.First(c => c.Header.Equals("MyComboBox")) as DataGridComboBoxColumn; // setup the ItemsSource binding var binding = new Binding("MyItemsSource") { Source = this }; comboBoxColumn.ItemsSourceBinding = binding; // setup the DisplayMemberPath binding binding = new Binding("MyDisplayMemberPath") { Source = this }; comboBoxColumn.DisplayMemberPathBinding = binding; // setup the SelectedValuePath binding binding = new Binding("MySelectedValuePath") { Source = this }; comboBoxColumn.SelectedValuePathBinding = binding; // setup the SelectedValue binding binding = new Binding("MySelectedValue") { Source = this, Mode = BindingMode.TwoWay }; comboBoxColumn.SelectedValueBinding = binding; } // in your ViewModel or code-behind, define the following properties for each of the bindings: public IEnumerable MyItemsSource { get; set; } public string MyDisplayMemberPath { get; set; } public string MySelectedValuePath { get; set; } public object MySelectedValue { get; set; } ``` 这样就可以在 WPF 使用 DataGridComboBoxColumn 并绑定数据源了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

楚楚3107

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值