【问题标题】:Accessing elements of a User Control访问用户控件的元素
【发布时间】:2012-04-03 03:25:14
【问题描述】:

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

现在,在我的主页上,我想将出现在我的用户控件中的文本从“示例”更改为 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";}

有什么想法吗?

【问题讨论】:

    标签: wpf xaml


    【解决方案1】:

    详细说明@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;
        }
    }
    

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

    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>
    

    希望这会有所帮助!

    【讨论】:

      【解决方案2】:

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

      【讨论】:

      猜你喜欢
      • 2016-01-17
      • 1970-01-01
      • 2011-02-06
      • 1970-01-01
      • 2020-05-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多