WPF 菜单项内容“名称”

发布于 2024-09-04 00:31:27 字数 453 浏览 6 评论 0原文

我有很多菜单项,我希望能够更改它们的“内容”,以便它显示在程序中。当我加载程序时,它们的“内容名称”在我创建的 Setter 中设置。但唯一的问题是我有近一百个 MenuItem 对象,并且我需要它们在程序中的显示名称不同(而不是设置器的默认值)。我可以创建超过 100 个不同的“Setter”并更改其中的一行..但这非常耗时。有没有更简单的方法?我希望能够在声明它们的 XAML 中执行此操作。有办法做到这一点吗?我一直在寻找并尝试不同的尝试,但到目前为止什么都没有......也许有人知道?

编辑:

抱歉,也许我有点不清楚..

我已经创建了MenuItems,它们基于我创建的Setter...问题是..我现在希望每个菜单项仍然基于该Setter ,但要为用户显示唯一的“内容”/名称...目前,它们都有设置器赋予的“内容”名称,但我正在寻找一种方法来通过设置每个 MenuItem 的内容名称XAML..这可能吗?

谢谢

I have a LOT of MenuItem(s), and I want to be able to change their "Content" so that it displays in the program. When I load up the program, their "Content Name" is set in a Setter I created.. but the only problem is that I have almost a hundred MenuItem objects, and I need their display names in the program to be different (not the setter's default). I could just create over 100 different "Setter"'s and change one line in them.. but that is very time consuming. Is there a simpler approach? I want to be able to do this in the XAML where I am declaring them. Is there a way to do this? I've been searching and trying different attempts, but nothing so far.. perhaps someone knows?

EDIT:

Sorry, Perhaps I am being a bit unclear..

I already have created the MenuItems and they are based on the Setter that I have created... The problem is.. I now want each one to still be based on that Setter, but to have a unique "Content"/Name that displays for the user...Currently, they all have the "Content" name given to them by the setter, but I am looking for a way to set each MenuItem's content name through XAML.. is this possible?

Thanks

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

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

发布评论

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

评论(2

夏花。依旧 2024-09-11 00:31:27

你的问题不清楚。我认为创建数百个菜单项的最佳方法是从代码而不是 XAML 中创建它们。例如在 foreach 循环中。然后你可以给他们每个人一个独特且有意义的名字。请更清楚地描述您的问题。

谢谢

You question is not clear. i think the best way to create hundreds of menu items is to create them from the code not in XAML. for example in a foreach loop. then you can give each of them a unique and meaningfull name. please describe your problem more clearly.

thanks

你对谁都笑 2024-09-11 00:31:27

现在我明白你的问题了。一般来说,我认为在 XAML 文件中为每个菜单项设置 content 属性是一个非常糟糕的主意。特别是当您处理数百件物品时。更好的方法是使用 WPF 和 DataTemplates 的数据绑定功能,而不是在 XAML 文件中硬编码 menuItem 名称。我将为您的问题提出两种解决方案。第一个解决方案使用代码隐藏方法来创建菜单项,然后将它们绑定到 MainMenu 的 ItemsSource 属性,而不使用 dataTemplates。下面的代码是窗口的代码隐藏代码:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();

        MenuItems = new ObservableCollection<MenuItem>();

        for (int i = 0; i < 40; i++)
        {
            MenuItem menuItem = new MenuItem();
            menuItem.Header = "MenuItem" + i.ToString();
            MenuItems.Add(menuItem);
        }

        MainMenu.DataContext = this;
    }

    public ObservableCollection<MenuItem> MenuItems
    {
        get;
        set;
    }
}

在这段代码中,我们首先创建了 40 个菜单项,然后将它们绑定到 MainMenu 对象的 DataContext 属性。以下代码显示了窗口的 XAML 代码,包括它的 MainMenu 对象:

<Window x:Class="WpfApplication17.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525" >
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="auto"/>
            <RowDefinition/>
        </Grid.RowDefinitions>
        <Menu Grid.Row="0" Name="MainMenu" ItemsSource="{Binding MenuItems}">

        </Menu>
    </Grid>
</Window> 

在此方法中,您可以首先在代码中创建所有菜单项及其名称,然后将它们绑定到 Menu 对象。然后您可以使用样式来设置菜单项的公共属性。

但更好的解决方案是使用 dataTemplates,就像我在下面的代码中所做的那样。在这种方法中,您首先创建一个类来存储菜单项名称。然后借助 WPF 的数据模板功能,您可以将它们绑定到您的 MainMenu 项。该解决方案的隐藏代码如下:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();

        MenuItems = new ObservableCollection<CustomMenuItem>();

        MenuItems.Add(new CustomMenuItem("Item 1"));
        MenuItems.Add(new CustomMenuItem("Item 2"));
        MenuItems.Add(new CustomMenuItem("Item 3"));

        MainMenu.DataContext = this;
    }

    public ObservableCollection<CustomMenuItem> MenuItems
    {
        get;
        set;
    }
}

public class CustomMenuItem
{
    public string Name { get; set; }

    public CustomMenuItem(string name)
    {
        Name = name;
    }
}

在此代码中,我使用 CustomMenuItem 类来存储菜单项名称。 MainWindows 构造函数负责创建菜单项,但您可以从其他来源检索它们,例如数据库的 XML 文件。 MainWindow 的 XAML 代码如下:

<Window x:Class="WpfApplication17.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:WpfApplication17"
        Title="MainWindow" Height="350" Width="525" >
    <Window.Resources>
        <DataTemplate DataType="{x:Type local:CustomMenuItem}">
            <TextBlock Text="{Binding Name}"></TextBlock>
        </DataTemplate>
    </Window.Resources>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="auto"/>
            <RowDefinition/>
        </Grid.RowDefinitions>
        <Menu Grid.Row="0" Name="MainMenu" ItemsSource="{Binding MenuItems}">

        </Menu>
    </Grid>
</Window>

通过这种方式,您可以从 XML 文件或其他数据源检索示例的菜单项名称,并且它们不会硬编码到您的 XAML 文件中。然后您可以使用 DataTemplates 的强大功能以您喜欢的方式查看菜单项。

Now I understand your problem. generaly i think it would be a very bad idea to set the content property for each of your menuItems in the XAML file. Specialy when you are dealing with hundreds of items. a better way is to use the Data binding feature of WPF and DataTemplates, not to hardcode the menuItem names in the XAML file. I will propuse two solutions for your problem. first solution uses the code-behind approach to create menu items and then bind them to MainMenu's ItemsSource property without using dataTemplates. the following code is the code-behind code for the window:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();

        MenuItems = new ObservableCollection<MenuItem>();

        for (int i = 0; i < 40; i++)
        {
            MenuItem menuItem = new MenuItem();
            menuItem.Header = "MenuItem" + i.ToString();
            MenuItems.Add(menuItem);
        }

        MainMenu.DataContext = this;
    }

    public ObservableCollection<MenuItem> MenuItems
    {
        get;
        set;
    }
}

in this code first we created 40 number of menuItems and then we bind them to the DataContext property of the MainMenu object. the following code shows the XAML code of the windows including it's MainMenu object:

<Window x:Class="WpfApplication17.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525" >
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="auto"/>
            <RowDefinition/>
        </Grid.RowDefinitions>
        <Menu Grid.Row="0" Name="MainMenu" ItemsSource="{Binding MenuItems}">

        </Menu>
    </Grid>
</Window> 

in this approch you can first create all of your menu items and their names in the code and after that bind them to the Menu object. then you can use styles to set common properties of the menu items.

but a better solution is to use dataTemplates as I did in the following code. in this approach first you created a class to store your menu item names. then with the help of the data template feature of WPF you can bind them to your MainMenu items. the code-behind of this solution is as follows:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();

        MenuItems = new ObservableCollection<CustomMenuItem>();

        MenuItems.Add(new CustomMenuItem("Item 1"));
        MenuItems.Add(new CustomMenuItem("Item 2"));
        MenuItems.Add(new CustomMenuItem("Item 3"));

        MainMenu.DataContext = this;
    }

    public ObservableCollection<CustomMenuItem> MenuItems
    {
        get;
        set;
    }
}

public class CustomMenuItem
{
    public string Name { get; set; }

    public CustomMenuItem(string name)
    {
        Name = name;
    }
}

in this code I used the CustomMenuItem class to store menuitem names. the MainWindows constructor is respossible for creating the menuitems but you can retrieve them from other sources, like a XML file of database. the XAML code for the MainWindow is like this:

<Window x:Class="WpfApplication17.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:WpfApplication17"
        Title="MainWindow" Height="350" Width="525" >
    <Window.Resources>
        <DataTemplate DataType="{x:Type local:CustomMenuItem}">
            <TextBlock Text="{Binding Name}"></TextBlock>
        </DataTemplate>
    </Window.Resources>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="auto"/>
            <RowDefinition/>
        </Grid.RowDefinitions>
        <Menu Grid.Row="0" Name="MainMenu" ItemsSource="{Binding MenuItems}">

        </Menu>
    </Grid>
</Window>

this way you can retrieve your menuitem names fot\r example from a XML file or from other data sources and they are not hardcoded into you XAML file. then you can use the powerfull features of DataTemplates to view you menu items the way you like.

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