4
我正嘗試使用F#和WPF創建一個簡單的任務調度器。它基本上只是一個任務列表,其中每個任務都有一個「刪除」按鈕。處理列表外的按鈕點擊不是問題 - 可以使用常規命令處理。然而,處理一個按鈕點擊在列表項不直截了當。我嘗試使用RelayCommand描述here綁定路由到父,但發件人對象始終爲空(我期望它是集合中的任務對象)。還嘗試附加建議here的財產,但無法使其工作。F#WPF:處理列表框中的點擊事件
如何分配一個事件處理程序,通過點擊刪除按鈕來獲取任務對象?
這裏是App.fs:
namespace ToDoApp
open System
open System.Windows
open System.Collections.ObjectModel
open System.Windows.Input
open FSharp.ViewModule
open FSharp.ViewModule.Validation
open FsXaml
type App = XAML<"App.xaml">
type MainView = XAML<"MainWindow.xaml">
type Task(str) =
member x.Description with get() = str
type MainViewModel() as self =
inherit ViewModelBase()
let tasks = new ObservableCollection<Task>()
let addTaskCommand() =
let descr = sprintf "Do something at %A" (DateTime.Now.AddMinutes(30.0))
tasks.Add <| new Task(descr)
member this.Tasks with get() = tasks
member this.AddTask = this.Factory.CommandSync addTaskCommand
module main =
[<STAThread>]
[<EntryPoint>]
let main argv =
App().Run()
MainWindow.xaml:
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:ToDoApp;assembly=ToDoApp"
xmlns:fsxaml="http://github.com/fsprojects/FsXaml"
Title="Simple ToDo app" Height="200" Width="400">
<Window.DataContext>
<local:MainViewModel/>
</Window.DataContext>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Button Name="newJobButton" Command="{Binding AddTask}" Width="100" Height="32" Margin="5, 5, 5, 5" HorizontalAlignment="Left">New task</Button>
<ScrollViewer Grid.Row="1" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto">
<ListBox Name="lstBox" ItemsSource="{Binding Tasks}" >
<ListBox.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="80" />
</Grid.ColumnDefinitions>
<Label Grid.Column="0" Content="{Binding Description}" Margin="5 5 0 0"/>
<!-- OnClick ??? -->
<Button Grid.Column="1">Delete</Button>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</ScrollViewer>
</Grid>
</Window>
App.xaml中是微不足道的,所以我不會在這裏顯示它。
工程就像一個魅力!謝謝! –