2012-03-22 84 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