假设我有一个业务对象(没有AllowNullLiteralAttribute
)。
type Person(name: string) =
member val Name = name
override x.ToString() = name
还有一个视图模型,可选择设置所选人员。
type MainWindowModel() =
let mutable selectedPerson: Person option = None
:
member val People = ObservableCollection<Person>()
member x.SelectedPerson
with get() = selectedPerson
and set(v) =
if selectedPerson <> v then
selectedPerson <- v
x.RaisePropertyChanged("SelectedPerson")
将 WPF 控件的SelectedItem
属性绑定到 F# 选项属性(不使用AllowNullLiteralAttribute
)的最佳方法是什么?
如果我这样做...
<StackPanel>
<ListBox ItemsSource="{Binding People}"
SelectedItem="{Binding SelectedPerson}"
DisplayMemberPath="Name" />
<TextBlock Text="{Binding SelectedPerson}" />
</StackPanel>
...导致错误,无法将“George”从“Person”类型转换为“Microsoft.FSharp.Core.FSharpOption`1[Person]”类型