0
在Caliburn.Micro中,我想在子視圖(ViewModel)中綁定Parent屬性。Caliburn.Micro在子視圖中綁定父屬性。 (UWP)
首先是簡單的例子。
這是子視圖(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>
以上僅爲示例。我用簡單。並且我想綁定不使用「x:Name」。因爲「TextBlock」是最終綁定的目標。我想在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);
}
}
問題
如何 「Parent屬性」 綁定到子視圖?
我發現一些有用的鏈接..但我不能讓尚未...
請您分享一個可以重現您的問題的[MCVE]嗎? – Scavenger