开发者

WPF from F# - reacting to event

I am having trouble with 开发者_运维问答the following example (slightly modified from the Expert F# book)

open System.Windows
open System.Windows.Controls
open System.Windows.Shapes
open System.Windows.Media
open System

let contentInit =
    let w = new Window(Topmost=true)
    let c = new Canvas()
    w.Content <- c

    let e = new Ellipse(Width=150., Height=150., Stroke=Brushes.Black)
    c.Children.Add(e) |> ignore

    e.MouseLeftButtonUp.Add(fun _ -> 
                                    e.Fill <- Brushes.Red
                                )
    w

let a = new Application()

[<EntryPoint>]
[<STAThread>]
let main (args: string []) =
    let myWin = contentInit
    do a.Run(myWin) |> ignore
    1

The behaviour that I expect is that the Ellipse is coloured red when (left) clicked, but nothing happens when the ellipse is clicked. What could be the problem here you think?


I think the problem is that your ellipse has no fill - try hitting the Black-parts or use

let e = new Ellipse(Width=150., Height=150., Stroke=Brushes.Black, Fill = Brushes.White)

if the fill is Null no Hittest will be executed - you have to give it some color (even transparent) to work this way.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜