簡體   English   中英

WPF將內部控件與父數據上下文綁定

[英]WPF Binding inner control with parent data context

我做了一個用戶控件

<UserControl x:Class="MyApp.MyControl"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         mc:Ignorable="d"  x:Name="uc">
<Grid Width="Auto" Height="Auto">
    <TextBlock Text="{Binding Path=DataContext.TextContent, ElementName=uc}"/>
    <TextBlock Text="{Binding Path=DataContext.TextContent2, ElementName=uc}"/>
</Grid>

我希望定義的控件(uc)中的子控件將綁定到uc.DataContext的屬性。 我使用定義的控件如下:

<Window x:Class="Tms.TMSClient.Views.MainWindow" Name="window"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:control="clr-namespace:MyApp"
    xmlns:ribbon="clr-namespace:Microsoft.Windows.Controls.Ribbon;assembly=RibbonControlsLibrary">      

    <control:MyControl DataContext="{Binding Path=MyControlVM}"/>

分配給窗口的DataContext具有以下結構:WindowVM.MyControlVM.TextContent。

給定的代碼不起作用,因為文本框的DataContext被綁定到WindowVM。 我認為問題可能是因為內部文本框在定義的控件(uc)之前被綁定,因此uc的有界DataContext尚未生效。

我想要的是:自定義控件(MyControl)將綁定到其對應的viewmodel(MyControlVM),MyControl的內部元素將綁定到MyControlVM的屬性。

你對這個問題有什么解決方案嗎?

如果我理解正確,您希望數據將MyControl視圖模型中的屬性綁定到MyControl UserControl內的TextBox.Text屬性。 如果這是正確的,那么您可以使用RelativeSource Binding或您已在使用的ElementName語法。

首先,確保將視圖模型設置為UserControlDataContext

public MyControl()
{
    DataContext = new YourControlViewModel();
}

當子控件自動繼承其父級的DataContext對象時,您現在可以通過UserControl的XAML中的MyControl.DataContext屬性從TextBox引用此視圖模型:

<TextBlock Text="{Binding DataContext.TextContent, 
    RelativeSource={RelativeSource AncestorType={x:Type Window}}}" />

這就是你所需要的。

默認情況下,每個控件都從其父控件繼承其DataContext。 因此,不需要明確地綁定它。

實際上,當您想要將控件的DataContext綁定到嵌套屬性時,您必須指定:

<control:MyControl DataContext="{Binding Path=TextContent}"/>
<TextBlock Text="{Binding Path=TextContent}"/>

在我的測試應用程序中為我工作。

MainWindow.xaml

<Window x:Class="DataContextTest.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:my="clr-namespace:DataContextTest"
    Title="MainWindow" Height="350" Width="525">
<Window.DataContext>
    <my:MyOuterDataContext />
</Window.DataContext>
<Grid>
    <my:MyControl DataContext="{Binding Path=MyInnerDataContext}" />
</Grid>

MyControl.xaml

<UserControl x:Class="DataContextTest.MyControl"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         mc:Ignorable="d" 
         d:DesignHeight="300" d:DesignWidth="300">
<Grid>
    <TextBlock Text="{Binding Path=TextContent}" />
</Grid>

DataContexts:

public class MyOuterDataContext
{
    public MyInnerDataContext MyInnerDataContext { get; set; }

    public MyOuterDataContext()
    {
        MyInnerDataContext = new MyInnerDataContext();
    }
}

public class MyInnerDataContext
{
    public string TextContent { get { return "foo"; } }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM