简体   繁体   中英

XAML Binding for DataGrid

Here is a requirement for XAML Binding for DataGrid.

  1. I have 4 lists ( IList<T> ) in my code behind file
  2. Need to use WPF DataGrid with 4 columns; 1 column for each list
  3. Need to bind each list to corresponding column from the grid
  4. You can assume strings are displayed from each list on the columns of the grid

My question is how can I use these lists in DataGridcolumnTemplate (For ex, ListBox etc..) Can someone please explain?

WPF DataGrid derives from ItemsControl, which means that it only supports binding to one collection. That's my understanding. So I think in your case one of possible solutions is to use four ListBox controls aligning side by side and craft the styles to make them look more like a Grid if you want the similar looking and feel.

If you are binding to a Data grid, it is better to have a List than having as separate List.

For example,

You have a Class :

 public class Product 
    {
        public decimal guProductId { get; set; }
        public string productCode { get; set; }
    }
 List<Product> Products;

Which can be binded to a datagrid rather than having separate list of GuProductID and ProductCodes Like below.

   List<decimal> guProductIds;
   List<string> productCodes;

It is easy to bind the List to bind to datagrid rather than having Individual Lists.

If you're not obligated to use DataGrid, why not make your own usercontrol that hosts 4 ListBoxes and fill them with each List. Something like this ...

   <UserControl x:Class="Listboxes.ListBoxes"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
    <UserControl.Resources>
        <DataTemplate x:Key="lbItemTemplate">
            <Label Content="{Binding}"/>
        </DataTemplate>
    </UserControl.Resources>

    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition/>
            <ColumnDefinition/>
            <ColumnDefinition/>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>
        <ListBox Grid.Column="0"
                 ItemsSource="{Binding}"
                 ItemTemplate="{StaticResource lbItemTemplate}"/>
        <ListBox Grid.Column="1"
                 ItemTemplate="{StaticResource lbItemTemplate}"/>
        <ListBox Grid.Column="2"
                 ItemTemplate="{StaticResource lbItemTemplate}"/>
        <ListBox Grid.Column="3"
                 ItemTemplate="{StaticResource lbItemTemplate}"/>

    </Grid>
</UserControl>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM