【问题标题】:Caliburn.Micro binding parent property in Child View. ( UWP )Caliburn.Micro 在子视图中绑定父属性。 (UWP)
【发布时间】:2018-03-25 06:17:09
【问题描述】:

在 Caliburn.Micro 中,我想在 Child View(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}"/>

这是子视图模型。

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>

以上只是示例。我用的很简单。我想绑定没有“x:Name”。因为“TextBlock”是绑定的最终目标。我想在 ChildView 中绑定一个自定义控件。

这是我当前在子视图中的 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);
        }
    }

问题

如何将“父属性”绑定到子视图??

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

【问题讨论】:

标签: binding uwp caliburn.micro


【解决方案1】:

我已经在GitHub 上发布了答案,但要点是,如果您使用开箱即用的ScreenConductor 类,那么视图模型将具有Parent 属性您可以使用它来创建与视图模型父导体属性的绑定。

【讨论】:

    猜你喜欢
    • 2011-05-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-25
    • 1970-01-01
    • 2017-11-08
    • 2017-05-05
    • 1970-01-01
    相关资源
    最近更新 更多