为什么CommandParameter没有改变?

时间:2012-03-23 16:13:31

标签: c# wpf commandparameter

在我的View BarButtonItem绑定到Command和Commandparmeter以及ItemLinkSource绑定到MenuItems。 datagrid的焦点行绑定到SelectedItem。现在,我想在ViewModel中将SelectedItem传递给CommandParmeter。甚至我在menuitems中的每个命令绑定不同的commandParamer。

  <UserControl.Resources>
    <DataTemplate x:Key="BarItemTemplate">
        <ContentControl>
            <dxb:BarButtonItem Content="{Binding Caption}"                                  
                               CommandParameter="{Binding CommandParameter}"
                               Command="{Binding Command}"/>
        </ContentControl>
    </DataTemplate>
</UserControl.Resources>
<dxb:BarManager>
    <dxb:BarManager.Bars >
        <dxb:Bar x:Name="mainMenuBar" IsMainMenu="True" ItemLinksSource="{Binding MenuItems}" ItemTemplate="{StaticResource BarItemTemplate}"/>
    </dxb:BarManager.Bars>
    <Grid >
        <dxg:GridControl          
            ItemsSource="{Binding Model}">
            <dxg:GridControl.Columns>
                <dxg:GridColumn x:Name="colID" FieldName="Id" />
                <dxg:GridColumn  FieldName="UserName"/>      
            </dxg:GridControl.Columns>
            <dxg:GridControl.View> 
                <dxg:TableView FocusedRow="{Binding Path=SelectedItem,Mode=TwoWay}" AllowEditing="False" NavigationStyle="Row" ShowGroupPanel="False"/>                      
            </dxg:GridControl.View>
        </dxg:GridControl>

    </Grid>
</dxb:BarManager>

现在,我实现了包含MenuItems的ViewModel.Import MenuService,并在MenuItems中添加了一个新命令,并将SelectedItem作为parementer传递

public class ListViewModel : INotifyPropertyChanged, IPartImportsSatisfiedNotification
{
    #region Import ------------------------------------
    [Import(typeof(IMenuService))]
    protected IMenuService MenuService { get; set; }
    #endregion

    #region Public fields----------------------------
    public ObservableCollection<IMenuItem> MenuItems
    {
        get
        {
            return this.MenuService.MenuItems;
        }
    }

    private object selectedItem;
    public object SelectedItem
    {
        get { return selectedItem; }
        set
        {
            if (selectedItem != value)
            {
                selectedItem = value;
                OnPropertyChanged("SelectedItem");
            }
        }
    }

    #endregion


    #region Public Fields-----------------

    public DelegateCommand<object>  NewCommand { get; private set; }    

    #endregion

    #region Constrcator

    public ListViewModel()
    {
        this.NewCommand = new DelegateCommand<object>(OnNewExcute, CanNewExcute);


    }       

    #endregion

    #region NewCommand-----------------

    private void OnNewExcute(object item)
    {

    }

    private bool CanNewExcute(object item)
    {
        return true;
    }
    #endregion


    #region IPartImportsSatisfiedNotification -----------------

    public void OnImportsSatisfied()
    {
        this.MenuService.AddMenuItem(this.NewCommand,selectedItem)

    }

    #endregion

}

MenuService代码如下:

 [Export(typeof(IMenuService))]
[PartCreationPolicy(CreationPolicy.NonShared)]
public class MenuService:IMenuService
{
    #region Constructor
    public MenuService()
    {
        this.MenuItems = new ObservableCollection<IMenuItem>();
    }        
    #endregion


    #region IMenuService
    public ObservableCollection<IMenuItem> MenuItems{ get; internal set; }

    public void AddMenuItem(ICommand command,object commandParameter)
    {
        this.MenuItems.Add(new MenuItem() { Command = command, CommandParameter=commandParameter});
    }


    #endregion


}

下面的MenuItem代码:

  public class MenuItem:DependencyObject, IMenuItem 
{
    public static readonly DependencyProperty CommandParameterProperty;
    static MenuItem()
    {       
        CommandParameterProperty = DependencyProperty.Register("CommandParameter", typeof(object), typeof(MenuItem), new PropertyMetadata(null));
    }

    public object CommandParameter
    {
        get { return (object)GetValue(CommandParameterProperty); }
        set { SetValue(CommandParameterProperty, value); }
    }

    public ICommand Command { get; set; }


}

但是当运行代码并且SelectedItem被更改时。 CommandParameter不会改变。如果我插入seletedItem =“Test”。 NewCommand将收到参数。但是更改SelectedItem。它仍将通过“测试”。这是我的问题。

0 个答案:

没有答案