将动态创建的菜单项绑定到Button.ContextMenu中的ICommand

时间:2013-11-05 10:24:36

标签: c# wpf xaml

好吧,这是WPF的明确新手,显然需要不断学习MVVM,我的代码并没有专门设计,但是我确实指定了一个类作为GUI的接口和控制器,而模型代码驻留在另一组类中。一直在搜索网络上的例子,以及类似我的问题,其中有很多,但经过迷宫三天后,我正在寻求帮助。

我需要的是一个简单的下拉菜单,其中包含可以动态更新的项目(它是一个与USB设备通信的应用程序,因此有许多可用的设备应显示其设备ID和序列号),以及当前选中的项目应该显示在Button上(或者我最终得到的Dropdown菜单的任何实现)。在这个例子中,我只是创建一个静态列表,但稍后将在完整的应用程序中动态更新相同的列表。

到目前为止,我看到它在正确的轨道上:我得到当前选定的设备ID字符串以显示在按钮上,按下按钮,我得到所有可用设备的列表(它没有'打扰我当前选择的设备在列表中冗余显示。但是,当选择一个项目时,我无法挂钩任何事件,因此无法更新按钮中的项目,或者做任何其他事情。

我的XAML如下。请注意,这大致被黑客攻击,并且这里有一些没有意义的东西,例如来自示例的“IsChecked”属性的“IsActive”。最大的问题是,就我所知,ContextMenu.Resources中的Setter属性似乎都没有做任何事情......尝试更改fontsize无济于事。当然,真正重要的问题是“MyCommand”绑定无效,该方法永远不会被调用。

    <Label Content="Device Selected:" HorizontalAlignment="Left" Margin="25,22,0,0" VerticalAlignment="Top" Width="124" FontWeight="Bold" FontSize="14" Height="25"/>
    <Button x:Name="DeviceSelMenuButton" Content="{Binding DeviceID_and_SN, Mode=TwoWay}" HorizontalAlignment="Left" Height="28" Margin="25,52,0,0" VerticalAlignment="Top" Width="187" FontSize="14" Click="DeviceSelMenuButton_Click">
        <Button.ContextMenu>
            <ContextMenu ItemsSource="{Binding DeviceID_SN_Collection, Mode=TwoWay}">
                <ContextMenu.Resources>
                    <Style x:Key="SelectDeviceStyle" TargetType="MenuItem">
                        <Setter Property="Command" Value="{Binding MyCommand}"/>
                       <Setter Property="CommandTarget" Value="{Binding RelativeSource Self}"/> 
                        <Setter Property="IsChecked" Value="{Binding IsActive}"/>
                        <Setter Property="IsCheckable" Value="True"/>
                        <Setter Property="FontSize" Value="14"/>
                    </Style>
                </ContextMenu.Resources>
            </ContextMenu>
        </Button.ContextMenu>
    </Button>

来自MainWindow.xaml.cs的代码:

public partial class MainWindow : Window
{
    CustomDeviceGUI _customDeviceGui = new CustomDeviceGUI();

    public MainWindow()
    {
        InitializeComponent();
        this.DataContext = _customDeviceGui;
    }

    private void DeviceSelMenuButton_Click(object sender, RoutedEventArgs e)
    {
        // " (sender as Button)" is PlacementTarget
        (sender as Button).ContextMenu.IsEnabled = true;
        (sender as Button).ContextMenu.PlacementTarget = (sender as Button);
        (sender as Button).ContextMenu.Placement = System.Windows.Controls.Primitives.PlacementMode.Bottom;
        (sender as Button).ContextMenu.IsOpen = true;
    }

    private void SomeMethod(object sender, DataTransferEventArgs e)
    {
        // TODO Somehow get the index of the selected menu item (collection index, 0-based)
        //     int selIndex = (sender as Button).ContextMenu.Items.IndexOf    ??         
        _customDeviceGui.UpdateDeviceID("RelayPro id updated");
    }

}

GUI代码:

class CustomDeviceGUI : INotifyPropertyChanged
{
    // Declare the event 
    public event PropertyChangedEventHandler PropertyChanged = delegate { }; 
    private string _deviceDisplayString;
    private ICommand _updateMenu; 
    List<string> ControllerDeviceList = new List<string>();

    private System.Collections.ObjectModel.ObservableCollection<string> _DeviceID_SN_Collection = new System.Collections.ObjectModel.ObservableCollection<string>();

    // CTOR
    public CustomDeviceGUI()
    {
        ControllerDeviceList.Add("CustomDevice Device 1");
        ControllerDeviceList.Add("CustomDevice Device 2");
        ControllerDeviceList.Add("CustomDevice Device 3");
        ControllerDeviceList.Add("CustomDevice Device 6");
        UpdateDeviceID(ControllerDeviceList[0]);
    }

    #region CustomDeviceGUI Properties

    public System.Collections.ObjectModel.ObservableCollection<string> DeviceID_SN_Collection
    {
        get
        {
            _DeviceID_SN_Collection.Clear();
            foreach (string str in ControllerDeviceList)
            {
                _DeviceID_SN_Collection.Add(str);
            }
            return _DeviceID_SN_Collection;
        }
        private set 
        {
            _DeviceID_SN_Collection = value;
        }
    }

    public string DeviceID_and_SN
    {
        get
        {
            return _deviceDisplayString;
        }
        private set
        {
            _deviceDisplayString = value;
        }
    }

    public ICommand MyCommand
    {
        get
        {
            if (_updateMenu == null)
                _updateMenu = new MyGuiCommand();

            return _updateMenu;
        }

    }


    #endregion

    #region Public Methods


    public void UpdateDeviceID(string deviceID)
    {
        this._deviceDisplayString = deviceID;
        RaisePropertyChangeEvent("DeviceID_and_SN");
        RaisePropertyChangeEvent("DeviceID_SN_Collection");            
    }

    #endregion

    protected void RaisePropertyChangeEvent(string name)
    {
        PropertyChangedEventHandler handler = this.PropertyChanged;

        try
        {
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(name));
            }
        }
        catch (Exception e)
        {
            //  ... TODO Remove this catchall or find specific exceptions
        }
    }


    public class MyGuiCommand : ICommand
    {
        public void Execute(object parameter)
        {
            //   Debug.WriteLine("Hello, world");
            int hmm = 3;
        }

        public bool CanExecute(object parameter)
        {
            return true;
        }
        public event EventHandler CanExecuteChanged // was ;
        {
            add { CommandManager.RequerySuggested += value; }
            remove { CommandManager.RequerySuggested -= value; }
        }
    }


} // class CustomDeviceGUI

2 个答案:

答案 0 :(得分:0)

我必须做的所有更改都在XAML中。主要是使用祖先来获取正确的数据上下文。我也切换到ContextMenu.ItemContainer而不是ContextMenu.Resources。

   <ContextMenu.ItemContainerStyle>
      <Style TargetType="MenuItem"> 
         <Setter Property="Command" Value="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Button}}, Path=DataContext.MyCommand}"/>
      </Style>
   </ContextMenu.ItemContainerStyle>

答案 1 :(得分:-1)

尽管我不确定我是否认为:

<Setter Property="Command" Value="{Binding MyCommand}"/>

绑定需要一个RoutedUICommand对象。

编辑: 我注意到的另一件事是你之前没有设置任何命令绑定。像这样:

<Window.CommandBindings>
    <CommandBinding Command="MyCommand" Executed="Execute" />
</Window.CommandBindings>

只是一个示例,您可以将CommandBindings设置为许多其他控件。