简体   繁体   English

单击XAML / C#中的菜单项时如何显示带有填充项的列表框

[英]How to display a listbox with the populated items on click of a menu item in XAML/C#

I have a context menu in my XAML file. 我的XAML文件中有一个上下文菜单。 When I click on this menu item, I want to display a listbox to the user with a list of data populated from a backend call. 当我单击此菜单项时,我想向用户显示一个列表框,其中包含从后端调用中填充的数据列表。 How can I achieve this? 我该如何实现? I am a novice in XAML/WPF. 我是XAML / WPF的新手。

This would be your xaml: 这将是您的xaml:

<Window x:Class="MyWpfApp.MyWindow"
    xmlns:cmd="clr-namespace:MyWpfApp.MyCommandsNamespace"
    xmlns:vm="clr-namespace:MyWpfApp.MyViewModelsNamespace"
    ...>
    <Window.Resources>
        <DataTemplate x:Key="MyItemTemplate" DataType="{x:Type vm:MyItemClass}">
            <TextBlock Text="{Binding MyItemText}"/>
        </DataTemplate>
    </Window.Resources>
    <Window.CommandBindings>
         <CommandBinding Command="{x:Static cmd:MyCommandsClass.MyCommand1}" Executed="ExecuteMyCommand" CanExecute="CanExecuteMyCommand"/>
    </Window.CommandBindings>

    <Window.ContextMenu>
        <ContextMenu>
        <MenuItem Header="MyMenuItem1" 
              CommandTarget="{Binding}"
              Command="{x:Static cmd:MyCommandsClass.MyCommand1}"/>
        </ContextMenu>
    </Window.ContextMenu>
    <Grid>
        <ItemsControl ItemsSource="{Binding MyList}"
            ItemTemplate="{StaticResource MyItemTemplate}"/>
    </Grid>
</Window>

and this would be your cs code: 这将是您的CS代码:

    public MyWindow()
    {
        VM = new MyViewModelsNamespace.MyViewModel();
        this.DataContext = VM;
        InitializeComponent();
    }
    public void ExecuteMyCommand(object sender, ExecutedRoutedEventArgs e)
    {
        VM.MyList.Add(new MyItemClass{MyItemText="some text"});
    }
    public void CanExecuteMyCommand(object sender, CanExecuteRoutedEventArgs e)
    {
        if (...) e.CanExecute = false;
        else e.CanExecute = true;
    }

where MyViewModel is something like: MyViewModel是这样的:

    public class MyViewModel : DependencyObject
    {
        //MyList Observable Collection
        private ObservableCollection<MyItemClass> _myList = new ObservableCollection<MyItemClass>();
        public ObservableCollection<MyItemClass> MyList { get { return _myList; } }
    }

and MyItemClass is something like: 和MyItemClass是这样的:

    public class MyItemClass : DependencyObject
    {
        //MyItemText Dependency Property
        public string MyItemText
        {
            get { return (string)GetValue(MyItemTextProperty); }
            set { SetValue(MyItemTextProperty, value); }
        }
        public static readonly DependencyProperty MyItemTextProperty =
            DependencyProperty.Register("MyItemText", typeof(string), typeof(MyItemClass), new UIPropertyMetadata("---"));
    }

I forgot to mention command: 我忘了提到命令:

public static class MyCommandsClass
{
    public static RoutedCommand MyCommand1 = new RoutedCommand();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在C#中的列表框项中添加右键单击/上下文菜单 - Adding a right click / context menu to listbox items in C# C#Xaml如何获取列表框所选项目的文本框值 - C# Xaml How to get listbox selected item textbox value 列表框不显示项目C#XAML - Listbox does not show items c# xaml C# 列表框:在列表框中显示自定义项 - C# Listbox: Display custom items in listbox c#-如何从模型中的项目列表中获取特定项目,并在单击按钮时显示所选项目的属性? - c# - How to get a specific item from a list of items in model and display the properties of the selected item on button click? CodeGo.net&gt; XAML:如何通过单击文本框和按钮打开菜单? - c# - XAML: how to open an menu with textbox and button by button click? 如何在WPF C#的列视图中显示列表框项目 - How to display listbox items in column view in WPF c# 如何在C#中以表格形式显示列表框项目 - How to display Listbox items in tabular form in c# 如何使用C#在Windows Phone的列表框中显示项目字典? - How to display a dictionary of items in a Listbox on Windows Phone, using C#? 如何在C#中的按钮单击事件中在列表框中插入新项目,然后将焦点设置在列表框新项目上 - How to insert a new item in a listbox and then setfocus on the listbox new item on a button click event in C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM