RomanKovalev
RomanKovalev

Reputation: 859

How inherit DataContext?

please note that I using F# that specific with WPF. What should do to set DataContext of childs of the control at any depth? Particulary how set the data context to control with name "TargetControl". Problem context:

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>

My bootloader to start this cap:

[<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)

Upvotes: 0

Views: 1167

Answers (1)

flq
flq

Reputation: 22829

Not super-sure about the fsharp syntax, but this should make sense:

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

Upvotes: 1

Related Questions