子曾经曰过

  博客园  :: 首页  ::  ::  ::  :: 管理

WPF数据绑定

xaml文件

<Window x:Class="WpfApplication2.MainWindow"
xmlns
="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x
="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:src
="clr-namespace:WpfApplication2"
Title
="MainWindow" Height="350" Width="525">
<Grid>
<Grid.Resources>
<src:PO x:Key="datas"></src:PO>
<!--src表示数据源-->
<DataTemplate x:Key="imgtemplate">
<!--datatemplate表示数据的显示形式-->
<TextBlock Foreground="Black" FontSize="12">
<TextBlock.Text>
<Binding Path="ProcessName"></Binding>
</TextBlock.Text>
</TextBlock>
</DataTemplate>
</Grid.Resources>

<ListBox ItemsSource="{StaticResource datas}" ItemTemplate="{StaticResource imgtemplate}" Margin="12,46,0,0" Name="listBox1" Height="231" VerticalAlignment="Top" HorizontalAlignment="Left" Width="120"/>

<DataGrid ItemsSource="{StaticResource datas}" AutoGenerateColumns="False" Height="231" HorizontalAlignment="Left" Margin="138,46,0,0" Name="dataGrid1" VerticalAlignment="Top" Width="353" HorizontalGridLinesBrush="#E6E5DFDF" VerticalGridLinesBrush="#E6E5DFDF">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Path=ProcessName}" CanUserReorder="False" CanUserResize="False" CanUserSort="False" Header="进程名" IsReadOnly="True" />
<DataGridTextColumn Binding="{Binding Path=MemoryUsed}" CanUserReorder="False" CanUserResize="False" CanUserSort="False" Header="占用内存" IsReadOnly="True" />
</DataGrid.Columns>
</DataGrid>
<Button Content="机器属性" Height="23" HorizontalAlignment="Left" Margin="263,8,0,0" Name="BTN" VerticalAlignment="Top" Width="75" Click="BTN_Click" />
<Label Content="已用内存" Height="28" HorizontalAlignment="Left" Margin="21,12,0,0" Name="label1" VerticalAlignment="Top" Width="70" />
<Label Content="已用内存" Height="28" HorizontalAlignment="Left" Margin="97,12,0,0" Name="label6" VerticalAlignment="Top" Width="70" />
<TextBlock Height="23" HorizontalAlignment="Left" Margin="173,12,0,0" Name="textBlock1" Text="TextBlock" VerticalAlignment="Top" />

</Grid>
</Window>

 .cs文件

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.Diagnostics;

namespace WpfApplication2
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
[System.Runtime.InteropServices.DllImportAttribute(
"user32.dll", EntryPoint = "MessageBoxA")]
public static extern int MessageBoxA(
[System.Runtime.InteropServices.InAttribute()]
System.IntPtr hWnd,
[System.Runtime.InteropServices.InAttribute()]
[System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.LPStr)]
string lpText,
[System.Runtime.InteropServices.InAttribute()]
[System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.LPStr)]
string lpCaption,
uint Type);

[System.Runtime.InteropServices.DllImportAttribute(
"kernel32.dll", EntryPoint = "Beep")]
[
return: System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.Bool)]
public static extern bool Beep(uint dwFreq, uint dwDuration);

[System.Runtime.InteropServices.DllImportAttribute(
"winmm.dll", EntryPoint = "PlaySoundW")]
[
return: System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.Bool)]
public static extern bool PlaySoundW([System.Runtime.InteropServices.InAttribute()] [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.LPWStr)] string pszSound, System.IntPtr hmod, uint fdwSound);


[System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential)]
public struct MEMORYSTATUS
{

/// DWORD->unsigned int
public uint dwLength;

/// DWORD->unsigned int
public uint dwMemoryLoad;

/// SIZE_T->ULONG_PTR->unsigned int
public uint dwTotalPhys;

/// SIZE_T->ULONG_PTR->unsigned int
public uint dwAvailPhys;

/// SIZE_T->ULONG_PTR->unsigned int
public uint dwTotalPageFile;

/// SIZE_T->ULONG_PTR->unsigned int
public uint dwAvailPageFile;

/// SIZE_T->ULONG_PTR->unsigned int
public uint dwTotalVirtual;

/// SIZE_T->ULONG_PTR->unsigned int
public uint dwAvailVirtual;
}

[System.Runtime.InteropServices.DllImportAttribute(
"kernel32.dll", EntryPoint = "GlobalMemoryStatus")]
public static extern void GlobalMemoryStatus([System.Runtime.InteropServices.OutAttribute()] out MEMORYSTATUS lpBuffer);


public MainWindow()
{
InitializeComponent();
}

private void BTN_Click(object sender, RoutedEventArgs e)
{
//MessageBoxA(IntPtr.Zero, "text", "caption",0); //第一个参数要求是指针型,一般容易写成NULL,是不对的
//Beep(100, 30);
//PlaySoundW(@"D:\wpftestmusic\2.wav", IntPtr.Zero,1);
MEMORYSTATUS mm = new MEMORYSTATUS();
//unsafe
//{
// int* p;
// int a = 10;
// p = &a;
// label1.Content = (*p).ToString();
// label1.Content = (int)p; //强制将object转换为int型

//}
GlobalMemoryStatus(out mm); //out,表示执行该函数后的返回结果是一个struct型,返回给了mm

label6.Content
= mm.dwMemoryLoad.ToString()+"%"; //已用内存占百分比


Process[] p;
p
= System.Diagnostics.Process.GetProcesses();
//dataGrid1.DataContext = p;
//foreach (var i in p)
//{
// textBlock1.Text += i.ToString();
//}



}







}

public class ProcessObject //构造原型类
{
public string processname;
public string memoryused;
public ProcessObject()
{ }
public ProcessObject(string value1,string value2)
{
this.processname = value1;
this.memoryused = value2;
}
public string ProcessName
{
get { return processname; }
set { processname = value; }
}
public string MemoryUsed
{
get { return memoryused; }
set { memoryused = value; }
}
}
public class PO : List<ProcessObject> //将原型类初始化并赋值
{
public PO()
{
Process[] p;
p
= System.Diagnostics.Process.GetProcesses();
foreach (var i in p)
{
Add(
new ProcessObject(i.ProcessName, (i.PrivateMemorySize64).ToString())); //PrivateMemorySize64单位字节
}
}
}
}
posted on 2011-05-23 19:58  人的本质是什么?  阅读(271)  评论(0)    收藏  举报