[英]XAML Binding for DataGrid
這是XAML綁定DataGrid的要求。
IList<T>
) 我的問題是如何在DataGridcolumnTemplate中使用這些列表(例如,對於ListBox等。)有人可以解釋一下嗎?
WPF DataGrid繼承自ItemsControl,這意味着它僅支持綁定到一個集合。 那是我的理解。 因此,我認為在您的情況下,一種可能的解決方案是使用四個ListBox控件並排對齊,並設計樣式以使它們看起來更像Grid(如果您想要類似的外觀)。
如果要綁定到數據網格,則擁有一個列表比擁有一個單獨的列表更好。
例如,
您有一個班級:
public class Product
{
public decimal guProductId { get; set; }
public string productCode { get; set; }
}
List<Product> Products;
可以綁定到數據網格,而不必像下面那樣單獨包含GuProductID和ProductCodes列表。
List<decimal> guProductIds;
List<string> productCodes;
將列表綁定到數據網格很容易,而不是具有單個列表。
如果您沒有義務使用DataGrid,為什么不使用自己的用戶控件來承載4個ListBox,並用每個List填充它們。 像這樣的東西...
<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>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.