0

in Caliburn.Micro, I want to bind Parent property in Child View(ViewModel).

First is simple example.

This is Child View(XAML).

<UserControl
x:Class="Bg7Uwp1.Views.App.DerLayout.Der2View"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:controls="using:Microsoft.Toolkit.Uwp.UI.Controls" 
xmlns:micro="using:Caliburn.Micro"
xmlns:local="using:Bg7Uwp1.Controls"
mc:Ignorable="d"
x:Name="RootDer2">

<TextBlock Text="{Binding DataContext.hello, ElementName=RootDer2}"/>

This is Child ViewModel.

public class Der2ViewModel : Conductor<Screen>
{
    public Der2ViewModel()
    {
    }
}

This is parent View Model.

public string _hello = "HELLOWORLD!";
    public string hello
    {
        get { return _hello; }
        set
        {
            this.Set(ref _hello, value);
        }
    }

and this is parent View.

<UserControl
x:Class="Bg7Uwp1.Views.App.SpectrumView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:controls="using:Microsoft.Toolkit.Uwp.UI.Controls" 
xmlns:micro="using:Caliburn.Micro"
xmlns:local="using:Bg7Uwp1.Controls"
mc:Ignorable="d"
x:Name="Root"
d:DesignHeight="800"
d:DesignWidth="1000">

<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">

    <ContentControl x:Name="ActiveItem" 
                    Grid.Row="0"
                    HorizontalContentAlignment="Stretch" 
                    VerticalContentAlignment="Stretch"/>

</Grid>
</UserControl>

Above is just example. I used simple. and I want to bind without "x:Name". because "TextBlock" is final target to bind. I want to bind a custom control in ChildView.

This is my current CustomControl in Child View.

     <local:SensorMeter

        x:Name="DerRMeter"  
        HorizontalAlignment="Center" 
        DerThreashold1Svh="{Binding DerMeters[1].DerThreashold1Svh, ElementName=RootDer2}"
        DerThreashold2Svh="{Binding DerMeters[1].DerThreashold2Svh, ElementName=RootDer2}"
        DerSvh="{Binding DerMeters[1].DerSvh, ElementName=RootDer2}"
        Title="{Binding DataContext.DerMeters[1].Title, ElementName=RootDer2}"
        connectionDeviceStatusEnum="{Binding DerMeters[1].connectionDeviceStatusEnum, ElementName=RootDer2}"
        ticketThresholdType="{Binding DerMeters[1].ticketThresholdType, ElementName=RootDer2}"
        device="{Binding DerMeters[1].device, ElementName=RootDer2}"
        micro:Message.Attach="[Event DoubleTapped]=[Action Click_DoubleTapped(1)];[Event Tapped]=[Action Click_Tapped(1)]"    

        />

This is my current Parent View Model.

public ObservableCollection<DerMeter> DerMeters
    {
        get { return _derMeters; }
        set
        {
            this.Set(ref _derMeters, value);
        }
    }

Question

How to bind "Parent property" to Child View ??

I found some helpful links.. but I can not make yet...

Kazuhiko Nakayama
  • 801
  • 1
  • 8
  • 24

1 Answers1

0

I've posted an answer on GitHub, but the gist of it is that if you're using the out of the box Screen and Conductor classes then the view model will have a Parent property which you can use to create bindings to a view models parent conductor's properties.

Nigel Sampson
  • 10,549
  • 1
  • 28
  • 31