访问用户控件的元素

时间:2022-07-14 21:35:58

I have created a UserControl as follows:

我创建了一个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>

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

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

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

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

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

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

Any idea ?

任何想法 ?

2 个解决方案

#1


11  

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

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

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

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

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

这会公开一个公共DependencyProperty,您可以在UserControl的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:

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

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

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.) If we give the UserControl a name we can change the property within the Window code-behind:

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

3.)或者最后,您可以在Window的代码隐藏中创建另一个DependencyProperty,并将其绑定到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.";
    }
}

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

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

Hope this helps!

希望这可以帮助!

#2


1  

In program view, the best way to do this is using data binding rather than set the value in code behind. To solve this problem, the simplest way is to register a dependency property of the UserControl bind this value to the TextBlock and then set the value in MainPage.

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

#1


11  

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

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

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

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

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

这会公开一个公共DependencyProperty,您可以在UserControl的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:

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

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

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.) If we give the UserControl a name we can change the property within the Window code-behind:

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

3.)或者最后,您可以在Window的代码隐藏中创建另一个DependencyProperty,并将其绑定到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.";
    }
}

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

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

Hope this helps!

希望这可以帮助!

#2


1  

In program view, the best way to do this is using data binding rather than set the value in code behind. To solve this problem, the simplest way is to register a dependency property of the UserControl bind this value to the TextBlock and then set the value in MainPage.

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