访问用户控件的元素

时间:2012-04-03 03:25:14

标签: 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.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>

现在,在我的主页面中,我想将用户控件中出现的文本从“Sample”更改为Hello World。所以,我在mainpage.xaml

中做了这个
<local:MyControl x:Name="MyControl" Margin="100,50 0,0"></local:MyControl>

当我尝试引用MyControl时,在mainpage.xaml.cpp中,它似乎无法识别:

MainPage::MainPage(){MyControl->Text = "Hello World";}

有什么想法吗?

2 个答案:

答案 0 :(得分:13)

详细说明@Steven您的回答在您的UserControl中定义DependencyProperty。 定义DependencyProperty允许更改通知以触发对控件的更新。

在UserControl的代码隐藏中,您可以添加依赖项属性。

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;
    }
}

这会公开您可以在UserControl的XAML中绑定的公共DependencyProperty

<UserControl>
        <TextBlock Text="{Binding Path=TextBlockText}" />
</UserControl>

现在您需要一种从Window控件设置该属性的方法。我将详细介绍三种方法:

1。)由于TextBlockText属性在UserControl上公开,我们可以直接在XAML中设置它,如:

<Window x:Class="WpfApplication2.MainWindow"
  xmlns:local="clr-namespace:WpfApplication2">
  <local:MyUserControl TextBlockText="Text that you want to set.">
  </local:MyUserControl>
</Window>

2.。)如果我们给UserControl一个名称,我们可以在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.";

3。)或者最后,您可以在DependencyProperty代码隐藏中创建另一个Window并将其绑定到UserControl的依赖项属性。这样,每当您更新属性Window代码中的值时,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.";
    }
}

并绑定到Window 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>

希望这有帮助!

答案 1 :(得分:1)

在程序视图中,执行此操作的最佳方法是使用数据绑定,而不是在后面的代码中设置值。 要解决此问题,最简单的方法是注册UserControl的依赖项属性将此值绑定到TextBlock,然后在MainPage中设置值。