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);
}
}