wpf绑定用户控件及listview(gridview)数据源

时间:2022-09-06 11:45:59

用户控件:

前台界面

<UserControl x:Class="testUserControl.project"
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">
<Grid>
<Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="22,0,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />
<ListView Name="lvData" Width="200" Height="200" Margin="12,29,88,71">
<ListView.View>
<GridView>
<GridViewColumn Width="100" Header="ID" DisplayMemberBinding="{Binding ID}"/>
<GridViewColumn Width="100" Header="Data" DisplayMemberBinding="{Binding Data}"/>
</GridView>
</ListView.View>
</ListView>
</Grid>
</UserControl>

后台代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;

namespace testUserControl
{
/// <summary>
/// Interaction logic for project.xaml
/// </summary>
public partial class project : UserControl
{

#region Member

private ObservableCollection<ModelClass> ModelClassList = new ObservableCollection<ModelClass>();

#endregion

#region Properties
/// <summary>
/// 用于保存数据实体的集合
/// ObservableCollection这个集合可以实时和前台交互,也就是说
/// 只需要修改集合中的内容,前台的ListView会自动刷新,不需要手动刷新
/// </summary>
public ObservableCollection<ModelClass> _ModelClassList
{
get { return ModelClassList; }
set { ModelClassList = value; }
}

#endregion

#region Constructor
public project()
{
InitializeComponent();

//指定数据源来自哪个对象(我理解是这样的)
lvData.DataContext = this;

for (int i = 0; i < 100; i++)
{
ModelClass m = new ModelClass() // 我在这里给ModelClass赋值,不管你数据库里是Data还是pname ,到这里,都给Data,前台不需要动
{
ID = i,
Data = "test"
};
_ModelClassList.Add(m);
}

//讲集合赋给listview
lvData.ItemsSource = _ModelClassList;

//照这个写,只要修改ModelClass类里的内容,然后添加到集合里就可以了
//DataGrid控件实际也是这样的
}
#endregion

#region Event
private void button1_Click(object sender, RoutedEventArgs e)
{
ModelClass m = lvData.SelectedItem as ModelClass;

_ModelClassList.Remove(m);
}
#endregion
}
}

 

实体类:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace testUserControl
{
public class ModelClass
{
public int ID { get; set; }
public string Data { get; set; }//这个data对应不是数据库中的data字段啊?这个还不是随便你的么,规范的话,要一样,不一样么,反正程序是不会出错啦。
}
}

 

方法一:

后台绑定用户控件

前台:

<Window x:Class="testUserControl.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded">

<Grid Name="Layout">

</Grid>
</Window>

后台:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace testUserControl
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}

private void Window_Loaded(object sender, RoutedEventArgs e)
{
project project = new project();
Layout.Children.Add(project);
}
}
}


方法二:

前台绑定用户控件:

<Window x:Class="testUserControl.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:userControl="clr-namespace:testUserControl"
Title="MainWindow" Height="350" Width="525">
<Grid>
<userControl:project/>
</Grid>
</Window>