2016-11-20 51 views
2

我使用FsXaml,并试图改变一个ContentControl中的内容,以及一个按钮,该按钮的点击的内容的内容。什么是截至目前为止我已经做了 -如何更改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中?

+0

你不能有两个“不同”类型的成员,所以你需要明确地将它们转换成一些常见的类型 – Sehnsucht

+0

@Sehnsucht你是对的。谢谢! –

+0

为什么不使用'FsVM'? –

回答

2

它的工作原理 -

let uc1 = new UCon1() :> obj 
let uc2 = new UCon2() :> obj 
相关问题