以编程方式设置 WPF 元素的名称属性

发布于 2024-10-29 02:27:17 字数 1848 浏览 1 评论 0原文

我正在使用 WiPFlash 为我们办公室开发的 WPF 应用程序编写测试。为了做到这一点,我必须向几个到目前为止还不需要它们的 WPF 组件添加名称属性。

但在尝试为运行时创建的一组对象设置唯一名称时,我遇到了障碍。 XAML 看起来像:

<UserControl x:Class="Atlas.Activities.RibbonActivity.RibbonActivityView" 
                     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
                     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
                     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
                     xmlns:ribbon="clr-namespace:Microsoft.Windows.Controls.Ribbon;assembly=RibbonControlsLibrary" 
                     xmlns:RoutedMessaging="clr-namespace:Caliburn.PresentationFramework.RoutedMessaging;assembly=Caliburn.PresentationFramework" mc:Ignorable="d" d:DesignHeight="300" d:DesignWidth="300">

<ribbon:RibbonButton Style="{StaticResource RibbonButtonView}" Name="RibbonActivity">
    <ribbon:RibbonButton.ContextMenu>
        <ribbon:RibbonContextMenu>
            <ribbon:RibbonMenuItem x:Name="Modify" Header="Modify..." RoutedMessaging:Message.Attach="[Event Click] = [Action Modify]" />
            <ribbon:RibbonMenuItem x:Name="Hide" Header="Hide" RoutedMessaging:Message.Attach="[Event Click] = [Action Hide]" />
            <ribbon:RibbonMenuItem x:Name="Delete" Header="Delete" RoutedMessaging:Message.Attach="[Event Click] = [Action Delete]" />
        </ribbon:RibbonContextMenu>
    </ribbon:RibbonButton.ContextMenu>
</ribbon:RibbonButton>

来设置主题

WithSubject(activity);

构造函数使用:继承自 Caliburn.PresentationFramework.Screens

。所以我想要做的是将每个控件的名称设置为主题(活动)内的名称字段。

任何人都可以帮助我,或者至少让我知道这是否可能,

提前致谢,

克利。

I'm writing tests using WiPFlash for a WPF application that has been developed in our office. In order to do this I have had to add name attributes to several of WPF components that up until now have not needed them.

I have hit a block though in trying to set unique names to a set of objects that are created at runtime. The XAML looks like:

<UserControl x:Class="Atlas.Activities.RibbonActivity.RibbonActivityView" 
                     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
                     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
                     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
                     xmlns:ribbon="clr-namespace:Microsoft.Windows.Controls.Ribbon;assembly=RibbonControlsLibrary" 
                     xmlns:RoutedMessaging="clr-namespace:Caliburn.PresentationFramework.RoutedMessaging;assembly=Caliburn.PresentationFramework" mc:Ignorable="d" d:DesignHeight="300" d:DesignWidth="300">

<ribbon:RibbonButton Style="{StaticResource RibbonButtonView}" Name="RibbonActivity">
    <ribbon:RibbonButton.ContextMenu>
        <ribbon:RibbonContextMenu>
            <ribbon:RibbonMenuItem x:Name="Modify" Header="Modify..." RoutedMessaging:Message.Attach="[Event Click] = [Action Modify]" />
            <ribbon:RibbonMenuItem x:Name="Hide" Header="Hide" RoutedMessaging:Message.Attach="[Event Click] = [Action Hide]" />
            <ribbon:RibbonMenuItem x:Name="Delete" Header="Delete" RoutedMessaging:Message.Attach="[Event Click] = [Action Delete]" />
        </ribbon:RibbonContextMenu>
    </ribbon:RibbonButton.ContextMenu>
</ribbon:RibbonButton>

The constructor set the subject using:

WithSubject(activity);

which is inhereted from Caliburn.PresentationFramework.Screens.

So what I want to do is set the name of each control to be the name field within the subject (activity).

Can anyone help me out, or at least let me know if this is possible,

Thanks in advance,

Klee.

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

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

发布评论

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

评论(1

软糯酥胸 2024-11-05 02:27:17

根据您想要实现的目标的示例

Style="{StaticResource RibbonButtonView}" Name="{绑定主题.name}">

您无法在 XAML 中执行此操作。 XAML 已编译,并且在编译过程中使用 name 元素,以便您可以在代码隐藏中访问 XAML 元素。当 XAML 编译器将 XML 元素转换为 C# 代码时,它使用 Name 属性作为分部类中变量的名称。它不是一个可以在运行时由 WPF 绑定系统设置的属性。 看看这个很棒的问题 有关此行为的更多详细信息。

如果您想动态指定控件名称,则需要在代码隐藏中创建它们,但我不知道这是否会实现您想要做的事情,因为我不知道您正在使用什么命名属性为.

As per your example of what your trying to achieve

Style="{StaticResource RibbonButtonView}" Name="{Binding Subject.name}">

You cannot do this in XAML. XAML is compiled and the name element is used in the compilation process so that you can access the XAML element in the code-behind. When the XAML compiler converts the XML element into C# code, it uses the Name property as the name of the variable in a partial class. It isn't a property that can be set at runtime by the WPF binding system. Check out this great SO question for more details about this behaviour.

If you want to dynamically give controls names, you will need to create them in the code-behind, but I don't know if this will achieve what you're trying to do since I don't know what you're using the Name property for.

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