子视图中的Caliburn.Micro绑定父属性。 (UWP)

时间:2017-10-13 07:09:52

标签: binding uwp caliburn.micro

在Caliburn.Micro中,我想在子视图(ViewModel)中绑定Parent属性。

首先是一个简单的例子。

这是子视图(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}"/>

这是Child ViewModel。

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

这是父视图模型。

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

这是父视图。

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

以上就是例子。我用的很简单。我想绑定没有&#34; x:Name&#34;。因为&#34; TextBlock&#34;是绑定的最终目标。我想在ChildView中绑定自定义控件。

这是我在Child View中的当前CustomControl。

     <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)]"    

        />

这是我目前的父视图模型。

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

问题

如何绑定&#34;父母财产&#34;到儿童观看??

我找到了一些有用的链接..但我还不能做...

1 个答案:

答案 0 :(得分:0)

我已在GitHub上发布了答案,但其中的要点是,如果您正在使用开箱即用的ScreenConductor课程,那么视图模型将具有Parent属性,您可以使用该属性创建绑定到视图模型父导体的属性。

相关问题