请注意我使用的是特定于WPF的F#。如何在任何深度设置控件子级的DataContext?特别是如何设置数据上下文来控制名称“TargetControl”。问题背景:
的App.xaml:
<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
>
<Frame Name="Frame" Source="MainWindow.xaml" />
</Window>
MainWindow.xaml:
<UserControl xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Grid>
<TabControl Grid.Row="0" Grid.Column="0" Name="mainTab">
<!-- Tests work area -->
<TabItem Header="Проверка проекта">
<Frame Source="TestsPropagate.xaml" />
</TabItem>
</TabControl>
</Grid>
</UserControl>
的 TestsPropagate.xaml 的
<UserControl
Name="TargetControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<TextBox Text="{Binding Path=testField}" />
</UserControl>
我的引导程序启动此上限:
[<STAThread>]
[<EntryPoint>]
let main(_) = //(new Application()).Run(Application.Current) //mainWindowViewModel)
OFTD.DOM.ExtraEntities.Verification.EnitiesInitializer.InitializeReaders()
let app = new Application()
let view = Application.LoadComponent( new System.Uri("App.xaml", UriKind.Relative) ) :?> Window
let vm = new AppViewModel() // is data context to TargetControl
app.Run(view)
答案 0 :(得分:1)
对fsharp语法不太确定,但这应该有意义:
let vm = new AppViewModel() // is data context to TargetControl
view.DataContext <- vm
app.Run(view)