Drag and Drop Button #319
Answered
by
JaggerJo
matthewcrews
asked this question in
Q&A
-
I have a simple app here: It contains a window with a button that I'm trying to enable click and drag for using the Elmish style. The logic is the following: module Counter =
open Avalonia.FuncUI.DSL
open Avalonia.Controls
open Avalonia.Layout
type State = {
IsSelected: bool
X : float
Y : float
}
let init() = {
IsSelected = false
X = 10.0
Y = 10.0
}
type Msg =
| Selected
| Deselected
| Move of x: float * y: float
let update (msg: Msg) (state: State) : State =
match msg with
| Selected ->
{ state with IsSelected = true }
| Deselected ->
{ state with IsSelected = false }
| Move (x, y) ->
if state.IsSelected then
{ state with X = x; Y = y }
else
state
let view (state: State) (dispatch) =
Button.create [
Button.top state.Y
Button.left state.X
Button.width 10.0
Button.height 10.0
Button.background "Blue"
Button.onPointerPressed (fun _ ->
Msg.Selected |> dispatch)
Button.onPointerReleased (fun _ ->
Msg.Deselected |> dispatch)
Button.onPointerMoved (fun e ->
let delta = e.GetPosition null
Msg.Move (delta.X, delta.Y) |> dispatch)
] I'm not getting any movement and the Select/Deselect logic doesn't appear to be working. I feel like I'm missing something simple and fundamental. |
Beta Was this translation helpful? Give feedback.
Answered by
JaggerJo
Jun 23, 2023
Replies: 1 comment 4 replies
-
Hey @matthewcrews 👋
let view (state: State) (dispatch) =
Canvas.create [
Canvas.children [
Button.create [
Button.top state.Y
Button.left state.X
Button.width 10.0
Button.height 10.0
Button.background "Blue"
Button.onPointerPressed (fun _ ->
Msg.Selected |> dispatch)
Button.onPointerReleased (fun _ ->
Msg.Deselected |> dispatch)
Button.onPointerMoved (fun e ->
let delta = e.GetPosition null
Msg.Move (delta.X, delta.Y) |> dispatch)
]
]
] |
Beta Was this translation helpful? Give feedback.
4 replies
Answer selected by
matthewcrews
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hey @matthewcrews 👋
Top
,Left
,Right
,Top
are attached properties of the canvas. You need to wrap the button control in a canvas for them to have any effect.