3个主从数据网格水平显示

时间:2014-09-19 16:39:17

标签: wpf wpfdatagrid

我是WPF新手。我有一个WPF应用程序。在其中一个WPF表单中,需要显示一个3个WPF数据网格控件,其行为是以级联方式进行的。

Datagrid1显示在左侧; datagrid2显示在中间; datagrid3显示在右侧。因此,这3个数据网格显示为水平。 Datagrid1向客户展示; datagrid2显示特定客户的订单; datagrid3显示特定订单的订单详细信息。

当用户点击datagrid1的客户行时,应用程序可以在datagrid2中加载该客户的订单。当用户单击datagrid2的订单行时,应用程序可以在datagrid3中显示该订单的订单详细信息。

请告诉我如何使用该功能。谢谢。

1 个答案:

答案 0 :(得分:0)

我做了一个例子。

MainWindow.xaml

<Window x:Class="StackOverflow.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:viewModel="clr-namespace:StackOverflow.ViewModel"
        Title="MainWindow" Height="350" MinWidth="525">
    <Window.DataContext>
        <viewModel:TestViewModel />
    </Window.DataContext>
    <DockPanel Margin="10">
        <DataGrid DockPanel.Dock="Left" MinWidth="200" Width="Auto" ItemsSource="{Binding Customers}" IsReadOnly="True" SelectedItem="{Binding SelectedCustomer}" AutoGenerateColumns="False" SelectionUnit="FullRow">
            <DataGrid.Columns>
                <DataGridTextColumn Header="ID" Binding="{Binding CustomerId}" Width="Auto"></DataGridTextColumn>
                <DataGridTextColumn Header="First Name" Binding="{Binding FirstName}"></DataGridTextColumn>
                <DataGridTextColumn Header="Last Name" Binding="{Binding LastName}"></DataGridTextColumn>
            </DataGrid.Columns>
        </DataGrid>
        <DataGrid MinWidth="200" DockPanel.Dock="Right" ItemsSource="{Binding OrderDetails}" SelectionUnit="FullRow" IsEnabled="{Binding OrderDetailsEnabled}" IsReadOnly="True" AutoGenerateColumns="False">
            <DataGrid.Columns>
                <DataGridTextColumn Header="Details ID" Binding="{Binding OrderDetailsId}" Width="Auto"></DataGridTextColumn>
                <DataGridTextColumn Header="Address" Binding="{Binding OrderAddress}" Width="Auto"></DataGridTextColumn>
                <DataGridTextColumn Header="Order Time" Binding="{Binding OrderTime}" Width="Auto"></DataGridTextColumn>
            </DataGrid.Columns>
        </DataGrid>
        <DataGrid MinWidth="200" ItemsSource="{Binding Orders}" IsEnabled="{Binding OrderEnabled}" IsReadOnly="True" AutoGenerateColumns="False" SelectionUnit="FullRow" SelectedItem="{Binding SelectedOrder}">
            <DataGrid.Columns>
                <DataGridTextColumn Header="Order ID" Binding="{Binding OrderId}" Width="Auto"></DataGridTextColumn>
                <DataGridTextColumn Header="Prod Name" Binding="{Binding ProductName}"></DataGridTextColumn>
            </DataGrid.Columns>
        </DataGrid>
    </DockPanel>
</Window>

MainWindow.xaml.cs

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }
}

TestViewModel.cs

class TestViewModel : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    private bool _orderEnable = false;

    public bool OrderEnabled
    {
        get { return _orderEnable; }
        set { _orderEnable = value; RaisePropertyChanged("OrderEnabled"); }
    }

    private bool _orderDetailsEnabled = false;

    public bool OrderDetailsEnabled
    {
        get { return _orderDetailsEnabled; }
        set { _orderDetailsEnabled = value; RaisePropertyChanged("OrderDetailsEnabled"); }
    }



    private static readonly object _lock = new object();

    private ObservableCollection<Customer> _customers;

    public IEnumerable<Customer> Customers
    {
        get { return _customers; }
    }

    private IEnumerable<OrderDetail> _orderDetails;

    public IEnumerable<OrderDetail> OrderDetails
    {
        get { return _orderDetails; }
        set { _orderDetails = value; RaisePropertyChanged("OrderDetails"); }
    }


    private IEnumerable<Order> _orders;

    public IEnumerable<Order> Orders
    {
        get { return _orders; }
        private set
        {
            _orders = value; RaisePropertyChanged("Orders");
            OrderDetailsEnabled = false;
        }
    }

    private Order _selectedOrder;

    public Order SelectedOrder
    {
        get { return _selectedOrder; }
        set
        {
            if (value != null && _selectedOrder != value)
            {
                _selectedOrder = value;
                OrderDetails = _selectedOrder.OrderDetails;
                OrderDetailsEnabled = true;
            }
        }
    }

    private Customer _selectedCustomer;

    public Customer SelectedCustomer
    {
        get { return _selectedCustomer; }
        set
        {
            if (value != null && _selectedCustomer != value)
            {
                _selectedCustomer = value;
                Orders = _selectedCustomer.Orders;
                OrderEnabled = true;
            }
        }
    }

    public TestViewModel()
    {
        _customers = new ObservableCollection<Customer>();
        BindingOperations.EnableCollectionSynchronization(_customers, _lock);
        _customers.Add(new Customer
        {
            CustomerId = 1,
            FirstName = "TestFirst",
            LastName = "TestLast",
            Orders = new List<Order> { new Order { OrderId = 1, ProductName = "ProdOneOne", 
                OrderDetails = new List<OrderDetail> { new OrderDetail { OrderDetailsId = 1, OrderAddress = "EarthOneOne", OrderTime = DateTime.Now  } } },
            new Order { OrderId = 2, ProductName = "ProdOneTwo", 
                OrderDetails = new List<OrderDetail> { new OrderDetail { OrderDetailsId = 2, OrderAddress = "EarthOneTwo", OrderTime = DateTime.Now } } }}
        });

        _customers.Add(new Customer
        {
            CustomerId = 2,
            FirstName = "FirstTest",
            LastName = "LastTest",
            Orders = new List<Order> { new Order { OrderId = 1, ProductName = "ProdTwoOne", 
                OrderDetails = new List<OrderDetail> { new OrderDetail { OrderDetailsId = 1, OrderAddress = "EarthTwoOne", OrderTime = DateTime.Now } } },
            new Order { OrderId = 2, ProductName = "ProdTwTwo", 
                OrderDetails = new List<OrderDetail> { new OrderDetail { OrderDetailsId = 2, OrderAddress = "EarthTwoTwo", OrderTime = DateTime.Now } } } }
        });
    }

    public void RaisePropertyChanged(string propertyName)
    {
        var pc = PropertyChanged;
        if (pc != null)
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }

}



public class Customer
{
    public int CustomerId { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public IEnumerable<Order> Orders { get; set; }
}

public class Order
{
    public int OrderId { get; set; }
    public string ProductName { get; set; }
    public IEnumerable<OrderDetail> OrderDetails { get; set; }
}

public class OrderDetail
{
    public int OrderDetailsId { get; set; }
    public DateTime OrderTime { get; set; }
    public string OrderAddress { get; set; }
}

看起来非常冗长,但很容易理解。我希望这能让你开始。