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>
标签:原生,string,MvvM,nick,names,Wpf,Nickname,public,name From: https://www.cnblogs.com/friend/p/17026549.html