Mvvmlight 绑定 ItemSource
▲ 界面
XAML:
<Window x:Class="WpfApp1.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:WpfApp1" WindowStartupLocation="CenterScreen"
mc:Ignorable="d" x:Name="dataProvider"
Title="MainWindow" Height="200" Width="500">
<Window.DataContext>
<local:VMTest/>
</Window.DataContext>
<Grid>
<StackPanel>
<ListView x:Name="lstViewTest" ItemsSource="{Binding ListItems}">
<ListView.View>
<GridView>
<GridViewColumn Header="Name" DisplayMemberBinding="{Binding Name}" Width="200"></GridViewColumn>
<GridViewColumn Header="Mode" DisplayMemberBinding="{Binding Age}" Width="200"></GridViewColumn>
</GridView>
</ListView.View>
</ListView>
<Button Content="新增测试" x:Name="btnTest" Click="btnTest_Click" Margin="0 3 0 0"/>
<Button Content="删除测试" Name="btnremove" Click="btnremove_Click" Margin="0 3 0 0"/>
</StackPanel>
</Grid>
</Window>
Code Behind:
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
m_VMTest = DataContext as VMTest;
}
private VMTest m_VMTest;
private void btnTest_Click(object sender, RoutedEventArgs e)
{
m_VMTest.ListItems.Insert (0, new ItemTest() { Name = "华仔", Age = 70 });
}
private void btnremove_Click(object sender, RoutedEventArgs e)
{
m_VMTest.ListItems.RemoveAt(0);
}
}
public class ItemTest
{
public ItemTest()
{
Name = "小黑";
Age = 100;
}
public string Name { get; set; }
public int Age { get; set; }
}
VMTest:
public class VMTest : ObservableObject
{
public VMTest()
{
ListItems = new ObservableCollection<ItemTest>
{
new ItemTest() {Name = "111", Age = 1},
new ItemTest() {Name = "121",Age = 2},
new ItemTest() {Name = "1131",Age = 3},
};
}
public void VMConstructor()
{
}
private ObservableCollection<ItemTest> listItems;
public ObservableCollection<ItemTest> ListItems
{
get { return listItems; }
set { listItems = value; RaisePropertyChanged(nameof(listItems)); }
}
}
点击“新增”或“删除”按钮,能动态的影响绑定的数据结构。
路由事件测试
来自《WPF 深入浅出》书本里面的内容。
▲ 点击按钮报时
XAML:
<Window x:Class="Test_RouteEvent.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:Test_RouteEvent"
mc:Ignorable="d" local:TimeButton.ReportTime="ReportTimeHandler"
Title="MainWindow" Height="350" Width="400">
<Grid x:Name="grid_1" >
<Grid x:Name="grid_2" local:TimeButton.ReportTime="ReportTimeHandler">
<Grid x:Name="grid_3">
<StackPanel x:Name="stackPanel_1" Orientation="Vertical" local:TimeButton.ReportTime="ReportTimeHandler">
<ScrollViewer x:Name="scrollView_1" VerticalScrollBarVisibility="Auto" local:TimeButton.ReportTime="ReportTimeHandler">
<ListBox x:Name="listBox" BorderBrush="Black" BorderThickness="1" local:TimeButton.ReportTime="ReportTimeHandler" Margin="5" Height="240"/>
</ScrollViewer>
<local:TimeButton x:Name="timeButton" Width="80" Height="40" Content="报时" Margin="10" local:TimeButton.ReportTime="ReportTimeHandler"/>
</StackPanel>
</Grid>
</Grid>
</Grid>
</Window>
Code behind:
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void ReportTimeHandler(object sender, ReportTimeEventArgs e)
{
FrameworkElement element = sender as FrameworkElement;
string timeStr = e.ClickTime.ToLongTimeString();
string content = $"{timeStr} 到达 {element.Name}";
listBox.Items.Add(content);
}
}
// 用于承载事件消息的时间参数
class ReportTimeEventArgs : RoutedEventArgs
{
public ReportTimeEventArgs(RoutedEvent routedEvent, object source) :
base(routedEvent, source)
{
}
public DateTime ClickTime { get; set; }
}
class TimeButton : Button
{
// 声明和注册路由事件
public static readonly RoutedEvent ReportTimeEvent = EventManager.RegisterRoutedEvent
("ReportTime", RoutingStrategy.Bubble, typeof(EventHandler<ReportTimeEventArgs>), typeof(TimeButton));
//CLR 事件包装器
public event RoutedEventHandler ReportTime
{
add { AddHandler(ReportTimeEvent, value); }
remove { RemoveHandler(ReportTimeEvent, value); }
}
// 激发路由事件,借用 Click 事件的激发方法
protected override void OnClick()
{
base.OnClick(); // 保证 Button 原有的功能正常使用, CLick 事件能被激发
ReportTimeEventArgs args = new ReportTimeEventArgs(ReportTimeEvent, this);
args.ClickTime = DateTime.Now;
RaiseEvent(args);
}
}
标签:ItemTest,Name,public,杂乱,测试,new,VMTest,MainWindow
From: https://www.cnblogs.com/huvjie/p/18293912