F#とWPFを使用して簡単なタスクスケジューラを作成しようとしています。基本的には、すべてのタスクに「削除」ボタンがあるタスクの一覧です。リストの外側にあるボタンのクリックを処理することは問題ではありません。これは通常のコマンドで処理できます。しかし、ボタンを操作するのリスト項目は簡単ではありません。私はRelayCommandを使用して、hereを親にルーティングされたバインディングで説明しましたが、送信者オブジェクトは常にnullです(私はそれをコレクションからのタスクオブジェクトにすることを期待していました)。また、お勧めのhereとしてプロパティを接続しようとしましたが、動作させることができませんでした。F#WPF:ListBoxのクリックイベントを処理する
[削除]ボタンをクリックしてタスクオブジェクトを取得するイベントハンドラを割り当てるにはどうすればよいですか?ここで
が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は自明であるので、私はここでそれを示していませんよ。
魅力的な作品です!ありがとうございました! –