如何更改ContentControl的内容

时间:2016-11-20 08:48:03

标签: wpf f#

我正在使用FsXaml并尝试更改ContentControl的内容以及单击该按钮时按钮的内容。到目前为止我所做的是 -

type MainView = XAML<"MainWindow.xaml">
type UCon1 = XAML<"UC1.xaml">
type UCon2 = XAML<"UC2.xaml">

type MainViewModel() as self = 
    inherit ViewModelBase()  

    let uc1 = new UCon1()
    let uc2 = new UCon2()

    let mutable buttonContent = "Beginning"
    let mutable content = uc1

    let canExecute(obj) = true
    let actionOnButtonClick(obj) = 
        match buttonContent with
        | "Beginning" -> self.ButtonContent <- "Ending"
                     //self.Content <- uc2 
        | _ -> self.ButtonContent <- "Beginning"
               //self.Content <- uc1

    member self.ButtonContent 
        with get () = buttonContent 
        and set value = buttonContent <- value
                        self.OnPropertyChanged "ButtonContent"

    member self.Content 
        with get () = content
        and set value = content <- value
                        self.OnPropertyChanged "Content"

    member x.ACommand = new RelayCommand(canExecute, actionOnButtonClick)

和XAML -

<ContentControl Content="{Binding Content}"/>
<Button Grid.Row="1" Command="{Binding ACommand}" Content="{Binding ButtonContent}"/>

如果我取消注释

self.Content <- uc2 

我在内容设置器中出现错误

This expression was expected to have type
    UCon1
but here has type
    UCon2

如何更改ContentControl的内容?

1 个答案:

答案 0 :(得分:2)

有效 -

let uc1 = new UCon1() :> obj
let uc2 = new UCon2() :> obj