WPF点滴

1 设置窗体的最大化,而且无边框

1   <Style x:Key="WindowsStyle" TargetType="Window">

2             <Setter Property="Height" Value="1024"></Setter>

3             <Setter Property="Width" Value="1280"></Setter>

4             <Setter Property="WindowStyle" Value="None"></Setter>

5             <Setter Property="WindowState" Value="Maximized"></Setter>

6         </Style>
View Code

2 设置按钮的透明,无边框

1     <Style x:Key="ButtonStyle" TargetType="Button">

2             <Setter Property="Background" Value="Transparent"></Setter>

3             <Setter Property="BorderBrush" Value="Transparent"></Setter>

4         </Style>
View Code

3, 添加样式

 1 <Window x:Class="ZhiHeng.IntelligentExpress.WpfUI.Views.MainForm"

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

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

 4         Title="MainForm" Style="{StaticResource WindowsStyle}" >

 5     <Canvas>

 6 

 7   <Button Canvas.Left="121" Name="btnReturnShipment" Canvas.Top="887" Height="108" Width="86"  Style="{StaticResource ButtonStyle}" />

 8    </Canvas>

 9 

10 </Window>
View Code

4 设置程序的启动项

 首先在appliaction.xmal文件中修改StartupURL为Startup='AppLicationStart'(AppLicationStart为事件名称,可随便定义)

然后在cs 文件中定义事件

1 private void AppLicationStart(object sender, StartupEventArgs e) 

2         {

3            //加入自己的逻辑

4             MainForm main = new MainForm();

5             main.Show();

6         }      
View Code

5 获得用户控件所在的窗体

1  Window win=  Window.GetWindow(this);
View Code

 6 WPF 程序关闭方法

 Application.Current.Shutdown();
WindowsFrom中 则为Application.Exit();
也可是使用 Environment.Exit(1);//终止进程

7 checkbox 

        wpf 中的checkbox 不想winfrom中的有2中状态,而是3种,分别是:null , false,true(不知道为毛这样设置),控制状态的有一个属性为IsThreeState

    当为false时,获取到的值为True和false2种状态,否则就是3中,默认为false

不知道为啥我这个checkbox 当选中和为选中的时候显示没区别,顿时感觉整个人都不好啦,,,,,,

 

解决方案为它家一个触发器

1 <CheckBox Content="是否为3G" Name="chk3G" IsThreeState="False" Canvas.Left="929" Canvas.Top="851" FontSize="36" Foreground="White" FontFamily="SimSun">

 2             <CheckBox.Resources>

 3                 <Style TargetType="CheckBox">

 4                     <Style.Triggers>

 5                         <Trigger Property="IsChecked" Value="True">

 6                             <Trigger.Setters>

 7                                 <Setter Property="Background" Value="Red" />

 8                             </Trigger.Setters>

 9                         </Trigger>

10                     </Style.Triggers>

11                 </Style>

12             </CheckBox.Resources>

13         </CheckBox>
View Code

 

  

    这回感觉好多啦。。。。。。

8 wpf中监视网络状态,并实现图片的变化

         先上效果图

在线        离线 

     

 1  NetworkChange.NetworkAvailabilityChanged += new NetworkAvailabilityChangedEventHandler(NetworkChange_NetworkAddressChanged);//监视网络的变化

 2 

 3 void NetworkChange_NetworkAddressChanged(object sender, NetworkAvailabilityEventArgs e)

 4         {

 5             

 6             this.Dispatcher.Invoke((Action)(() => {

 7                 Image t = new Image();

 8                 System.Drawing.Bitmap bmp = Properties.Resources.connected;//连接的图片

 9                 IntPtr hBitmap = bmp.GetHbitmap();

10                 System.Windows.Media.ImageSource BitmapCon = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(hBitmap, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());

11 

12                 System.Drawing.Bitmap bmp1 = Properties.Resources.unconnected;//断开的图片

13                 IntPtr hBitmap1 = bmp1.GetHbitmap();

14                 System.Windows.Media.ImageSource BitmapUncon = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(hBitmap1, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());

15                 Program.OnLine = e.IsAvailable; pic_OnLine.Source = Program.OnLine ? BitmapCon : BitmapUncon; 

16             }));

17         }
View Code

 

 9,C#后台设置按钮的背景图片

 

  this.btnTrue.Background = new ImageBrush { ImageSource = new BitmapImage(new Uri("pack://application:,,,/Images/btngreennomal.png")) }; 

 10, 设置textBox中的文本为选中状态

  

1 txtCardNum.Focus();

2             txtCardNum.SelectionStart = 0;  //设置起始位置 

3             txtCardNum.SelectionLength = 12;  //设置长度
View Code

 

你可能感兴趣的:(WPF)