<Window x:Class="WpfDemo.XPASEDemo" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Height="377.631" Width="744.737" xmlns:sys="clr-namespace:System;assembly=mscorlib" xmlns:local="clr-namespace:WpfDemo" WindowStartupLocation="CenterScreen" Title="{x:Static local:XPASEDemo.windowTitle}" > <Window.Resources> <sys:String x:Key="mykey">Hello WPF Resources</sys:String> <Style x:Key="{x:Type Button}" TargetType="{x:Type Button}"> <Setter Property="Width" Value="60"></Setter> <Setter Property="Height" Value="36"></Setter> <Setter Property="Margin" Value="5"></Setter> </Style> </Window.Resources> <Grid Margin="0,0,25,28" RenderTransformOrigin="0.5,0.5"> <Grid.ColumnDefinitions> <ColumnDefinition Width="109*"/> <ColumnDefinition Width="247*"/> </Grid.ColumnDefinitions> <Grid.Background> <ImageBrush ImageSource="pic/1.jpg" Stretch="Uniform" Opacity="0.8"> </ImageBrush> </Grid.Background> <ComboBox x:Name="cmb1" HorizontalAlignment="Left" Margin="75,31,0,288" Width="149" IsReadOnly="True" Opacity="0.6" RenderTransformOrigin="0.5,0.5" SelectionChanged="ComboBox_SelectionChanged" Grid.Column="1"> <ComboBox.Background> <LinearGradientBrush EndPoint="0,1" StartPoint="0,0"> <GradientStop Color="#FFF0F0F0" Offset="0"/> <GradientStop Color="#FF138F95" Offset="1"/> </LinearGradientBrush> </ComboBox.Background> <ComboBoxItem Content="111" Opacity="0.7" RenderTransformOrigin="0.5,0.5" HorizontalAlignment="Left" Width="185" Margin="0,0,-38,0"> </ComboBoxItem> </ComboBox> <ListBox HorizontalAlignment="Left" Height="137" Margin="62,106,0,0" VerticalAlignment="Top" Width="158" RenderTransformOrigin="0.5,0.5" Opacity="0.5" Grid.Column="1"> <ListBox.ItemsSource> <x:Array Type="sys:String"> <sys:String>Tim</sys:String> <sys:String>Jack</sys:String> <sys:String>Tom</sys:String> <sys:String>Jerry</sys:String> </x:Array> </ListBox.ItemsSource> </ListBox> <local:Button1 x:Name="btn2" Margin="100,100,0,0" Width="149" Height="50" HorizontalAlignment="Left" VerticalAlignment="Top" Content="測試" MyWindowType="{x:Type TypeName=local:BindingValidationDemo}" RenderTransformOrigin="0.5,0.5" Click="btn2_Click" Grid.ColumnSpan="2"> </local:Button1> <Button Content="Button" HorizontalAlignment="Left" Margin="326,188,0,0" VerticalAlignment="Top" Width="75" Grid.Column="1"/> <Button Content="Button" HorizontalAlignment="Left" Margin="326,229,0,0" VerticalAlignment="Top" Width="75" Grid.Column="1"/> <Button Content="Button" Style="{x:Null}" HorizontalAlignment="Left" Margin="326,270,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click_1" Grid.Column="1"/> <TextBox x:Name="tb1" Text="{x:Static local:XPASEDemo.ShowText}" HorizontalAlignment="Left" Height="23" Margin="89,190,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120" /> <GroupBox HorizontalAlignment="Left" Margin="249,15,-134,0" VerticalAlignment="Top" Grid.Column="1"> <GroupBox.Header> <Image Source="./pic/4.jpg" Width="20" Height="20"></Image> </GroupBox.Header> <TextBlock TextWrapping="Wrap" Margin="5" Width="249"> <TextBlock.Text> agoagreatAmericaninwhosesymbolic1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111 </TextBlock.Text> </TextBlock> </GroupBox> <ListBox x:Name="lb2" HorizontalAlignment="Left" Margin="127,83,0,0" VerticalAlignment="Top" Height="150" Width="122" SelectionChanged="lb2_SelectionChanged" Grid.Column="1"/> </Grid> </Window>
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; 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; namespace WpfDemo { /// <summary> /// MainWindow.xaml 的交互逻辑 /// </summary> public partial class XPASEDemo : Window { public static string windowTitle = "WPF Demo"; //public static string ShowText { get { return "静态显示信息"; } set; } public static string ShowText { get; set; } public XPASEDemo() { ShowText = "静态显示信息"; InitializeComponent(); Employee e1 = new Employee(); e1.name = "張三"; e1.age = 10; Employee e2 = new Employee(); e2.name = "李四"; e2.age = 20; Employee e3 = new Employee(); e3.name = "王五"; e3.age = 30; List<Employee> list = new List<Employee> {e1,e2,e3 }; lb2.ItemsSource = list; lb2.DisplayMemberPath = "name"; lb2.SelectedValuePath = "age"; } private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e) { MessageBox.Show(this.cmb1.SelectedItem.ToString()); } private void Button_Click(object sender, RoutedEventArgs e) { string str = this.FindResource("mykey") as string; MessageBox.Show(str); } private void btn2_Click(object sender, RoutedEventArgs e) { //Thread d = new Thread(delegate() { // this.btn2.Content = "非UI線程調用控件"; //}); //d.Start(); this.Dispatcher.Invoke(delegate() { this.btn2.Content = "非UI線程調用控件"; }); } private void Button_Click_1(object sender, RoutedEventArgs e) { MessageBox.Show(ShowText+" "+this.tb1.Text); } private void Button_Click_2(object sender, RoutedEventArgs e) { Button btn = sender as Button; DependencyObject level1= VisualTreeHelper.GetParent(btn); MessageBox.Show(level1.GetType().ToString()); } private void lb2_SelectionChanged(object sender, SelectionChangedEventArgs e) { MessageBox.Show(lb2.SelectedValue.ToString()); } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; namespace WpfDemo { class Button1:Button { public Type MyWindowType { get; set; } protected override void OnClick() { base.OnClick(); Window win=Activator.CreateInstance(MyWindowType) as Window; if(win!=null) { win.ShowDialog(); } } } }