【wpf】【课设】listview的数据绑定和关于前端的一些东东

来源:转载


最近搞课设,一个关于windows下链接蓝牙的软件,叫做基于Windows OS Bluetooth模块设计与实现,遇到了需要用listview,以前用安卓的时候,数据绑定比较方便,然而写wpf时,由于基本上是从头开始,所以很多基础不扎实,再加上查书的时候有很少写到listview的数据绑定。通过度娘的帮助,学习到了一个比较简单的方法进行wpf里listview的数据绑定----使用ObservableCollection类;

public ObservableCollection<object> ObservableObj; public MainWindow() { InitializeComponent(); ObservableObj = new ObservableCollection<object>(); listView.DataContext = ObservableObj; } private void Open_Click(object sender, RoutedEventArgs e) { BluetoothRadio radio = BluetoothRadio.PrimaryRadio;//获取当前PC的蓝牙适配器 if (radio == null)//检查该电脑蓝牙是否可用 { MessageBox.Show("请您打开电脑的蓝牙!"); return; } InTheHand.Net.Bluetooth.BluetoothRadio.PrimaryRadio.Mode = InTheHand.Net.Bluetooth.RadioMode.Connectable; InTheHand.Net.Sockets.BluetoothClient cli = new InTheHand.Net.Sockets.BluetoothClient(); InTheHand.Net.Sockets.BluetoothDeviceInfo[] devices = cli.DiscoverDevices(); ObservableObj.Clear(); foreach (InTheHand.Net.Sockets.BluetoothDeviceInfo device in devices) //设备搜寻 { device.Update(); device.Refresh(); int i; for (i = 0; i < devices.Length; i++) { ObservableObj.Add(new { Name = devices[i].DeviceName,Address = devices[i].DeviceAddress, Statu= devices[i].Connected }); } link.Text = "搜索完成!"; break; } }


对了,代码中蓝牙模块的实现利用了引入了第三方库InTheHand.Net.Personal.dll;


   声明一个 ObservableCollection类,再运行listView.DataContext = ObservableObj;使其和listview进行绑定,然后直接运用 ObservableCollection类.Add();这个方法将数据添加到listview中。



好了,现在来说第二个问题,当打开项目时,看右边在个button和listview还能和谐相处




但是在将边框拖动放大或者缩小时,listview会将button覆盖掉,例如下图:


因为技术比较菜,也没系统的学过xaml的前端,没找到如何布局使其能够完美的做到自适应。后来看代码,发现listview 和button的HorizontalAlignment属性都是Right,那么我想,如果将button的HorizontalAlignment属性改为Left,将其置为左右布局的话是不是就可以解决问题呢(后来发现还真可以)


但是我觉得这应该不算最完美的办法,如果各位有其他更好的方法的话,请联系我^_^ 


好吧,这是上个星期的所有成果,代码如下:

XAML:

<Window x:Class="Bluetooth_TEST.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:Bluetooth_TEST" xmlns:c="clr-namespace:System.Collections;assembly=mscorlib" mc:Ignorable="d" Title="Bluetooth_TEST" Height="350" Width="566"> <Grid Margin="0,0,-8,0"> <Button x:Name="open" Content="打开" HorizontalAlignment="Right" Margin="0,24,25,0" VerticalAlignment="Top" Width="80" Click="Open_Click"/> <Button x:Name="close" Content="关闭" HorizontalAlignment="Right" Margin="0,54,25,20" VerticalAlignment="Top" Width="80" Click="Close_Click"/> <Button x:Name="connect" Content="连接" HorizontalAlignment="Right" Margin="0,84,25,20" VerticalAlignment="Top" Width="80" Click="connect_Click"/> <TextBlock x:Name="link" HorizontalAlignment="Right" Margin="0,114.909,25,0" TextWrapping="Wrap" Text="未连接" VerticalAlignment="Top" Width="80" Height="16.574" TextAlignment="Center"> <TextBlock.OpacityMask> <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0"> <GradientStop Color="red" Offset="0"/> </LinearGradientBrush> </TextBlock.OpacityMask> </TextBlock> <ListView x:Name="listView" Margin="33.333,25.332,133.667,55.668" ItemsSource="{Binding}" SelectionChanged="listView_SelectionChanged" > <ListView.View> <GridView> <GridViewColumn Header="Name" Width="150" DisplayMemberBinding="{Binding Name}"> </GridViewColumn> <GridViewColumn Header="Address" Width="150" DisplayMemberBinding="{Binding Address}"> </GridViewColumn> <GridViewColumn Header="Statu" Width="70" DisplayMemberBinding="{Binding Statu}"> </GridViewColumn> </GridView> </ListView.View> </ListView> </Grid></Window>

C#:

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;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 InTheHand.Net.Bluetooth;using InTheHand.Net.Sockets;using InTheHand.Net;using System.Threading;using System.Collections.ObjectModel;using InTheHand.Windows.Forms;namespace Bluetooth_TEST{ public partial class MainWindow : Window { public ObservableCollection<object> ObservableObj; public MainWindow() { InitializeComponent(); ObservableObj = new ObservableCollection<object>(); listView.DataContext = ObservableObj; } private void Open_Click(object sender, RoutedEventArgs e) { BluetoothRadio radio = BluetoothRadio.PrimaryRadio;//获取当前PC的蓝牙适配器 if (radio == null)//检查该电脑蓝牙是否可用 { MessageBox.Show("请您打开电脑的蓝牙!"); return; } InTheHand.Net.Bluetooth.BluetoothRadio.PrimaryRadio.Mode = InTheHand.Net.Bluetooth.RadioMode.Connectable; InTheHand.Net.Sockets.BluetoothClient cli = new InTheHand.Net.Sockets.BluetoothClient(); InTheHand.Net.Sockets.BluetoothDeviceInfo[] devices = cli.DiscoverDevices(); ObservableObj.Clear(); foreach (InTheHand.Net.Sockets.BluetoothDeviceInfo device in devices) //设备搜寻 { device.Update(); device.Refresh(); int i; for (i = 0; i < devices.Length; i++) { ObservableObj.Add(new { Name = devices[i].DeviceName,Address = devices[i].DeviceAddress, Statu= devices[i].Connected }); } link.Text = "搜索完成!"; break; } } private void Close_Click(object sender, RoutedEventArgs e) { ObservableObj.Clear(); link.Text = "未连接"; } private void connect_Click(object sender, RoutedEventArgs e) { } private void listView_SelectionChanged(object sender, SelectionChangedEventArgs e) { MessageBox.Show("选中了"+e.AddedItems[0].ToString()); } }}

运行截图:





现在完全还是半成品,边学边做边更新吧



分享给朋友:
您可能感兴趣的文章:
随机阅读: