小编web*_*316的帖子

如何动态绑定和静态添加MenuItems?

我将我的MenuItem的ItemsSource绑定到我的ViewModel中的ObservableCollection.这是我的xaml:

<MenuItem Header="_View"
          ItemsSource="{Binding Windows}">
  <MenuItem.ItemContainerStyle>
    <Style>
      <Setter Property="MenuItem.Header"
              Value="{Binding Title}" />
    </Style>
  </MenuItem.ItemContainerStyle>
</MenuItem>
Run Code Online (Sandbox Code Playgroud)

这部分很有用,但是现在我还想在同一个View MenuItem中添加一些静态MenuItem,用分隔符分隔.这样的东西,即使我知道这不起作用,因为我不能设置两次项目.

<MenuItem Header="_View"
          ItemsSource="{Binding Windows}">
  <MenuItem.ItemContainerStyle>
    <Style>
      <Setter Property="MenuItem.Header"
              Value="{Binding Title}" />
    </Style>
  </MenuItem.ItemContainerStyle>
  <Separator />
  <MenuItem Header="item 1" />
  <MenuItem Header="item 2" />
</MenuItem>
Run Code Online (Sandbox Code Playgroud)

现在我通过向MenuItem添加另一个级别来创建一个解决方法,如下所示:

<MenuItem Header="_View">
  <MenuItem Header="Windows"
            ItemsSource="{Binding Windows}">
    <MenuItem.ItemContainerStyle>
      <Style>
        <Setter Property="MenuItem.Header"
                Value="{Binding Title}" />
      </Style>
    </MenuItem.ItemContainerStyle>
  </MenuItem>
  <MenuItem Header="Load Layout" />
  <MenuItem Header="Save Layout" />
</MenuItem>
Run Code Online (Sandbox Code Playgroud)

这很好用,但如果可能的话,我宁愿没有子菜单.哦,我也更喜欢在xaml而不是代码背后执行此操作.有任何想法吗?

wpf xaml styles datatemplate menuitem

22
推荐指数
1
解决办法
2万
查看次数

使用WPF应用程序使用MEF运行多个不同的应用程序

我正在尝试创建一个WPF应用程序,它将成为其他应用程序的中心点.主应用程序应该能够根据用户需求动态加载其他应用程序.经过一些研究,MEF似乎可能是解决这个问题的方法.

我是MEF的新手所以我写了一个测试应用程序,试图让MEF工作.测试应用程序定义了一个非常基本的ITool接口.我能够毫无困难地从类库中导入多个类,但是我无法导入另一个WPF应用程序.这可能与MEF有关吗?

我的主要WPF应用程序创建了一个ToolContainer实例,用于创建和组合部件.

class ToolContainer
{
    [ImportMany(typeof(ITool))]
    IEnumerable<Lazy<ITool>> _tools;

    private CompositionContainer _container;

    public ToolContainer()
    {
        AggregateCatalog catalog = new AggregateCatalog();
        catalog.Catalogs.Add(new DirectoryCatalog(
            "C:\\Application Development\\Tool Center\\Tool Extensions"));

        _container = new CompositionContainer(catalog);

        try
        {
            _container.ComposeParts(this);
        }
        catch (CompositionException compositionException)
        {
            //TODO: show error dialog
        }
    }

}
Run Code Online (Sandbox Code Playgroud)

这是界面

public interface ITool
{
    String ToolName { get; }
    void OpenTool();
}
Run Code Online (Sandbox Code Playgroud)

我创建了多个继承自ITool的类,并且导出工作正常.这里有一个例子.

[Export(typeof(ITool))]
public class Class1 : ITool
{
    public String ToolName
    {
        get { return "....."; }
    }

    public void OpenTool()
    { …
Run Code Online (Sandbox Code Playgroud)

c# wpf mef

4
推荐指数
1
解决办法
1099
查看次数

标签 统计

wpf ×2

c# ×1

datatemplate ×1

mef ×1

menuitem ×1

styles ×1

xaml ×1