在XAML中选择UserControl的子控件

发布于 2025-01-18 20:27:29 字数 1403 浏览 4 评论 0原文

这是一个通用的语法问题。

我有一个USERCONTROL

<UserControl x:Class="UserControlTest.Views.MyControl">
            
            <StackPanel Name="StackPanel1">
                <Button Name="Button1" Content="Hello From UserControl XAML"/>
                <Button Name="Button2" Content="Hello From UserControl XAML"/>
            </StackPanel>

</UserControl>

,并且窗口

<Window xmlns:views="using:UserControlTest.Views"
        x:Class="UserControlTest.Views.MainWindow">

    <views:MyControl Name="MyControl1"></views:MyControl>

</Window>

有办法从Mainwindow的XAML中选择子元素的属性,就像我在CS中可以做的那样?

因此,基本上我正在寻找这样的 XAML等效

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        this.Get<UserControl>("MyControl1").Get<Button>("Button1").Content = "Hello From Window CS";
    }
}

我希望的是与此类似的东西:

<Window xmlns:views="using:UserControlTest.Views"
        x:Class="UserControlTest.Views.MainWindow">

    <views:MyControl Name="MyControl1">
        <views:MyControl ??? Button1> Hello From Window XAML <views:MyControl ??? Button1>
    </views:MyControl>
</Window>

这仅是关于从XAML中的外部选择孩子。这不是要设置可以通过绑定和propper类定义实现的值。

This is a general Syntax Question.

I have a UserControl

<UserControl x:Class="UserControlTest.Views.MyControl">
            
            <StackPanel Name="StackPanel1">
                <Button Name="Button1" Content="Hello From UserControl XAML"/>
                <Button Name="Button2" Content="Hello From UserControl XAML"/>
            </StackPanel>

</UserControl>

And the Window

<Window xmlns:views="using:UserControlTest.Views"
        x:Class="UserControlTest.Views.MainWindow">

    <views:MyControl Name="MyControl1"></views:MyControl>

</Window>

Is there a way to select the properties of the child elements from XAML of the MainWindow like i can do in cs?

So basically im looking for an XAML equivalent of this:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        this.Get<UserControl>("MyControl1").Get<Button>("Button1").Content = "Hello From Window CS";
    }
}

What i hope for is something similar to this:

<Window xmlns:views="using:UserControlTest.Views"
        x:Class="UserControlTest.Views.MainWindow">

    <views:MyControl Name="MyControl1">
        <views:MyControl ??? Button1> Hello From Window XAML <views:MyControl ??? Button1>
    </views:MyControl>
</Window>

This is only about selecting the child from outside in XAML. This is not about setting a value which can be achieved with bindings and a propper class definition.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

∞觅青森が 2025-01-25 20:27:29

不确定这是否是您要寻找的内容,但是您可以使用样式并选择您感兴趣的控件:

<views:MyControl>
  <views:MyControl.Styles>
    <Style Selector="Button#Button1">
      <Setter Property="Content" Value="Hello From Window XAML"/>
    </Style>
  </views:MyControl.Styles>
</views:MyControl>

您可以阅读有关选择器此处

Not sure if this is what you are looking for, but you could use styles and select the control you are interested in:

<views:MyControl>
  <views:MyControl.Styles>
    <Style Selector="Button#Button1">
      <Setter Property="Content" Value="Hello From Window XAML"/>
    </Style>
  </views:MyControl.Styles>
</views:MyControl>

You can read more about selectors here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文