Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/274.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在XAML/C中单击菜单项时显示包含填充项的列表框#_C#_Wpf_Xaml - Fatal编程技术网

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

C# 如何在XAML/C中单击菜单项时显示包含填充项的列表框#,c#,wpf,xaml,C#,Wpf,Xaml,我的XAML文件中有一个上下文菜单。单击此菜单项时,我希望向用户显示一个列表框,其中包含从后端调用填充的数据列表。我怎样才能做到这一点?我是XAML/WPF的新手。这将是您的XAML: <Window x:Class="MyWpfApp.MyWindow" xmlns:cmd="clr-namespace:MyWpfApp.MyCommandsNamespace" xmlns:vm="clr-namespace:MyWpfApp.MyViewModelsNamespace"

我的XAML文件中有一个上下文菜单。单击此菜单项时,我希望向用户显示一个列表框,其中包含从后端调用填充的数据列表。我怎样才能做到这一点?我是XAML/WPF的新手。

这将是您的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>
其中MyViewModel类似于:

    public class MyViewModel : DependencyObject
    {
        //MyList Observable Collection
        private ObservableCollection<MyItemClass> _myList = new ObservableCollection<MyItemClass>();
        public ObservableCollection<MyItemClass> MyList { get { return _myList; } }
    }
    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("---"));
    }
我忘了提到命令:

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

1.什么东西你都累了?2.此关联菜单附加到什么?3.您正在使用MVVM模式吗?3.您想在哪里显示列表框?在屏幕上还是在弹出窗口上?5.物品清单有多长?6.您希望在不同的线程上加载数据吗?&很多问题!!希望这会有所帮助。顺便说一下,我忘了提到命令声明。我已经把它添加到了答案中。
public static class MyCommandsClass
{
    public static RoutedCommand MyCommand1 = new RoutedCommand();
}