gpt4 book ai didi

wpf - 访问用户控件的元素

转载 作者:行者123 更新时间:2023-12-02 05:07:30 26 4
gpt4 key购买 nike

我创建了一个用户控件,如下所示:

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

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

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

有什么想法吗?

最佳答案

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

这会公开一个公共(public)DependencyProperty,您可以将其绑定(bind)到 UserControl 的 XAML 中。

<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 并将其绑定(bind)到 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.";
}
}

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

希望这有帮助!

关于wpf - 访问用户控件的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9986698/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com