Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/340.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# 制作一个列表框菜单_C#_Wpf_Xaml - Fatal编程技术网

C# 制作一个列表框菜单

C# 制作一个列表框菜单,c#,wpf,xaml,C#,Wpf,Xaml,我正在尝试制作一个与Microsoft Office中的菜单样式相同的简单菜单: (来源:) 我试过使用列表框作为菜单,但似乎无法将每个列表框项绑定到不同的面板。有没有一个简单的方法可以做到这一点?如果有人能给我一些建议或指导我学习一些教程,我将非常感激。我觉得我在网上到处都找遍了 我有几个问题。我在c#中使用了Silvermind的代码,并添加了一个事件: [ContentProperty("Content")] public class ModelForMyListBox : I

我正在尝试制作一个与Microsoft Office中的菜单样式相同的简单菜单:


(来源:)

我试过使用
列表框作为菜单,但似乎无法将每个列表框项绑定到不同的面板。有没有一个简单的方法可以做到这一点?如果有人能给我一些建议或指导我学习一些教程,我将非常感激。我觉得我在网上到处都找遍了


我有几个问题。我在c#中使用了Silvermind的代码,并添加了一个事件:

    [ContentProperty("Content")]
public class ModelForMyListBox : INotifyPropertyChanged
{
    private string title;
    private object content;
    public string Title
    {
        get { return title; }
        set
        {
            if (value == title)
                return;
            title = value;
            OnPropertyChanged("Title");
        }
    }
    public object Content
    {
        get { return content; }
        set
        {
            if (value == content)
                return;
            content = value;
            OnPropertyChanged("Content");
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    protected void OnPropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
            handler(this, new PropertyChangedEventArgs(propertyName));
    }
}
在XAML中,我在添加此项时出错

xmlns:controls="clr-namespace:WpfApplication1.Controls"
“WPFAApplication1.Controls”不包括在程序集中

至于XAML的其余部分,我在使用控件时遇到了一个错误,但我想这是由于上面的错误


希望你能帮我纠正最后一个问题?

最简单的方法是:

[ContentProperty("Content")]
public class ModelForMyListBox : INotifyPropertyChanged
{
    private string title;
    private object content;
    public string Title
    {
        get { return title; }
        set
        {
            if (value == title)
                return;
            title = value;
            OnPropertyChanged("Title");
        }
    }
    public object Content
    {
        get { return content; }
        set
        {
            if (value == content)
                return;
            content = value;
            OnPropertyChanged("Content");
        }
    }
}
在窗口或usercontrol中添加带有前缀的ModelForMyListBox命名空间,如控件 比如
xmlns:controls=“clr命名空间:Application1.controls”



我希望这能让你顺利完成。

非常感谢。等我回到家里的电脑再试试
<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="Auto"/>
        <ColumnDefinition Width="1*"/>
    </Grid.ColumnDefinitions>
    <ListBox x:Name="MenuListBox" DisplayMemberPath="Title">
        <controls:ModelForMyListBox Title="Share">
            <!-- <MyCustomViewObject here -->
        </controls:ModelForMyListBox>
        <controls:ModelForMyListBox Title="Help">
            <!-- <MyCustomViewObject here -->
        </controls:ModelForMyListBox>
    </ListBox>
    <ContentPresenter Grid.Column="1" 
                      DataContext="{Binding ElementName=MenuListBox, Path=SelectedItem}"
                      Content="{Binding Path=Content}"/>
<Grid>