我如何在框架模式MVVM中关联TreeView和DataGrid?

时间:2017-01-04 18:47:25

标签: c# wpf mvvm treeview caliburn.micro

我使用Caliburn.Micro,想要在框架模式MVVM中使用TreeView和DataGrid进行通风。 TreeView具有分层结构,我希望突出显示的树项成为DataGrid的项目源。但TreeView没有属性SelectedItem。

我有2个型号合同和产品

pubcli class Contarct()
{
public Contract()
        {
            ProductItems = new BindableCollection<Product>();
        }

        private string _contractName;
        private string _firmName;
        private BindableCollection<Product> _productItems;
} 

public class Prodcut()
{
    public Product()
        {

            Children = new BindableCollection<Product>();
        }

        private string _productName;
        private string _contractName;
        private string _serialNumber;
        private string _markNumber;
        private string _xrayNumber;
}

还有一个类ContractService

public class ContractService
{
  public ContractService()
        {
            Childrens = new BindableCollection<Product>
            {
                new Product{ProductName = "Comp1", SerialNumber = "SN123456", Children = null },

            };
            Products = new BindableCollection<Product>
            {
                new Product {ProductName = "Comp2", SerialNumber = "SN123456", Children = Childrens },

            };

            Contracts = new BindableCollection<Contract>
           {
               new Contract {ContractName = "Alfa" , ProductItems = Products},

           };

        }



//commands



 public void GetData(Action<BindableCollection<Contract>,Exception>callback)
    {
        callback(Contracts, null);
    }

    public void _addnewContract()
    {
        Contracts.Add(new Contract {ContractName = "Test" });
    }
    public void _removeContarct(Contract contr)
    {
        Contracts.Remove(contr);
    }
    public Contract FindContarct(Contract contr)
    {
        if (contr == null)
            return null;
        return Contracts.FirstOrDefault(Contracts => Contracts.ContractName == contr.ContractName);
    }

}

视图模型

public class MainViewModel : PropertyChangedBase
    {
        private readonly ContractService _contractService;
        public MainViewModel(ContractService contractService)
        {
            _contractService = contractService;
            _contractService.GetData(
                (items, error) =>
                {
                    contract = items;
                });    
        }
        public void contract_SelectedItemChanged(Contract sender, RoutedPropertyChangedEventArgs<object> e)
        {

            _contractService.FindContarct(sender);




        }
        public BindableCollection<Contract> contract { get; set; }

        public void AddNewContract()
        {
            _contractService._addnewContract();

        }
        public void RemoveContract()
        {
            _contractService._removeContarct(SelectedContract);
        }
        private Contract _selectedContract;
        public Contract SelectedContract
        {
            get { return _selectedContract; }
            set
            {
                _selectedContract = value;
                NotifyOfPropertyChange("SelectedContract");
                NotifyOfPropertyChange(() => CanRemoveContract);
            }
        }

        public bool CanRemoveContract
        {
            get
            {
                if (SelectedContract == null)
                    return false;
                var findContract = _contractService.FindContarct(SelectedContract);
                if (findContract == null)
                    return false;
                return true;
            }
        }

并且

<Grid>
        <WrapPanel>
            <TreeView ItemsSource="{Binding contract}" MinWidth="200"  
                      cal:Message.Attach="[Event SelectedItemChanged]=[Action contract_SelectedItemChanged($this,$eventArgs)]" 
                      >
                <TreeView.Resources>
                    <HierarchicalDataTemplate DataType="{x:Type local:Contract}" ItemsSource="{Binding ProductItems}">
                        <TextBlock Text="{Binding ContractName}" />
                    </HierarchicalDataTemplate>
                    <HierarchicalDataTemplate DataType="{x:Type local:Product}" ItemsSource="{Binding Children}">
                        <TextBlock Text="{Binding ProductName}" />
                    </HierarchicalDataTemplate>
                </TreeView.Resources>
            </TreeView>
            <DataGrid ItemsSource="{Binding contract}" AutoGenerateColumns="False" SelectedItem="{Binding SelectedContract}" >
                <DataGrid.Columns>
                    <DataGridTextColumn Binding="{Binding ContractName}" Header="Имя контракта" ></DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding ProductName}" Header="Имя продукта" ></DataGridTextColumn>
                    <DataGridTextColumn></DataGridTextColumn>
                    <DataGridTextColumn></DataGridTextColumn>
                </DataGrid.Columns>
            </DataGrid>
</Grid>

0 个答案:

没有答案