根据复选框选择来选择项目值

时间:2019-02-15 17:56:27

标签: c# wpf visual-studio wpf-controls

我在wpf中有一个正在开发的项目,并且使用TreeView控件。在子项目的结构内部,我放置了一个StackPanel,在其中放置了其他一些组件。一个文本框,Checbox和一个ImageView。我需要从对应的复选框项中获取数据,并选中true,但我不知道如何通过常规的Checkbox事件来做到这一点。尝试使用MouseDouble事件从项目中获取数据。

这是项目的结构。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ClassModel
{
    public class Base
    {
        public int Id { get; set; }
        public string Nome { get; set; }
        public string Descricao { get; set; }
        public Tipo Tipo { get; set; }
        public ICollection<Email> Emails { get; set; }
        public ICollection<Telefone> Telefones { get; set; }

        public Base()
        {
            Emails = new List<Email>();
            Telefones = new List<Telefone>();
        }

        override
        public string ToString()
        {
            return Nome;
        }
    }
}

Class Contato

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ClassModel
{
    public class Contato:Base
    {
        public virtual ContatoJuridico ContatoJuridico { get; set; }
        public Departamento Departamento { get; set; }

        public string ToDetalhes()
        {
            return Nome + " - " + Departamento;
        }
    }
}

ContatoJuridico类

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ClassModel
{
    /// <summary>
    /// Nos casos em que a categoria do objeto for Auditoria ou Organização_Social os atributos
    /// Auditoria e OrganizacaoSocial são nulos
    /// </summary>
    public class ContatoJuridico:Base
    {
        public Categoria Categoria { get; set; }
        public virtual ContatoJuridico Auditoria { get; set; }
        public virtual ContatoJuridico OrganizacaoSocial { get; set; }
        public ICollection<Contato> Contatos { get; set; }
        public ICollection<ContatoJuridico> Juridicos { get; set; }

        public ContatoJuridico()
        {
            //Contatos = new List<Contato>();
            Contatos = new ObservableCollection<Contato>();
            Juridicos = new List<ContatoJuridico>();
        }
    }
}

XAML 这是映射我的TreeView的代码段

<ScrollViewer Grid.Column="0" Grid.Row="0" Grid.RowSpan="2">
            <TreeView x:Name="trViewContatos" MouseDoubleClick="TrViewContatos_MouseDoubleClick">
                <TreeView.Resources>
                    <HierarchicalDataTemplate x:Name="data" DataType="{x:Type fild:ContatoJuridico}" ItemsSource="{Binding Contatos}">

                        <StackPanel x:Name="stack" Orientation="Horizontal">
                            <Image Source="/Imagens/pessoaJuridica1.png" Height="15" Width="15" Margin="0,0,5,0" />
                            <CheckBox x:Name="chMaster" Checked="ChMaster_Checked"></CheckBox>
                            <TextBlock Text="{Binding Nome}" IsEnabled="False"/>
                        </StackPanel>

                    </HierarchicalDataTemplate>
                    <HierarchicalDataTemplate DataType="{x:Type fild:Contato}" ItemsSource="{Binding Emails}">

                        <StackPanel Orientation="Horizontal">
                            <Image Source="/Imagens/usuário-png-8.png" Height="15" Width="15" Margin="0,0,5,0" />
                            <CheckBox x:Name="chItem" Checked="ChItem_Checked"></CheckBox>
                            <TextBlock Text="{Binding Nome}" IsEnabled="False"/>
                        </StackPanel>

                    </HierarchicalDataTemplate>
                    <!--<DataTemplate DataType="{x:Type fild:Contato}">

                        <StackPanel Orientation="Horizontal">
                            <Image Source="/Imagens/usuário-png-8.png" Height="15" Width="15" Margin="0,0,5,0" />
                            <CheckBox x:Name="chItem" Checked="ChItem_Checked"></CheckBox>
                            <TextBlock Text="{Binding Nome}" IsEnabled="False"/>
                        </StackPanel>

                    </DataTemplate>-->
                </TreeView.Resources>
            </TreeView>

XAML.CS 在此代码中,我双击了树视图项

private void TrViewContatos_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            try
            {
                if (trViewContatos.SelectedItem is ContatoJuridico)
                {
                    ContatoJuridico cc = trViewContatos.SelectedItem as ContatoJuridico;

                    foreach (Email em in cc.Emails)
                    {
                        emails.Add(em);
                    }

                    MessageBox.Show($"Você selecionou o item {cc.ToString()}");
                }
                else
                {
                    Contato cc = trViewContatos.SelectedItem as Contato;

                    foreach (Email em in cc.Emails)
                    {
                        emails.Add(em);
                    }

                    MessageBox.Show($"Você selecionou o item {cc.ToString()}");
                }            
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

所以我需要基于复选框事件从Treeview项中获取数据。 你能帮我吗?

0 个答案:

没有答案