Wpf 访问用户控件的元素

Wpf 访问用户控件的元素,wpf,xaml,Wpf,Xaml,我创建了一个UserControl,如下所示: <UserControl x:Class="MySample.MyControl" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:MySample" xmlns:d="http://schemas.microso

我创建了一个UserControl,如下所示:

<UserControl
x:Class="MySample.MyControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:MySample"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="300"
d:DesignWidth="400">

<Canvas>

    <Ellipse Width="150" Height="150"/>

    <TextBlock>Sample</TextBlock>

</Canvas>

有什么想法吗?

在程序视图中,最好的方法是使用数据绑定,而不是在代码中设置值。
要解决此问题,最简单的方法是注册UserControl的dependency属性并将此值绑定到TextBlock,然后在MainPage中设置该值。

详细说明@Steven You的答案,在UserControl中定义一个
DependencyProperty
。 定义
dependencProperty
允许更改通知触发控件更新

在UserControl的代码隐藏中,可以添加dependency属性

public partial class MyUserControl : UserControl
{
    public string TextBlockText
    {
        get { return (string)GetValue(TextBlockTextProperty); }
        set { SetValue(TextBlockTextProperty, value); }
    }

    public static readonly DependencyProperty TextBlockTextProperty =
        DependencyProperty.Register("TextBlockText", typeof(string), typeof(MyUserControl), new UIPropertyMetadata(""));


    public MyUserControl()
    {
        InitializeComponent();
        DataContext = this;
    }
}
这将公开一个公共的
DependencyProperty
,您可以在UserControl的XAML中绑定到该属性

<UserControl>
        <TextBlock Text="{Binding Path=TextBlockText}" />
</UserControl>
2.)如果我们给UserControl一个名称,我们可以更改窗口代码中的属性:

<Window x:Class="WpfApplication2.MainWindow"
  xmlns:local="clr-namespace:WpfApplication2">
  <local:MyUserControl Name="CoolUserControl">
  </local:MyUserControl>
</Window>
3.)或者最后,您可以在
窗口中创建另一个
DependencyProperty
,并将其绑定到
用户控件的dependency属性。这样,每当您更新属性时,
窗口中的值
code
UserControl
依赖项属性也会改变。正如@Steven您之前所说,这是一个更好的选择,因为您的代码隐藏不需要了解任何控件

public partial class MainWindow : Window
{

    public string UserControlText
    {
        get { return (string)GetValue(UserControlTextProperty); }
        set { SetValue(UserControlTextProperty, value); }
    }

    public static readonly DependencyProperty UserControlTextProperty =
        DependencyProperty.Register("UserControlText", typeof(string), typeof(MainWindow), new UIPropertyMetadata(""));


    public MainWindow()
    {
        InitializeComponent();
        DataContext = this;
        UserControlText = "Text that you want to set.";
    }
}
要绑定到窗口XAML中新的
DependencyProperty

<Window x:Class="WpfApplication2.MainWindow"
        xmlns:local="clr-namespace:WpfApplication2">
    <local:MyUserControl TextBlockText="{Binding RelativeSource={RelativeSource AncestorType={x:Type Window}, Mode=FindAncestor}, Path=UserControlText}"></local:MyUserControl>
</Window>


希望这有帮助

你能举一个小例子说明怎么做吗?读这个:看看这个数据绑定
<Window x:Class="WpfApplication2.MainWindow"
  xmlns:local="clr-namespace:WpfApplication2">
  <local:MyUserControl Name="CoolUserControl">
  </local:MyUserControl>
</Window>
CoolUserControl.TextBlockText = "Text that you want to set.";
public partial class MainWindow : Window
{

    public string UserControlText
    {
        get { return (string)GetValue(UserControlTextProperty); }
        set { SetValue(UserControlTextProperty, value); }
    }

    public static readonly DependencyProperty UserControlTextProperty =
        DependencyProperty.Register("UserControlText", typeof(string), typeof(MainWindow), new UIPropertyMetadata(""));


    public MainWindow()
    {
        InitializeComponent();
        DataContext = this;
        UserControlText = "Text that you want to set.";
    }
}
<Window x:Class="WpfApplication2.MainWindow"
        xmlns:local="clr-namespace:WpfApplication2">
    <local:MyUserControl TextBlockText="{Binding RelativeSource={RelativeSource AncestorType={x:Type Window}, Mode=FindAncestor}, Path=UserControlText}"></local:MyUserControl>
</Window>