簡體   English   中英

從父控件綁定到子屬性

[英]Binding to child properties from parent control

我有一個用戶控件:

<UserControl x:Class="WpfApplication2.Control1"
         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" 
         xmlns:local="clr-namespace:WpfApplication2"
         mc:Ignorable="d" 
         d:DesignHeight="300" d:DesignWidth="300">
<Grid>
    <StackPanel Orientation="Horizontal" Name="StackPanelInsideUserControl">
        <Label />
        <TextBox Name="TextBoxInsideUserControl" Text="Initial Text" />
        <Button Content="OK" />
    </StackPanel>
</Grid>

我正在嘗試從其父項綁定到TextBoxInsideUserControl TextBox的Text屬性,例如:

<Window x:Class="WpfApplication2.MainWindow"
    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:local="clr-namespace:WpfApplication2"
    mc:Ignorable="d"
    Title="MainWindow" Height="350" Width="525">
<Grid>
    <local:Control1 StackPanelInsideUserControl.TextBoxInsideUserControl.Text="{Binding SomeText}" />
</Grid>

我該如何實現? xaml是否允許我們這樣做?

根據要求,將評論作為答案:

不,不是。 您應該在該UserControl上定義一個依賴項屬性 ,並將其綁定到TextBox的Text。 然后,您可以通過該依賴項屬性訪問UserControl外部的文本。

暫無
暫無

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

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