访问用户控件的元素 [英] Accessing elements of a User Control

查看:24
本文介绍了访问用户控件的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have created a UserControl as follows:

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

现在,从我的主页,我想将出现在我的用户控件中的文本从示例"更改为 Hello World.所以,我在 mainpage.xaml 中做了这个

Now, from my main page I would want to change the Text appearing in my User Control from "Sample" to Hello World. So, I did this in my mainpage.xaml

<local:MyControl x:Name="MyControl" Margin="100,50 0,0"></local:MyControl>

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

And in the mainpage.xaml.cpp when I try to reference MyControl, it seems unrecognized:

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

有什么想法吗?

推荐答案

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

Detailing out @Steven You's answer, in your UserControl define a DependencyProperty. Defining a DependencyProperty allows change notifications to trigger updates to your controls.

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

In the code-behind of your UserControl you can add the dependency property.

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 中.

This exposes a public DependencyProperty which you can bind to in your UserControl's XAML.

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

现在您需要一种从窗口控件设置该属性的方法.我将详细说明您可以采用的三种方法:

Now you need a way to set that property from your Window control. I'll detail three ways in which you can do this:

1.) 由于 TextBlockText 属性暴露在 UserControl 上,我们可以直接在 XAML 中设置它,例如:

1.) Since the TextBlockText property is exposed on the UserControl we can set it directly in the XAML like:

<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 代码隐藏中更改属性:

2.) If we give the UserControl a name we can change the property within the Window code-behind:

<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.) 或者最后,您可以在 Window 的代码隐藏中创建另一个 DependencyProperty 并将其绑定到 UserControl 的依赖属性.这样,每当您更新 Window 代码中的属性值时,UserControl 依赖项属性也会改变.正如@Steven You 之前所说,这是更好的选择,因为您背后的代码不需要了解任何控件.

3.) Or lastly you can create another DependencyProperty within your Window's code-behind and bind it to the UserControl's dependency property. This way whenever you update the property the value within your Window code the UserControl dependency property will change too. This is the preferable choice as @Steven You said before, since your code behind does not need to know about any controls.

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:

And to bind to our new DependencyProperty in the Window XAML:

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

希望这会有所帮助!

这篇关于访问用户控件的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆