[英]Data grid inside list view as item template and Read Data from Database
I have created a project through WPF and my code as below 我已经通过WPF和下面的代码创建了一个项目
<UserControl.Resources>
<x:Array x:Key="OrderDataGridItems" Type="{x:Type system:String}">
<system:String>1</system:String>
<system:String>2</system:String>
<system:String>3</system:String>
</x:Array>
<DataTemplate x:Key="TemplateWithDataGrid" >
<DataGrid ItemsSource="{StaticResource OrderDataGridItems}">
<DataGrid.Columns>
<DataGridTextColumn Header="Column1"
Width="*" />
<DataGridTextColumn Header="Column2"
Width="*" />
</DataGrid.Columns>
</DataGrid>
</DataTemplate>
</UserControl.Resources>
Triggers>
<Grid>
<ListView x:Name="CustomersOrderList" ItemTemplate="{StaticResource TemplateWithDataGrid}" >
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Setter Property="HorizontalContentAlignment" Value="Stretch" />
</Style>
</ListView.ItemContainerStyle>
</ListView>
</Grid>
</UserControl>
the above code works only for the static resource values(in OrderDataGridItems). 上面的代码仅适用于静态资源值(在OrderDataGridItems中)。 this CustomersOrderList inside listview works fine with database values.
listview内的这个CustomersOrderList与数据库值配合良好。 my requirement is to get and fill all the data from database in both DataTemplate and ListView from the database,is it possible??
我的要求是从数据库中获取并填充数据库中的所有数据到DataTemplate和ListView中,这可能吗? or please help me to get the this solution work, your help greatly appreciated, thank you in-advance for our support.
或者,请帮助我获得此解决方案的工作,我们将不胜感激,非常感谢您的支持。
You don't need to create Templates
until and unless you are not reusing those templates. 你并不需要创建
Templates
之前,除非你是不是重复使用这些模板。 To make this simple, I've given below sample code to populate a DataGrid
with data from external sources. 为简单起见,我在下面给出了示例代码,以使用来自外部源的数据填充
DataGrid
。
View - 查看-
<Grid>
<DataGrid ItemsSource="{Binding OrderDataGridItems }" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn Header="Column1" Width="Auto" Binding="{Binding CountryName}" />
</DataGrid.Columns>
</DataGrid>
</Grid>
CodeBehind - You should have this in ViewModel if following MVVM. CodeBehind-如果遵循MVVM,则应该在ViewModel中使用它。
public partial class MainWindow : Window, INotifyPropertyChanged
{
private List<CountryData> orderDataGridItems;
public List<CountryData> OrderDataGridItems
{
get { return orderDataGridItems; }
set
{
orderDataGridItems = value;
OnPropertyChanged("OrderDataGridItems");
}
}
public MainWindow()
{
InitializeComponent();
this.DataContext = this;
var listFromDataBase = new List<CountryData>();
listFromDataBase.Add(new CountryData { CountryName ="India"});
OrderDataGridItems = listFromDataBase;
}
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.