0

我有一个用户控件,它试图绑定父控件的文本框,

文本块控件.xaml

<UserControl x:Class="wpf_sandbox.TextBlockControl"
             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" >
    <Grid>
        <TextBlock Text="{Binding Text, ElementName=editor}"></TextBlock>
    </Grid>
</UserControl>

主窗口.xaml

<Window x:Class="wpf_sandbox.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:wpfSandbox="clr-   namespace:wpf_sandbox"
        Title="MainWindow" Height="350" Width="525">
    <StackPanel>
        <TextBox x:Name="editor"></TextBox>
        <wpfSandbox:TextBlockControl></wpfSandbox:TextBlockControl>       
    </StackPanel>
</Window>

这根本行不通。我尝试了几种方法,例如使用相对源和源,但也没有奏效。

4

1 回答 1

1

您可以将<TextBlockControl />的DataContext属性声明为“编辑器” <TextBox />Text属性:

<wpfSandbox:TextBlockControl DataContext="{Binding Text, ElementName=editor}" />

并在您的控制范围内:

<Grid>
    <TextBlock Text="{Binding}" />
</Grid>
于 2013-06-07T04:34:43.517 回答