WPF 02

Grid容器

分行和分列

<Grid><Grid.RowDefinitions><!--2*:此行是下面一行的两倍--><RowDefinition Height="2*"/><RowDefinition/></Grid.RowDefinitions><Grid.ColumnDefinitions><ColumnDefinition/><ColumnDefinition/></Grid.ColumnDefinitions><Border Background="blue"/><Border Grid.Row="1" Background="red"/><Border Grid.Column="1" Background="yellow"/><Border Grid.Row="1" Grid.Column="1"  Background="green"/></Grid>

跨行和跨列

<!--跨行和跨列--><Border Grid.ColumnSpan="2" Grid.RowSpan="2" Background="blue"/>

stackPanel:默认垂直排列

局部容器,一般修饰部分空间的元素排布

<StackPanel>
//<StackPanel Orientation="Horizontal"><Button Height="50" Width="100"/><Button Height="50" Width="100"/><Button Height="50" Width="100"/><Button Height="50" Width="100"/><Button Height="50" Width="100"/>
</StackPanel>

WrapPanel :默认水平排序且换行

<WrapPanel Grid.Row="1"><Button Height="50" Width="100"/><Button Height="50" Width="100"/><Button Height="50" Width="100"/><Button Height="50" Width="100"/><Button Height="50" Width="100"/>
</WrapPanel>

WrapPanel可以自动换行

DockPanel:默认最后一个元素填充剩余所有空间

<DockPanel><Button Height="50" Width="100"/><Button Height="50" Width="100"/><Button Height="50" Width="100"/><Button Height="50" Width="100"/><Button Height="50" Width="100"/>
</DockPanel>

取消最后一个元素填充剩余所有空间:

<DockPanel LastChildFill="False">

DockPanel可以停靠

<DockPanel LastChildFill="False"> <Button Height="50" Width="100" DockPanel.Dock="Bottom"/><Button Height="50" Width="100" DockPanel.Dock="Left"/><Button Height="50" Width="100" DockPanel.Dock="Right"/><Button Height="50" Width="100" DockPanel.Dock="Top"/><Button Height="50" Width="100" DockPanel.Dock="Bottom"/>
</DockPanel>

UniformGrid: 在有限的空间内根据控件均分剩余空间

<UniformGrid Rows="3" Columns="3"><Button/><Button/><Button/><Button/><Button/><Button/><Button/><Button/><Button/>
</UniformGrid>

ctrl+k+d:自动格式化代码

课程案例1

模块划分代码:

<Window x:Class="WpfDay01.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:WpfDay01"mc:Ignorable="d"Title="MainWindow" Height="450" Width="800"><Grid><Grid.RowDefinitions><RowDefinition Height="50"/><RowDefinition/></Grid.RowDefinitions><Border Background="#7378DB"/><Grid Grid.Row="1"><Grid.ColumnDefinitions><ColumnDefinition Width="200"/><ColumnDefinition/></Grid.ColumnDefinitions><Border Margin="5" Background="blue"/><Grid Grid.Column="1"><Grid.ColumnDefinitions><ColumnDefinition/><ColumnDefinition/><ColumnDefinition/><ColumnDefinition/><ColumnDefinition/></Grid.ColumnDefinitions><Grid.RowDefinitions><RowDefinition/><RowDefinition/><RowDefinition/></Grid.RowDefinitions><Border Margin="5" Background="#7378DB"/><Border Margin="5" Grid.Column="1" Background="#4598CE"/><Border Margin="5" Grid.Column="2"  Background="#E080CE"/><Border Margin="5" Grid.Column="3" Background="#50B9B8"/><Border Margin="5" Grid.Column="4" Background="#E07A7D"/><Border Margin="5"  Grid.Row="1" Grid.ColumnSpan="3" Background="green"/><Border Margin="5"  Grid.Row="1" Grid.Column="3" Grid.ColumnSpan="2" Background="Yellow"/><Border Margin="5"  Grid.Row="2" Grid.ColumnSpan="3" Background="red"/><Border Margin="5"  Grid.Row="3" Grid.Column="3" Grid.ColumnSpan="2" Background="Blue"/></Grid></Grid></Grid>
</Window>

练习案例1

<!--<Window x:Class="WpfDay01.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:WpfDay01"mc:Ignorable="d"Title="MainWindow" Height="450" Width="800"><Grid><Grid.RowDefinitions><RowDefinition Height="50"/><RowDefinition/></Grid.RowDefinitions><Border Background="#7378DB"/><Grid Grid.Row="1"><Grid.ColumnDefinitions><ColumnDefinition Width="200"/><ColumnDefinition/></Grid.ColumnDefinitions><Border Margin="5" Background="blue"/><Grid Grid.Column="1"><Grid.ColumnDefinitions><ColumnDefinition/><ColumnDefinition/><ColumnDefinition/><ColumnDefinition/><ColumnDefinition/></Grid.ColumnDefinitions><Grid.RowDefinitions><RowDefinition/><RowDefinition/><RowDefinition/></Grid.RowDefinitions><Border Margin="5" Background="#7378DB"/><Border Margin="5" Grid.Column="1" Background="#4598CE"/><Border Margin="5" Grid.Column="2"  Background="#E080CE"/><Border Margin="5" Grid.Column="3" Background="#50B9B8"/><Border Margin="5" Grid.Column="4" Background="#E07A7D"/><Border Margin="5"  Grid.Row="1" Grid.ColumnSpan="3" Background="green"/><Border Margin="5"  Grid.Row="1" Grid.Column="3" Grid.ColumnSpan="2" Background="Yellow"/><Border Margin="5"  Grid.Row="2" Grid.ColumnSpan="3" Background="red"/><Border Margin="5"  Grid.Row="3" Grid.Column="3" Grid.ColumnSpan="2" Background="Blue"/></Grid></Grid></Grid>
</Window>-->
<Window x:Class="WpfDay01.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:WpfDay01"mc:Ignorable="d"Title="MainWindow" Height="450" Width="800"><Grid><Grid.RowDefinitions><RowDefinition Height="50"/><RowDefinition Height="100"/><RowDefinition/></Grid.RowDefinitions><Border Background="LightCyan"/><Grid Grid.Row="1"><Grid.ColumnDefinitions><ColumnDefinition/><ColumnDefinition/><ColumnDefinition/><ColumnDefinition/><ColumnDefinition/></Grid.ColumnDefinitions><Border Margin="5" Background="#219AFB"/><Border Margin="5" Grid.Column="1" Background="#64B522"/><Border Margin="5" Grid.Column="2" Background="#FF9F00"/><Border Margin="5" Grid.Column="3" Background="#50B9B8"/><Border Margin="5" Grid.Column="4" Background="#E07A7D"/></Grid><Grid Grid.Row="2"><Grid.ColumnDefinitions><ColumnDefinition Width="0.667*"/><ColumnDefinition/></Grid.ColumnDefinitions><Grid><Grid.RowDefinitions><RowDefinition/><RowDefinition/><RowDefinition/></Grid.RowDefinitions><Grid.ColumnDefinitions><ColumnDefinition/><ColumnDefinition/></Grid.ColumnDefinitions><Border Margin="5" Grid.ColumnSpan="2" Background="blue"/><Border Margin="5" Grid.Row="1" Background="red"/><Border Margin="5" Grid.Row="1" Grid.Column="1" Background="yellow"/><Border Margin="5" Grid.Row="2" Background="pink"/><Border Margin="5" Grid.Row="2" Grid.Column="1" Background="green"/></Grid><Grid Grid.Column="1"><Grid.RowDefinitions><RowDefinition Height="1.5*"/><RowDefinition/></Grid.RowDefinitions><Grid.ColumnDefinitions><ColumnDefinition/><ColumnDefinition/></Grid.ColumnDefinitions><Border Margin="5" Background="AntiqueWhite"/><Border Margin="5" Grid.Column="1" Background="Gray"/><Border Margin="5" Grid.Row="1" Grid.ColumnSpan="2" Background="SaddleBrown"/></Grid></Grid></Grid>
</Window>

样式的使用方法

<Window x:Class="WpfDay01.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:WpfDay01"mc:Ignorable="d"Title="MainWindow" Height="450" Width="800"><Window.Resources><Style x:Key="BaseButtonStyle" TargetType="Button"><Setter Property="Foreground" Value="White"/><Setter Property="Background" Value="Blue"/></Style><Style x:Key="ButtonStyle" TargetType="Button" BasedOn="{StaticResource BaseButtonStyle}"><Setter Property="Content" Value="text"/></Style></Window.Resources><Grid><StackPanel><Button Content="button1" Background="Red" Style="{StaticResource ButtonStyle}"/><Button Content="button2" Style="{StaticResource ButtonStyle}"/><Button Content="button3" Style="{StaticResource ButtonStyle}"/></StackPanel></Grid>
</Window>

数据模板

案例1

MainWindow.xaml

<Window x:Class="WpfDay01.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:WpfDay01"mc:Ignorable="d"Title="MainWindow" Height="450" Width="800"><Grid><ListBox x:Name="list"><ListBox.ItemTemplate><DataTemplate><StackPanel Orientation="Horizontal"><Border Width="10" Height="10" Background="{Binding Code}"/><TextBlock Margin="10, 0" Text="{Binding Name}"/></StackPanel></DataTemplate></ListBox.ItemTemplate></ListBox></Grid>
</Window>

MainWindow.xaml.cs

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;
namespace WpfDay01
{public partial class MainWindow : Window{public MainWindow(){InitializeComponent();List<Color> test = new List<Color>();test.Add(new Color() { Code= "#FF69B4", Name= "热情的粉红" });test.Add(new Color() { Code= "#C71585", Name= "适中的紫罗兰红色" });test.Add(new Color() { Code= "#DA70D6", Name= "兰花的紫色" });list.ItemsSource = test;}}public class Color{public string Code { get; set; }public string Name { get; set; }}
}

案例2

MainWindow.xaml

<Window x:Class="WpfDay01.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:WpfDay01"mc:Ignorable="d"Title="MainWindow" Height="450" Width="800"><Grid><DataGrid x:Name="grid"AutoGenerateColumns="False"CanUserAddRows="False"><DataGrid.Columns><DataGridTextColumn Binding="{Binding Code}" Header="Code"/><DataGridTextColumn Binding="{Binding Name}" Header="Name"/><DataGridTemplateColumn Header="操作"><DataGridTemplateColumn.CellTemplate><DataTemplate><StackPanel Orientation="Horizontal"><Border Width="10" Height="10" Background="{Binding Code}"/><TextBlock Margin="10 0" Text="{Binding Name}"/></StackPanel></DataTemplate></DataGridTemplateColumn.CellTemplate></DataGridTemplateColumn></DataGrid.Columns></DataGrid></Grid>
</Window>

MainWindow.xaml.cs

namespace WpfDay01
{public partial class MainWindow : Window{public MainWindow(){InitializeComponent();List<Color> test = new List<Color>();test.Add(new Color() { Code= "#FF69B4", Name= "热情的粉红" });test.Add(new Color() { Code= "#C71585", Name= "适中的紫罗兰红色" });test.Add(new Color() { Code= "#DA70D6", Name= "兰花的紫色" });grid.ItemsSource = test;}}public class Color{public string Code { get; set; }public string Name { get; set; }}
}

修改dataTemplete

<DataTemplate><!--<StackPanel Orientation="Horizontal"><Border Width="10" Height="10" Background="{Binding Code}"/><TextBlock Margin="10 0" Text="{Binding Name}"/></StackPanel>--><StackPanel Orientation="Horizontal"><Button Content="添加"/><Button Content="修改"/><Button Content="删除"/></StackPanel>
</DataTemplate>

绑定方法

双向数据绑定关系

<Window x:Class="WpfDay01.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:WpfDay01"mc:Ignorable="d"Title="MainWindow" Height="450" Width="800"><Grid><StackPanel><Slider x:Name="slider" Margin="5"/><TextBox Text="{Binding ElementName=slider, Path=Value}" Margin="5"/><TextBox Text="{Binding ElementName=slider, Path=Value}" Margin="5"/><TextBox Text="{Binding ElementName=slider, Path=Value}" Margin="5"/></StackPanel></Grid>
</Window>

绑定模式

<StackPanel><Slider x:Name="slider" Margin="5"/><TextBox Text="{Binding ElementName=slider, Path=Value, Mode=OneTime}" Margin="5"/><TextBox Text="{Binding ElementName=slider, Path=Value, Mode=OneWay}" Margin="5"/><TextBox Text="{Binding ElementName=slider, Path=Value, Mode=OneWayToSource}" Margin="5"/><TextBox Text="{Binding ElementName=slider, Path=Value, Mode=TwoWay}" Margin="5"/></StackPanel>

TextBox数据绑定

MainWindow.xaml

<Window x:Class="WpfDay01.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:WpfDay01"mc:Ignorable="d"Title="MainWindow" Height="450" Width="800"><Grid><StackPanel><TextBox Text="{Binding Name}" Margin="5"/></StackPanel></Grid>
</Window>

创建类test

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace WpfDay01
{class Test{public string Name { get; set; }}
}

MainWindow.xaml.cs

namespace WpfDay01
{public partial class MainWindow : Window{public MainWindow(){InitializeComponent();this.DataContext = new Test(){Name = "张三"};}}public class Color{public string Code { get; set; }public string Name { get; set; }}
}

ICommand使用方法

业务逻辑代码:MainViewModel.cs

UI代码:MainWindow.xaml

实现ICommand中的接口:MyCommand.cs

将View与ViewModel挂钩:MainWindow.xaml.cs

MyCommand.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;namespace WpfDay01
{public class MyCommand : ICommand{Action executeAction;Func<bool> canExecuteAction;public MyCommand(Action action,Func<bool> canExcuteAction){executeAction = action;canExecuteAction = canExcuteAction;}public event EventHandler CanExecuteChanged;public bool CanExecute(object parameter){return canExecuteAction();}public void Execute(object parameter){executeAction();}}
}

MainViewModel.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
namespace WpfDay01
{public class MainViewModel{public MainViewModel(){ShowCommand = new MyCommand(Show,canExcuteAction);ShowCommand2 = new MyCommand(show2, canExcuteAction2);}private string myname="a";public string MyName{get { return myname; }set { myname = value; }}private bool canExcuteAction(){if (string.IsNullOrEmpty(MyName))return false;return true;}private bool canExcuteAction2(){   return true;}public MyCommand ShowCommand { get; set; }public MyCommand ShowCommand2 { get; set; }public void Show(){MessageBox.Show("点击了按钮!");}public void show2(){MyName = "b";MessageBox.Show(MyName);}}
}

MainWindow.xaml

<Window x:Class="WpfDay01.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:WpfDay01"mc:Ignorable="d"Title="MainWindow" Height="450" Width="800"><StackPanel><TextBox Text="{Binding MyName}"></TextBox><Button Width="100" Height="100" Content="Button" Command="{Binding ShowCommand}"/><Button Width="100" Height="100" Content="Button" Command="{Binding ShowCommand2}"/></StackPanel>
</Window>

MainWindow.xaml.cs

DataContext:连接View与ViewModel挂钩。

public partial class MainWindow : Window{public MainWindow(){InitializeComponent();this.DataContext = new MainViewModel();}private void Button_Click(object sender, RoutedEventArgs e){MessageBox.Show("hhhh");}}

更新UI界面:INotifyPropertyChanged

业务逻辑代码:MainViewModel.cs

UI代码:MainWindow.xaml

实现ICommand中的接口:MyCommand.cs

将View与ViewModel挂钩:MainWindow.xaml.cs

INotifyPropertyChanged更新界面:ViewModelBase.cs

ViewModelBase.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
namespace WpfDay01
{class ViewModelBase : INotifyPropertyChanged{public event PropertyChangedEventHandler PropertyChanged;public void OnPropertyChanged([CallerMemberName]string propertyName=""){PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));}}
}

MyCommand.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
namespace WpfDay01
{class MyCommand : ICommand{Action executeAction;public MyCommand(Action action){executeAction = action;     }public event EventHandler CanExecuteChanged;public bool CanExecute(object parameter){return true;}public void Execute(object parameter){executeAction();}}
}

MainViewModel.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;namespace WpfDay01
{class MainViewModel: ViewModelBase{public MainViewModel(){MyName = "Hello";ShowCommand = new MyCommand(Show);ShowCommand2 = new MyCommand(show2);}private string myname;public string MyName{get { return myname; }set { myname = value;OnPropertyChanged();}}private string myTitle;public string MyTitle{get { return myTitle; }set { myTitle = value; OnPropertyChanged(); }}public MyCommand ShowCommand { get; set; }public MyCommand ShowCommand2 { get; set; }public void Show(){MyName = "点击了按钮";MyTitle = "myTitle";MessageBox.Show("点击了按钮!");}public void show2(){MyName = "b";MyTitle = "myTitle2";MessageBox.Show(MyName);}}
}

MainWindow.xaml

<Window x:Class="WpfDay01.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:WpfDay01"mc:Ignorable="d"Title="MainWindow" Height="450" Width="800"><StackPanel><TextBox Text="{Binding MyName}"></TextBox><TextBox Text="{Binding MyTitle}"></TextBox><Button Width="100" Height="100" Content="Button" Command="{Binding ShowCommand}"/><Button Width="100" Height="100" Content="Button" Command="{Binding ShowCommand2}"/></StackPanel>
</Window>

MainWindow.xaml.cs

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;
namespace WpfDay01
{public partial class MainWindow : Window{public MainWindow(){InitializeComponent();this.DataContext = new MainViewModel();}private void Button_Click(object sender, RoutedEventArgs e){MessageBox.Show("hhhh");}}
}

MvvmLight框架

使用MvvmLight框架后不需要自定义MyCommand和ViewModelBase,直接调用即可。

MainViewModel.cs

using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
namespace WpfDay02
{class MainViewModel: ViewModelBase{public MainViewModel(){MyName = "hello!";ShowCommand = new RelayCommand(Show);}public RelayCommand ShowCommand { get; }private string myName;public string MyName{get { return myName; }set { myName = value; RaisePropertyChanged(); }}public void Show(){MyName = "按下了按钮!";MessageBox.Show("按下了按钮!");}}
}

MainWindow.xaml

<Window x:Class="WpfDay02.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:WpfDay02"mc:Ignorable="d"Title="MainWindow" Height="450" Width="800"><Grid><TextBox Text="{Binding MyName}"></TextBox><Button Height="100" Width="100" Content="btn" Command="{Binding ShowCommand}"></Button></Grid>
</Window>

MainWindow.xaml.cs

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;
namespace WpfDay02
{public partial class MainWindow : Window{public MainWindow(){InitializeComponent();this.DataContext = new MainViewModel();}}
}
将某个控件的内容关联到另外控件上(泛型的使用)

MainViewModel.cs

using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
namespace WpfDay02
{class MainViewModel: ViewModelBase{public MainViewModel(){MyName = "hello!";ShowCommand = new RelayCommand<string>(Show);}public RelayCommand<string> ShowCommand { get; }private string myName;public string MyName{get { return myName; }set { myName = value; RaisePropertyChanged(); }}public void Show(string content){MyName = "按下了按钮!";MessageBox.Show(content);}}
}

MainWindow.xaml

<Window x:Class="WpfDay02.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:WpfDay02"mc:Ignorable="d"Title="MainWindow" Height="450" Width="800"><Grid><StackPanel><TextBox Text="{Binding MyName}" Height="40" Margin="10"></TextBox><TextBox x:Name="txtInput" Height="40" Margin="10"></TextBox><Button Height="100" Width="100" Content="btn" Command="{Binding ShowCommand}"CommandParameter="{Binding ElementName=txtInput, Path=Text}"></Button></StackPanel></Grid>
</Window>

将textBox中的内容通过button按钮显示到messageBox中。

使用Messenger注册接收消息

修改部分:MainWindow.xaml.cs和MainViewModel.cs

MainWindow.xaml.cs

using GalaSoft.MvvmLight.Messaging;
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;
namespace WpfDay02
{public partial class MainWindow : Window{public MainWindow(){InitializeComponent();this.DataContext = new MainViewModel();//注册一个接收string类型参数的消息,地址是Token1Messenger.Default.Register<string>(this, "Token1", Show);}void Show(string value){MessageBox.Show(value);}}
}

MainViewModel.cs

using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using GalaSoft.MvvmLight.Messaging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
namespace WpfDay02
{class MainViewModel: ViewModelBase{public MainViewModel(){MyName = "hello!";ShowCommand = new RelayCommand<string>(Show);}public RelayCommand<string> ShowCommand { get; }private string myName;public string MyName{get { return myName; }set { myName = value; RaisePropertyChanged(); }}public void Show(string content){MyName = "按下了按钮!";//MessageBox.Show(content);//给Token1的地址发送一个string类型的值 contentMessenger.Default.Send(content, "Token1");}}
}

效果与上面案例相同

CommunityToolkit.Mvvm框架

MainViewModel.cs

using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using CommunityToolkit.Mvvm.Messaging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;namespace WpfDay02
{class MainViewModel: ObservableObject {public MainViewModel(){MyName = "hello!";ShowCommand = new RelayCommand<string>(Show);}public RelayCommand<string> ShowCommand { get; }private string myName;public string MyName{get { return myName; }set { myName = value; OnPropertyChanged(); }}public void Show(string content){MyName = "按下了按钮!";//MessageBox.Show(content);WeakReferenceMessenger.Default.Send(content, "Token1");           }}
}

MainWindow.xaml.cs

using CommunityToolkit.Mvvm.Messaging;
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 Sysatem.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace WpfDay02
{public partial class MainWindow : Window{public MainWindow(){InitializeComponent();this.DataContext = new MainViewModel();WeakReferenceMessenger.Default.Register<string, string>(this, "Token1", (s, e)=>{MessageBox.Show(e);});} }
}

MainWindow.xaml

<Window x:Class="WpfDay02.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:WpfDay02"mc:Ignorable="d"Title="MainWindow" Height="450" Width="800"><Grid><StackPanel><TextBox Text="{Binding MyName}" Height="40" Margin="10"></TextBox><TextBox x:Name="txtInput" Height="40" Margin="10"></TextBox><Button Height="100" Width="100" Content="btn" Command="{Binding ShowCommand}"CommandParameter="{Binding ElementName=txtInput, Path=Text}"></Button></StackPanel>   </Grid>
</Window>

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/92526.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

【AI视野·今日Robot 机器人论文速览 第四十四期】Fri, 29 Sep 2023

AI视野今日CS.Robotics 机器人学论文速览 Fri, 29 Sep 2023 Totally 38 papers &#x1f449;上期速览✈更多精彩请移步主页 Interesting: &#x1f4da;NCF,基于Neural Contact Fields神经接触场的方法实现有效的外部接触估计和插入操作。 (from FAIR ) 操作插入处理结果&am…

ESP8266使用记录(四)

放上最终效果 ESP8266&Unity游戏 整合放进了坏玩具车遥控器里 最终只使用了mpu6050的yaw数据&#xff0c;因为roll值漂移…… 使用了https://github.com/ElectronicCats/mpu6050 整个流程 ESP8266取MPU6050数据&#xff0c;处理后通过udp发送给Unity显示出来 MPU6050_Z…

TensorFlow学习1:使用官方模型进行图片分类

前言 人工智能以后会越来越发达&#xff0c;趁着现在简单学习一下。机器学习框架有很多&#xff0c;这里觉得学习谷歌的 TensorFlow&#xff0c;谷歌的技术还是很有保证的&#xff0c;另外TensorFlow 的中文文档真的很友好。 文档&#xff1a; https://tensorflow.google.cn/…

ORACLE Redo Log Buffer 重做日志缓冲区机制的设计

最近和朋友包括一些国产数据库的研发人员交流&#xff0c;很多程序员认为 Oracle 已经过时&#xff0c;开源数据库或者他们研发的国产数据库才代表数据库发展的未来。甚至在很多交流会议上拿出自家产品的某一个功能点和 Oracle 对比就觉得已经遥遥领先。 实际上数据库系统的发展…

JVM机制理解与调优方案

作者&#xff1a;逍遥Sean 简介&#xff1a;一个主修Java的Web网站\游戏服务器后端开发者 主页&#xff1a;https://blog.csdn.net/Ureliable 觉得博主文章不错的话&#xff0c;可以三连支持一下~ 如有需要我的支持&#xff0c;请私信或评论留言&#xff01; 前言 很多Java开发…

电脑WIFI突然消失

文章目录 1. 现象2. 解决办法1&#xff1a;重新启用无线网卡设置3. 解决办法2&#xff1a;更新无线网卡驱动4. 解决办法3&#xff1a;释放静电5. 解决办法4&#xff1a;拆机并重新插拔无线网卡 1. 现象 如下图&#xff1a;电脑在使用过程中WIFI消失 设备管理器中的无线网卡驱…

【深度学习实验】卷积神经网络(六):自定义卷积神经网络模型(VGG)实现图片多分类任务

目录 一、实验介绍 二、实验环境 1. 配置虚拟环境 2. 库版本介绍 三、实验内容 0. 导入必要的工具包 1. 构建数据集&#xff08;CIFAR10Dataset&#xff09; a. read_csv_labels&#xff08;&#xff09; b. CIFAR10Dataset 2. 构建模型&#xff08;FeedForward&…

解决内网拉取企微会话存档代理问题的一种办法

问题&#xff1a;客户的服务都是内网的&#xff0c;不能直接访问外网&#xff1b;访问外网的话需要走kong网关才能出去。 会话存档官网说可以使用socket5、http方式拉取会话存档&#xff1b;我这边尝试了直接使用kong网关的ip和端口配置进去&#xff0c;是访问不了的 我后面就…

<C++> 哈希表模拟实现STL_unordered_set/map

哈希表模板参数的控制 首先需要明确的是&#xff0c;unordered_set是K模型的容器&#xff0c;而unordered_map是KV模型的容器。 要想只用一份哈希表代码同时封装出K模型和KV模型的容器&#xff0c;我们必定要对哈希表的模板参数进行控制。 为了与原哈希表的模板参数进行区分…

开源layui前端框架 收款码生成系统源码 多合一收款码生成源码 带50多套UI模板

Layui前端的多合一收款码在线生成系统源码_附多套前端UI模板。 卡特三合一收款码生成系统源码&#xff0c;和收款啦采用一样的原理。 内部多达50多套模板&#xff0c;前端跟付款界面都特别好看。 识别收款码之后会自动加密&#xff0c;非常安全。 一样没有后台&#xff0c;一样…

机器人过程自动化(RPA)入门 9. 管理和维护代码

仅仅创建一个自动化项目是不够的。无论是决定使用哪种布局,还是正确命名步骤,以正确的方式组织项目都很重要。项目也可以在新的项目中重用,这对用户来说非常方便。本章解释了我们可以重用项目的方法。我们还将学习配置技术并看到一个示例。最后,我们将学习如何集成TFS服务器…

XSS详解

XSS一些学习记录 XXS短标签、属性、事件、方法短标签属性事件函数弹窗函数一些对于绕过有用的函数一些函数使用payload收集 浏览器编码问题XML实体编码URL编码JS编码混合编码 一些绕过方法利用constructor原型污染链构造弹框空格绕过圆括号过滤绕过其他的一些绕过 参考 XXS短标…

中国1km土壤特征数据集(2010年)

简介&#xff1a; 中国1km土壤特征数据集&#xff08;2010&#xff09;是基于第二次全国土壤调查的中国1:1000000比例尺土壤图和8595个土壤剖面图&#xff0c;以及美国农业部&#xff08;USDA&#xff09;中国区域土地和气候模拟标准&#xff0c;开发了一个多层土壤粒度分布数…

Apache shiro RegExPatternMatcher 权限绕过漏洞 (CVE-2022-32532)

漏洞描述 2022年6月29日&#xff0c;Apache 官方披露 Apache Shiro &#xff08;CVE-2022-32532&#xff09;权限绕过漏洞。 当Apache Shiro中使用RegexRequestMatcher进行权限配置&#xff0c;且正则表达式中携带"."时&#xff0c;未经授权的远程攻击者可通过构造恶…

C#,数值计算——Ranlim32的计算方法与源程序

1 文本格式 using System; namespace Legalsoft.Truffer { /// <summary> /// High-quality random generator using only 32-bit arithmetic.Same conventions /// as Ran.Period is 3.11E37 . Recommended only when 64-bit arithmetic is not /// a…

01-http概述

HTTP概述 HTTP使用的是可靠地数据传输协议。HTTP属于应用层协议 Web客户端和服务器 web服务器&#xff1a;又称http服务器&#xff0c;用于存储web内容&#xff0c;并向web客户端提供web内容web客户端&#xff1a;用于请求web服务器的应用软件&#xff0c;常见为浏览器 资源…

【Linux】线程同步和互斥

目录 一、线程互斥1.相关概念2.互斥锁&#xff08;mutex&#xff09;3.互斥锁的原理4.自定义封装一个锁 二、可重入和线程安全三、死锁死锁概念死锁四个必要条件如何避免死锁 四、线程同步1.条件变量概念条件变量接口基于阻塞队列实现生产者消费者模型 2.信号量概念信号量操作接…

深入理解React中fiber

一、前言 Fiber是对React核心算法的重写&#xff0c;Fiber是React内部定义的一种数据结构&#xff0c;将更新渲染耗时长的大任务&#xff0c;分为许多的小片。Fiber节点保存啦组件需要更新的状态和副作用&#xff0c;一个Fiber代表一个工作单元。 二、Fiber在React做了什么 …

快速排序与冒泡排序以及代码

快速排序 快速排序&#xff08;Quicksort&#xff09;是一种常用的排序算法&#xff0c;它基于分治的思想。 时间复杂度&#xff1a;O&#xff08;nlogn&#xff09; 空间复杂度&#xff1a;O&#xff08;logn&#xff09; 快速排序的基本思想如下&#xff1a; 选择一个元素…

xxl-job 2.2之后版本高版本executor未授权访问漏洞

xxl-job 低版本executor未授权访问 低版本的executor未授权访问漏洞是 POST /run HTTP/1.1 Host: your-ip:9999 Accept-Encoding: gzip, deflate Accept: */* Accept-Language: en User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like G…