Wpf原生MvvM
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | using System.ComponentModel; namespace WpfTestBlankApp.Models { public class Nickname : INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; private void Notify( string propName) { if (PropertyChanged!= null ) { PropertyChanged( this , new PropertyChangedEventArgs(propName)); } } string name; public string Name { get { return name; } set { name = value; Notify( "Name" ); } } string nick; public string Nick { get { return nick; } set { nick = value; Notify( "Nick" ); } } public Nickname(): this ( "name" , "nick" ) { } public Nickname( string name, string nick) { this .Nick = nick; this .Name = name; } } } |
属性双向绑定
1 using System.Collections.ObjectModel; 2 3 namespace WpfTestBlankApp.Models 4 { 5 public class Nicknames:ObservableCollection<Nickname> 6 { 7 } 8 }
集合数据绑定
1 using System.Windows; 2 using WpfTestBlankApp.Models; 3 4 namespace WpfTestBlankApp.Views 5 { 6 /// <summary> 7 /// Interaction logic for MainWindow.xaml 8 /// </summary> 9 public partial class MainWindow : Window 10 { 11 Nicknames names; 12 public MainWindow() 13 { 14 InitializeComponent(); 15 addButton.Click += AddButton_Click; 16 names= new Nicknames(); 17 //使数据可用于绑定 18 //dockPanel.DataContext= names; 19 //从资源中获取姓名集合 20 names = this.FindResource("names") as Nicknames; 21 } 22 23 private void AddButton_Click(object sender, RoutedEventArgs e) 24 { 25 names.Add(new Nickname()); 26 names.Add(new Nickname("张三","阿三")); 27 } 28 } 29 }
<Window x:Class="WpfTestBlankApp.Views.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:WpfTestBlankApp.Models" xmlns:prism="http://prismlibrary.com/" prism:ViewModelLocator.AutoWireViewModel="True" Title="{Binding Title}" Height="350" Width="525" > <Window.Resources> <local:Nicknames x:Key="names"> <local:Nickname Name="Don" Nick="Naked"/> <local:Nickname Name="Martin" Nick="Gudge"/> <local:Nickname Name="Tim" Nick="Stinky"/> </local:Nicknames> <Style x:Key="myStyle" TargetType="TextBlock"> <Setter Property="VerticalAlignment" Value="Center"/> <Setter Property="Margin" Value="2"/> <Setter Property="FontWeight" Value="Bold"/> <Setter Property="FontStyle" Value="Italic"/> </Style> </Window.Resources> <Grid> <DockPanel x:Name="dockPanel" DataContext="{StaticResource names}"> <TextBlock DockPanel.Dock="Top"> <TextBlock Style="{StaticResource myStyle}">Name:</TextBlock> <TextBox Text="{Binding Path=Name}"/> <TextBlock Style="{StaticResource myStyle}">Nick:</TextBlock> <TextBox Text="{Binding Path=Nick}"/> </TextBlock> <Button x:Name="addButton" DockPanel.Dock="Bottom" Content="Add" > <Button.Template> <ControlTemplate TargetType="{x:Type Button}"> <Grid> <Ellipse Width="128" Height="32" Fill="Yellow" Stroke="Black"/> <ContentPresenter VerticalAlignment="Center" HorizontalAlignment="Center"/> </Grid> </ControlTemplate> </Button.Template> </Button> <Button> <Button.LayoutTransform> <ScaleTransform ScaleX="3" ScaleY="3" /> </Button.LayoutTransform> <StackPanel Orientation="Horizontal"> <Canvas Width="20" Height="18" VerticalAlignment="Center"> <Ellipse Canvas.Left="1" Canvas.Top="1" Width="16" Height="16" Fill="Yellow" Stroke="Black" /> <Ellipse Canvas.Left="4.5" Canvas.Top="5" Width="2.5" Height="3" Fill="Black" /> <Ellipse Canvas.Left="11" Canvas.Top="5" Width="2.5" Height="3" Fill="Black" /> <Path Data="M 5,10 A 3,3 0 0 0 13,10" Stroke="Black" /> </Canvas> <TextBlock VerticalAlignment="Center">Click!</TextBlock> </StackPanel> </Button> <ListBox ItemsSource="{Binding}" IsSynchronizedWithCurrentItem="True"> <ListBox.ItemTemplate> <DataTemplate> <TextBlock> <TextBlock Text="{Binding Path=Name}"/>: <TextBlock Text="{Binding Path=Nick}"/> </TextBlock> </DataTemplate> </ListBox.ItemTemplate> </ListBox> </DockPanel> </Grid> </Window>
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)