简体   繁体   English

列表视图中的数据网格作为项目模板,并从数据库中读取数据

[英]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.

相关问题 从网格视图向数据库添加数据 - Adding data to database from grid view 从网格视图将多个数据插入数据库 - Multiple data insertion into database from grid view 数据网格数据模板中的项目源不起作用 - Item Source in Data Grid Data Template not working 如何通过单击数据模板列表视图中的项目来打开另一个xamarin表单页面? - How to open another xamarin forms page from clicking a item in a data template list view? 将具有搜索功能的数据网格视图中的数据保存到数据库中 - Saving data from a data grid view with search functionality to a database 将选中的项目从数据网格视图保存到文本框 - save seleted item from data grid view to textbox 如何将数据从列表插入到数据网格视图的列 - How to insert data from list to a column of data grid view 无法从 List&lt;&gt; 获取数据以填充数据网格视图 - can't get data from List<> to populate data grid view Windows 8.1应用商店中网格视图内的多个项目模板 - Multiple item template inside grid view in Windows 8.1 store app 单击列表视图项,在列表视图的数据模板中显示UI元素(图像,文本) - Display a UI element (Image, Text) in Data Template of a List View on click of List View Item
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM