WPF学习

来源:互联网 发布:单片机校验位计算 编辑:程序博客网 时间:2024/05/29 17:41

 

. 简单学习

 

1. WPF的启动项

 

App.xaml文件的内容大致如下:

 <Application x:Class="WpfApplicationLifeCycle.App"

 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

 

 StartupUri="Window1.xaml">

 

 <Application.Resources>

 </Application.Resources>

 </Application

 

 

或者自己定义一个类

 

static void Main()  {

Application app = new Application();

 

//调用Run方法,参数为启动的窗体对象

 Window2 win = new Window2();

 app.Run(win);

}

 

 

2. 应用程序关闭

 

(1)App.xaml中添加如下属性 ShutdownMode="OnExplicitShutdown"

或者(2

 

更改关闭模式必须要在调用app.Run()方法之前

Application app = new Application();

 Window2 win = new Window2();

 app.ShutdownMode = ShutdownMode.OnExplicitShutdown;

 app.Run(win);

 

 

 

3. 应用启动时添加事件

 

1. App.xaml文件中添加如下:

 

Startup="Application_Startup1"

 

2. ,在App.xaml.cs文件中添加事件的处理方法

 

public void Application_Startup1(object sender, StartupEventArgs e)

        {

            MessageBox.Show("the server begin to run ....");

        }

 

 

 

4. 窗体(Windows)上的一些事件

 

 public Window1()

        {

            InitializeComponent();

            this.Loaded += new RoutedEventHandler(window_loaded);   --用户初始化一些数据

            this.Closing += new System.ComponentModel.CancelEventHandler(windlow_closing);      --窗体关闭时的一些操作。   

        }

 

        private void windlow_closing(object sender, System.ComponentModel.CancelEventArgs e)

        {

            MessageBox.Show("closing .....");

        }

 

        private void window_loaded(object sender, RoutedEventArgs e)

        {

            textBox1.Text = "hello";

        }

 

 

 

5. 媒体操作

 

 

eg. 播放音乐

 

 <Canvas Height="217" HorizontalAlignment="Left" Margin="12,32,0,0" Name="canvas1" VerticalAlignment="Top" Width="258">

 

            <MediaElement Canvas.Left="37" Canvas.Top="16" Height="120" Name="mediaElement1" Width="160"  LoadedBehavior="Manual" Stretch="Fill"/>

 

            <TextBox Canvas.Left="54" Canvas.Top="142" Height="23" Name="txtMessage" Width="120" />

            <Button Canvas.Left="27" Canvas.Top="188" Content="开始" Height="23" Name="btnStart" Width="75" Click="btnStart_Click" />

            <Button Canvas.Left="122" Canvas.Top="188" Content="暂停" Height="23" Name="btnPause" Width="75" Click="btnPause_Click" />

        </Canvas>

 

 

 

 public MediaWindow()

        {

            InitializeComponent();

            this.Loaded += new RoutedEventHandler(MediaWindow_Loaded);   //MediaWindow加载时初始化音乐的路径

            this.Closing +=new System.ComponentModel.CancelEventHandler(MediaWindow_Closing);   //该方法用户在窗口关闭时,让用户确认是否关闭

        }

 

        public void MediaWindow_Loaded(object sender, RoutedEventArgs e)

        {

            mediaElement1.Source = new Uri(@"C:\Users\Public\Music\Sample Music\Sleep Away.mp3",UriKind.Absolute);

            txtMessage.Text = "Sleep Away";

        }

 

        private void btnStart_Click(object sender, RoutedEventArgs e)

        {

            mediaElement1.Play();     //播放事件

        }

 

        private void btnPause_Click(object sender, RoutedEventArgs e)

        {

            mediaElement1.Pause();   //暂停事件

        }

 

        public void MediaWindow_Closing(object sender, CancelEventArgs e)

        {

            MessageBoxButton mbb = MessageBoxButton.OKCancel;

            MessageBoxResult result = MessageBox.Show("确认关闭么?", "media Window", mbb);

            if(result==MessageBoxResult.Cancel){

                e.Cancel = true;    //取消退出

            }

        }

     

 

     . Panel

 

 1.   StackPanel

 

   是以堆叠的方式显示其中的控件

 

1.可以使用Orientation属性更改堆叠的顺序    Orientation="Vertical" 从上而下   Orientation="Horizontal" 从左到右

 

2. WrapPanel

 

  以流的形式由左到右,由上到下显示控件,其功能类似于Java AWT布局中的FlowLayout

 

3.  DockPanel

 

   以上、下、左、右、中为基本结构的布局方式,类似于Java AWT布局中的BorderLayout

 

 

    容器

 

1.         Grid是以表格形式组织控件的一种布局方式,和html中的table一模一样

 

Eg.  22列的Grid,第一行一列放两个Button。第一行第二列fang一个Button,

第二行的列跨两列

   

<Window x:Class="WPFPro1.GridWindw2"

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

        Title="GridWindw2" Height="359" Width="481">

    <Grid>

    

        <Grid Name="grid1" Margin="12,12,0,12">

            <Grid.RowDefinitions>

                <RowDefinition Height="137*" />

                <RowDefinition Height="159*" />

            </Grid.RowDefinitions>

            <Grid.ColumnDefinitions>

                <ColumnDefinition></ColumnDefinition>

                <ColumnDefinition></ColumnDefinition>

            </Grid.ColumnDefinitions>

           

            <Button Name="btn1" Content="ButtonA"Grid.Row="0" Grid.Column="0" VerticalAlignment="Top" />

            <Button Name="btn2" Content="ButtonB" Grid.Row="0" Grid.Column="0" VerticalAlignment="Bottom" />

            <Button Name="btn3" Content="ButtonC" Grid.Row="0" Grid.Column="1" />

            <Button Name="btn4" Content="ButtonD"Grid.Row="1" Grid.Column="0"  Grid.ColumnSpan="2" />

        </Grid>

    </Grid>

</Window>

 

   

 

2.       分割线(与列的使用一样,指定Grid的行和列即可)

 

<GridSplitter Width="3" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"

Grid.Row="0" Grid.Column="1" Grid.RowSpan="3"></GridSplitter>

3.     UniformGrid  为控件提供了一种简化的网格布局

单元格的数量取决于放入的控件的数量,且单元格一定是行、列数相同

<UniformGrid>

 <Button Content="ButtonA" />

 <Button Content="ButtonB" />

 <Button Content="ButtonC" />

 <Button Content="ButtonD" />

 <Button Content="ButtonE" />

 <Button Content="ButtonF" />

 <Button Content="ButtonG" />

 <Button Content="ButtonH" />

</UniformGrid>

. Canvas

1.     CanvasWPF中子元素的绝对定位的布局控件

其子元素使用WidthHeight定义元素的宽度和高度

使用Convas.LeftConvas.Right)、Convas.TopConvas.Bottom)定义与Convas容器的相对位置

<Canvas>

<Button Canvas.Left="10" Canvas.Top="10" Height="23" Width="75">LT</Button>

<Button Canvas.Right="10" Canvas.Top="10" Height="23" Width="75">RT</Button>

<Button Canvas.Left="10" Canvas.Bottom="10" Height="23" Width="75">LB</Button>

<Button Canvas.Right="10" Canvas.Bottom="10" Height="23" Width="75">RB</Button>

</Canvas>

 

2.     WPF中实现允许使用墨迹的控件

<InkCanvas>

 <InkCanvas.DefaultDrawingAttributes>

 <DrawingAttributes Color="Red" />

 </InkCanvas.DefaultDrawingAttributes>

 <Image Width="155" Height="155" InkCanvas.Left="10" InkCanvas.Top="10"

 Source="Logo2.png"/>

 </InkCanvas>

 

  Header Content

1.   Header 和 Content 属性的类型为 Object

Eg:设置buttonContent属性为Image

 

  <Button Canvas.Left="160" Canvas.Top="18" Height="23" Name="button1" Width="75" >

               <Image Width="75" Height="23" Source="C:\Users\Public\Pictures\Sample Pictures\Penguins.jpg"></Image>

           </Button>

 

2.   HeaderedContentControl模型

Eg: Expander控件

 

<Canvas Height="269" HorizontalAlignment="Left" Margin="61,486,0,0" Name="canvas2" VerticalAlignment="Top" Width="413">

            <Expander Canvas.Left="16" Canvas.Top="16"  Height="239" Name="expander1" Width="379">

                <Expander.Header>     

                    <Image Source="C:\Users\Public\Pictures\Sample Pictures\Penguins.jpg" Width="70" Height="40"></Image>

                </Expander.Header>

                <Expander.Content>

                    <Grid>

                        <Grid.RowDefinitions>

                            <RowDefinition Height="120*"></RowDefinition>

                            <RowDefinition Height="120*"></RowDefinition>

                        </Grid.RowDefinitions>

                        <Grid.ColumnDefinitions>

                            <ColumnDefinition ></ColumnDefinition>

                            <ColumnDefinition></ColumnDefinition>

                        </Grid.ColumnDefinitions>

                        <TextBlock Text="UserName" Grid.Row="0" Grid.Column="0" VerticalAlignment="Center"></TextBlock>

                        <TextBlock Text="Devin" Grid.Row="0" Grid.Column="1" VerticalAlignment="Center"></TextBlock>

                        <TextBlock Text="UserPass" Grid.Row="1" Grid.Column="0" VerticalAlignment="Center"></TextBlock>

                        <TextBlock  Name="txtUserPass" Grid.Row="1" Grid.Column="1" VerticalAlignment="Center"></TextBlock>

                    </Grid>

                  

                </Expander.Content>

            </Expander>

        </Canvas>

 

效果:

 

 

 

.  WPF资源

 

1.  通常使用 WPF 资源作为重用通常定义的对象和值的简单方法

 

<Window.Resources>

<SolidColorBrush x:Key="myBrush" Color="Gold" />

</Window.Resources>

<StackPanel>

<Button Margin="5" Content="Sample Button" Background="{StaticResource myBrush}" />

<Rectangle Margin="5" Width="100" Height="100" Fill="{StaticResource myBrush}" />

</StackPanel>

 

2.       WPF资源的定义位置

(1)如上的定义方法

(2)定义在App.xaml文件中,作为整个应用程序共享的资源,使用方法和上面一样

 

<Application.Resources>

 <SolidColorBrush Color="Gold" x:Key="myGoldBrush" />

</Application.Resources>

 

(3)定义在资源字典的XAML文件中


 

MyResourceDictionary.xaml 定义如下:

 

 

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

<SolidColorBrush x:Key="myWhiteBrush" Color="White" />

</ResourceDictionary>

 

 

使用时调用如下:

 

<Window.Resources>

<ResourceDictionary Source="MyResourceDictionary.xaml" />

</Window.Resources>

<StackPanel>

<Button Margin="5" Background="{StaticResource myWhiteBrush}">Sample Button</Button>

</StackPanel>

 

 

 

 

 

 

 

 

 

 

 

(七) 数据绑定

 

1.  最简单的binding

 

Eg.一个输入框输入值,另外一个输入框的值和第一个框保持一致

 

<TextBox Canvas.Left="17" Canvas.Top="21" Height="23" Name="textBox1" Width="120" Text="{Binding ElementName=slider1, Path=Value,UpdateSourceTrigger=PropertyChanged}" />

 

 <TextBox Canvas.Left="193" Canvas.Top="21" Height="23" Text="{Binding ElementName=textBox1, Path=Text}Name="textBox2" Width="120" />

 

 

ElementName 为控件的名称,Path为控件的属性名称

 

如果从后台进行上面的绑定的话代码如下:

Binding b =new Binding();

b.Source = textBox1;

b.Path = newPropertyPath("Text");

textBox2.SetBinding(TextBox.TextProperty, b);

 

 

2.  TextBox 和 Slider 进行绑定

 

当Slider进行滑动时TextBox中显示滑动的值,绑定如下

 

<TextBox Canvas.Left="17" Canvas.Top="21" Height="23" Name="textBox1" Width="120" Text="{Binding ElementName=slider1, Path=Value,UpdateSourceTrigger=PropertyChanged}" />

  

 

<Slider Grid.Column="1" Height="23" HorizontalAlignment="Left" Margin="64,392,0,0" Name="slider1" VerticalAlignment="Top" Width="327" />

 

 

 

 

 

 

3.       将一个List数组绑定到ListView

 

前台xaml中代码:

 

<ListView Height="207" HorizontalAlignment="Left" Margin="13,6,0,0" Name="listView1" VerticalAlignment="Top" Width="459" Grid.Column="1">

<ListView.View>

<GridView>

 <GridViewColumn Header="选择">

 <GridViewColumn.CellTemplate>

 <DataTemplate>

  <CheckBox Name="categoryChecked" Checked="categoryChecked_Checked" Tag="{Binding categoryId}"></CheckBox>

 </DataTemplate>

</GridViewColumn.CellTemplate>

 </GridViewColumn>

                          

<GridViewColumn Header="CategoryId"  DisplayMemberBinding="{Binding Path=categoryId}" ></GridViewColumn>

<GridViewColumn Header="CategoryName" DisplayMemberBinding="{Binding Path=categoryName}" ></GridViewColumn>

 <GridViewColumn Header="CategoryEngName"DisplayMemberBinding="{Binding Path=categoryEngName}" ></GridViewColumn>

</GridView>

</ListView.View>

</ListView>

 

 

后台绑定

 

List<Category> cList =Category.getCategories();

listView1.Items.Clear();    //防止绑定报错

listView1.ItemsSource = Category.getCategories();

 

 

4.       将一个DataTable进行绑定

 

前台:

 

<ListView Height="172" HorizontalAlignment="Left" Margin="28,14,0,0" Name="listView1" VerticalAlignment="Top" Width="350">

<ListView.View>

<GridView>

<GridViewColumn Header="?ª?Id"DisplayMemberBinding="{Binding Path=Order_Header_Id}"></GridViewColumn>

<GridViewColumn Header="?ª?Num"DisplayMemberBinding="{Binding Path=Cust_Po_Number}"></GridViewColumn>

 <GridViewColumn Header="¨ª¡ì??"DisplayMemberBinding="{Binding Path=Customer_Name}"></GridViewColumn>

</GridView>

 </ListView.View>

</ListView>

 

后台绑定代码:

 

DataTable[] dts = getData(1, 5); 

listView1.ItemsSource = dts[0].DefaultView;  //dts[0] 返回一个DataTable

 

 

0 0
原创粉丝点击