Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/308.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# WPF应用程序中动态和静态菜单项上的绑定错误_C#_Wpf_Xaml - Fatal编程技术网

C# WPF应用程序中动态和静态菜单项上的绑定错误

C# WPF应用程序中动态和静态菜单项上的绑定错误,c#,wpf,xaml,C#,Wpf,Xaml,在我的WPF应用程序中,我从动态源(XML)创建了一个菜单。我将其与静态菜单项相结合,运行良好,但在静态菜单项上出现了一些错误。菜单是这样的 Entity - dynamic menu items - separator - static menu item System.Windows.Data错误:4:找不到与绑定的源 参考“相对资源查找器”, AncestorType='System.Windows.Controls.ItemsControl', AncestorLevel='1'。Bi

在我的WPF应用程序中,我从动态源(XML)创建了一个菜单。我将其与静态菜单项相结合,运行良好,但在静态菜单项上出现了一些错误。菜单是这样的

Entity
- dynamic menu items
- separator
- static menu item
System.Windows.Data错误:4:找不到与绑定的源 参考“相对资源查找器”, AncestorType='System.Windows.Controls.ItemsControl', AncestorLevel='1'。BindingExpression:Path=HorizontalContentAlignment; DataItem=null;目标元素是“MenuItem”(名称=“”);目标属性 是“HorizontalContentAlignment”(类型为“HorizontalAlignment”)

与垂直对齐相同,打开菜单后,也会出现此错误

System.Windows.Data错误:40:BindingExpression路径错误: 在“对象”“ModViewModel”上找不到“MenuItemName”属性 (HashCode=13278932)”。BindingExpression:Path=MenuItemName; DataItem='ModViewModel'(HashCode=13278932);目标元素是 “菜单项”(名称=”;目标属性为“Header”(类型为“Object”)

绑定的XAML


有没有办法将静态菜单项与动态菜单项分开,使静态菜单项不使用ItemContainerStyle?或者是什么导致了这些错误?如果你需要更多的代码,请告诉我

编辑:

公共可观测集合菜单项
{
获取{return\u menuItem;}
设置
{
_menuItem=值;
OnPropertyChanged();
}
}
公共类实体项
{
公共字符串MenuItemName{get;set;}
公共字典组件{get;set;}
}
这对我很有用

 <Window.Resources>
    <CollectionViewSource x:Key="FilterOptionsBridge" Source="{Binding Path=MyProperty}" />
 </Window.Resources>

ViewModelProperty:

public List<string> MyProperty { get; private set; }
public List MyProperty{get;private set;}
在ViewModel的构造函数中:

MyProperty = new List<string>();
        MyProperty.Add("Menu1");
MyProperty=newlist();
MyProperty.Add(“菜单1”);
菜单项:

  <MenuItem>
     <MenuItem.ItemsSource>
        <CompositeCollection>
           <CollectionContainer Collection="{Binding Source={StaticResource FilterOptionsBridge}}" />
            <Separator></Separator>
            <MenuItem Header="Hello"></MenuItem>
        </CompositeCollection>                            
      </MenuItem.ItemsSource>
    </MenuItem>

要使用命令,必须创建如下样式:

 <Style TargetType="MenuItem" >
     <Setter Property="Header" Value="{Binding Path=Title}"/>         
     <Setter Property="Command" Value="{Binding Path=Command}"/>
     <Setter Property="CommandParameter" Value="{Binding Path=CommandParameter}"/>
 </Style>

现在看来

 <MenuItem>
    <MenuItem.Style>
       <Style TargetType="MenuItem">                            
          <Setter Property="Command" Value="{Binding Path=Command}"/>
          <Setter Property="CommandParameter" Value="{Binding Path=CommandParameter}"/>
       </Style>
    </MenuItem.Style>
  <MenuItem.ItemsSource>
 <CompositeCollection>
     <CollectionContainer Collection="{Binding Path=Title, Source={StaticResource FilterOptionsBridge}}" />
        <Separator></Separator>
            <MenuItem Header="Hello"></MenuItem>
      </CompositeCollection>                            
     </MenuItem.ItemsSource>
    </MenuItem>

你的虚拟机集合像

 public List<Menuitems> MyProperty { get; private set; }
public List MyProperty{get;private set;}
Menuitems.cs像

 public class Menuitems //Impliment INotifyPropertyChanged Interface
    {
        private List<string> _Title = new List<string>();
        private ICommand _Command;
        private object _Commandparameter;            
        public List<string> Title
        {
            get { return _Title; }
            set { _Title = value; NotifyPropertyChanged(); }
        }
        public ICommand Command
        {
            get { return _Command; }
            set { _Command = value; NotifyPropertyChanged(); }
        }
        public object CommandParameter
        {
            get { return _Commandparameter; }
            set { _Commandparameter = value; NotifyPropertyChanged(); }
        }           
    }
公共类菜单项//实现INotifyPropertyChanged接口
{
私有列表_Title=新列表();
专用ICommand_命令;
私有对象_Commandparameter;
公开名单标题
{
获取{return\u Title;}
设置{u Title=value;NotifyPropertyChanged();}
}
公共ICommand命令
{
获取{return\u命令;}
设置{u Command=value;NotifyPropertyChanged();}
}
公共对象命令参数
{
获取{return\u Commandparameter;}
设置{u Commandparameter=value;NotifyPropertyChanged();}
}           
}
在我的VM构造函数中

 var menu1 = new Menuitems() { Title = new List<string>() {"Menu1","Menu2" }, Command=command, CommandParameter=commandparameter };            
        MyProperty = new List<Menuitems>() { menu1 };
var menu1=new Menuitems(){Title=new List(){“menu1”、“Menu2”},Command=Command,CommandParameter=CommandParameter};
MyProperty=new List(){menu1};
这为收集容器提供了一个很好的例子,这对我很有用

 <Window.Resources>
    <CollectionViewSource x:Key="FilterOptionsBridge" Source="{Binding Path=MyProperty}" />
 </Window.Resources>

ViewModelProperty:

public List<string> MyProperty { get; private set; }
public List MyProperty{get;private set;}
在ViewModel的构造函数中:

MyProperty = new List<string>();
        MyProperty.Add("Menu1");
MyProperty=newlist();
MyProperty.Add(“菜单1”);
菜单项:

  <MenuItem>
     <MenuItem.ItemsSource>
        <CompositeCollection>
           <CollectionContainer Collection="{Binding Source={StaticResource FilterOptionsBridge}}" />
            <Separator></Separator>
            <MenuItem Header="Hello"></MenuItem>
        </CompositeCollection>                            
      </MenuItem.ItemsSource>
    </MenuItem>

要使用命令,必须创建如下样式:

 <Style TargetType="MenuItem" >
     <Setter Property="Header" Value="{Binding Path=Title}"/>         
     <Setter Property="Command" Value="{Binding Path=Command}"/>
     <Setter Property="CommandParameter" Value="{Binding Path=CommandParameter}"/>
 </Style>

现在看来

 <MenuItem>
    <MenuItem.Style>
       <Style TargetType="MenuItem">                            
          <Setter Property="Command" Value="{Binding Path=Command}"/>
          <Setter Property="CommandParameter" Value="{Binding Path=CommandParameter}"/>
       </Style>
    </MenuItem.Style>
  <MenuItem.ItemsSource>
 <CompositeCollection>
     <CollectionContainer Collection="{Binding Path=Title, Source={StaticResource FilterOptionsBridge}}" />
        <Separator></Separator>
            <MenuItem Header="Hello"></MenuItem>
      </CompositeCollection>                            
     </MenuItem.ItemsSource>
    </MenuItem>

你的虚拟机集合像

 public List<Menuitems> MyProperty { get; private set; }
public List MyProperty{get;private set;}
Menuitems.cs像

 public class Menuitems //Impliment INotifyPropertyChanged Interface
    {
        private List<string> _Title = new List<string>();
        private ICommand _Command;
        private object _Commandparameter;            
        public List<string> Title
        {
            get { return _Title; }
            set { _Title = value; NotifyPropertyChanged(); }
        }
        public ICommand Command
        {
            get { return _Command; }
            set { _Command = value; NotifyPropertyChanged(); }
        }
        public object CommandParameter
        {
            get { return _Commandparameter; }
            set { _Commandparameter = value; NotifyPropertyChanged(); }
        }           
    }
公共类菜单项//实现INotifyPropertyChanged接口
{
私有列表_Title=新列表();
专用ICommand_命令;
私有对象_Commandparameter;
公开名单标题
{
获取{return\u Title;}
设置{u Title=value;NotifyPropertyChanged();}
}
公共ICommand命令
{
获取{return\u命令;}
设置{u Command=value;NotifyPropertyChanged();}
}
公共对象命令参数
{
获取{return\u Commandparameter;}
设置{u Commandparameter=value;NotifyPropertyChanged();}
}           
}
在我的VM构造函数中

 var menu1 = new Menuitems() { Title = new List<string>() {"Menu1","Menu2" }, Command=command, CommandParameter=commandparameter };            
        MyProperty = new List<Menuitems>() { menu1 };
var menu1=new Menuitems(){Title=new List(){“menu1”、“Menu2”},Command=Command,CommandParameter=CommandParameter};
MyProperty=new List(){menu1};

这为
CollectionContainer

检查提供了一个很好的例子对不起,我对你的答案有点迷茫,如果我遗漏了静态菜单项“编辑模板”,我不会得到任何错误。所以我的问题是,为什么静态MenuItem会导致这些错误,就像它试图将静态MenuItem的header属性设置为
MenuItemName
。你的意思是说,如果从xaml中删除
,它就可以正常工作?是的,那么我在调试器窗口中根本没有错误。这让我有点困惑。我的意思是,它也可以与静态菜单项一起工作,但是我在调试器窗口中得到这些错误。我也做了同样的事情,比如用try with out binding,检查我是否在答案中发布了代码。检查一下,检查一下,对不起,