2012-03-22 83 views
0

请注意,我使用特定于WPF的F#。在任何深度设置DataContext的子控件应该怎么做?特别是如何设置数据上下文以控制名称“TargetControl”。问题背景:如何继承DataContext?

的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

哇,WPF和F#,你是勇敢的:) – GONeale 2012-06-26 07:10:04

+0

现在我用F#只在程序的内核并实现视图模型。然后,我在C#代码中展开视图模型(用于后面的代码),并在C#中使用WPF与F#的视图模型:) – psct 2012-07-31 11:37:17

回答

1

不超肯定的fsharp语法,但是这应该是有意义:

let vm = new AppViewModel() // is data context to TargetControl  
view.DataContext <- vm 
app.Run(view) 
+0

不,DataContext与UserControl的名称为“TargetControl”的DataContext未从视图继承。请注意,在框架控件 – psct 2012-03-22 13:20:07

+0

上使用了Source字段,我没有在代码中看到任何将您的DataContext与任何Visual关联的地方,因此我的回答为 – flq 2012-03-22 14:13:03

+0

ok,我的代码具有此行为。应该怎样在任何深度设置'view'的childs的DataContext? – psct 2012-03-22 15:26:50