Skip to content

Commit

Permalink
added an example usage of PassEventArgsToCommand to the EventBindings…
Browse files Browse the repository at this point in the history
…AndBehaviors sample elmish#33
  • Loading branch information
Tyson Williams committed Oct 29, 2019
1 parent 59ee570 commit 05c3415
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
16 changes: 15 additions & 1 deletion src/Samples/EventBindingsAndBehaviors.Views/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:i="http://schemas.microsoft.com/xaml/behaviors"
xmlns:local="clr-namespace:Elmish.WPF.Samples.EventBindingsAndBehaviors"
Title="EventBindings" Height="240" Width="400"
Title="EventBindings" Height="400" Width="400"
WindowStartupLocation="CenterScreen">
<StackPanel Margin="15" HorizontalAlignment="Center">
<TextBlock Text="Switch focus between these two inputs." Margin="0,0,0,10" TextAlignment="Center" />
Expand Down Expand Up @@ -47,5 +47,19 @@
<local:FocusWhenVisibleBehavior />
</i:Interaction.Behaviors>
</TextBox>
<Separator Margin="0,16,0,16"/>
<StackPanel Orientation="Horizontal" Margin="0,10">
<TextBlock Text="Postion of mouse is " />
<TextBlock Text="{Binding MousePositionX}" />
<TextBlock Text="x" />
<TextBlock Text="{Binding MousePositionY}" />
</StackPanel>
<Ellipse Fill="LightGray" Stroke="Black" Height="100" Width="300">
<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseMove">
<i:InvokeCommandAction PassEventArgsToCommand="True" Command="{Binding MouseMoveCommand}" />
</i:EventTrigger>
</i:Interaction.Triggers>
</Ellipse>
</StackPanel>
</Window>
20 changes: 18 additions & 2 deletions src/Samples/EventBindingsAndBehaviors/Program.fs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,17 @@ open System
open Elmish
open Elmish.WPF
open System.Windows
open System.Windows.Input


type Position = { X: int; Y: int }

type Model =
{ Msg1: string
Msg2: string
ButtonText: string
Visibility: Visibility }
Visibility: Visibility
MousePosition: Position }

let visibleButtonText = "Hide text box"
let hiddenButonText = "Show text box"
Expand All @@ -19,14 +23,16 @@ let init () =
{ Msg1 = ""
Msg2 = ""
ButtonText = visibleButtonText
Visibility = Visibility.Visible }
Visibility = Visibility.Visible
MousePosition = { X = 0; Y = 0 } }

type Msg =
| GotFocus1
| GotFocus2
| LostFocus1
| LostFocus2
| ToggleVisibility
| NewMousePosition of Position

let update msg m =
match msg with
Expand All @@ -38,7 +44,14 @@ let update msg m =
if m.Visibility = Visibility.Visible
then { m with Visibility = Visibility.Hidden; ButtonText = hiddenButonText }
else { m with Visibility = Visibility.Visible; ButtonText = visibleButtonText }
| NewMousePosition p -> { m with MousePosition = p }


let paramToNewMousePositionMsg (p: obj) =
let args = p :?> MouseEventArgs
let e = args.OriginalSource :?> UIElement;
let point = args.GetPosition e
NewMousePosition { X = int point.X; Y = int point.Y }

let bindings () : Binding<Model, Msg> list = [
"Msg1" |> Binding.oneWay (fun m -> m.Msg1)
Expand All @@ -50,6 +63,9 @@ let bindings () : Binding<Model, Msg> list = [
"ToggleVisibility" |> Binding.cmd ToggleVisibility
"ButtonText" |> Binding.oneWay (fun m -> m.ButtonText)
"TextBoxVisibility" |> Binding.oneWay (fun m -> m.Visibility)
"MouseMoveCommand" |> Binding.cmdParam paramToNewMousePositionMsg
"MousePositionX" |> Binding.oneWay (fun m -> m.MousePosition.X)
"MousePositionY" |> Binding.oneWay (fun m -> m.MousePosition.Y)
]


Expand Down

0 comments on commit 05c3415

Please sign in to comment.