首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将ListBox中的CommandParameter与ContextMenu中的CommandParameter绑定

将ListBox中的CommandParameter与ContextMenu中的CommandParameter绑定
EN

Stack Overflow用户
提问于 2012-08-28 23:16:22
回答 1查看 3.3K关注 0票数 0

虽然这个问题非常类似于其他人发布的,但它有一些扭曲!我在一个ObservableCollection ListBox中有一个条目,并使用DataTemplate来显示一些集合成员。但是,我已经定义了一个DelegateCommands ContextMenu,因此我可以使用命令参数执行在ViewModel中定义的

到目前为止,我的所有绑定都在工作,但我需要使用在ViewModel中选择的项的CommandParameter为ListBox中的命令提供一个参数,以便命令知道要对哪个项进行操作。但这是不可能的,因为我更改了ContextMenu DataContext,这样我就可以在ViewModel中获得DelegateCommands。

我得到了以下错误: System.Windows.Data错误:4:无法找到引用'ElementName=instrumentListView‘绑定的源代码。BindingExpression:Path=SelectedItem;

ViewModel中的命令正如我所希望的那样被调用,但是它的参数总是为null。

我还试图绑定到集合中的项,但是它们是不可见的,因为我必须切换我的DataContext。

以下是缩写的XAML文件:

代码语言:javascript
复制
            <ListBox x:Name="instrumentListView" ItemsSource="{Binding Instruments}" >              
                <ListBox.ItemTemplate>
                    <DataTemplate>
                       <StackPanel x:Name="instrumentStackPanel" HorizontalAlignment="Left" Orientation="Horizontal" Height="30" UseLayoutRounding="True" Tag="{Binding DataContext, RelativeSource={RelativeSource AncestorType=ListBox}}">                            
                        <Image Source="{Binding InstrumentIcon}" Margin="0,0,10,0"></Image>
                        <Label Content="{Binding Instrument}"></Label>
                        <StackPanel.ContextMenu >
                            <ContextMenu DataContext="{Binding PlacementTarget.Tag, RelativeSource={RelativeSource Self}}">
                                <MenuItem Header="Add Instrument" Command="{Binding Path=AddInstrumentToTest}" CommandParameter="{Binding Instrument}"/>
                                <MenuItem Header="Remove Instrument" Command="{Binding Path=RemoveInstrumentFromTest}" CommandParameter="{Binding Instrument}"/>
                            </ContextMenu>
                        </StackPanel.ContextMenu>
                        </StackPanel>                            
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>

下面是我的ViewModel的一份副本:

代码语言:javascript
复制
     public class ViewModel : INotifyPropertyChanged
    {
        public DelegateCommand<string> AddInstrumentToTest { get; set; }
        public DelegateCommand<string> RemoveInstrumentFromTest { get; set; }
        private ObservableCollection<IInstrument> instruments = new ObservableCollection<IInstrument>();

        public ViewModel()
        {
            this.AddInstrumentToTest = new DelegateCommand<string >(this.OnaddInstrumentToTest, this.canAddInstrument);
            this.RemoveInstrumentFromTest = new DelegateCommand<string >(this.OnremoveInstrumentFromTest, this.canRemoveInstrument);      
        }

        public ObservableCollection<IInstrument> Instruments
        {
            ...
        }

        public void OnaddInstrumentToTest(string inst) {...}
        public void OnremoveInstrumentFromTest(string inst) {...}
        public bool canRemoveInstrument(string inst) {...}
        public bool canAddInstrument(string inst) {...}

        ...
}

下面是IInstrument接口:

代码语言:javascript
复制
        public interface IInstrument
        {        
            string Model {get;set;}       
            string SerialNumber {get;set;}    
            InstrumentCommInterface.InstrumentInterface InstrumentComm {get;set;} 
            InstrumentClassification.InstrumentClass InstrumentClass {get;set;} 
            string FirmwareVersion {get;set;}  
            string Address {get;set;}
            string Port {get;set;}
            Lazy<IInstrumentFactory, IInstrumentPlugInMetaData> PlugInType {get;set;}
            string Instrument {get;set;}
            BitmapImage InstrumentIcon {get;set;}
            bool SelectedForTest {get;set;}
            ObservableCollection<string> Channels {get;set;}        
        }
EN

回答 1

Stack Overflow用户

发布于 2012-08-29 01:15:58

我将通过上下文菜单标记保留DataContext和隧道。

代码语言:javascript
复制
<!-- Keep complete ListBox, allows acces of selected item and higher up DataContext -->
<StackPanel Tag="{Binding RelativeSource={RelativeSource AncestorType=ListBox}}">
代码语言:javascript
复制
<ContextMenu DataContext="{Binding PlacementTarget.DataContext, RelativeSource={RelativeSource Self}}"
             Tag="{Binding PlacementTarget.Tag, RelativeSource={RelativeSource Self}}">

因此,现在DataContext仍然是您的项目,如果您需要选择的项,请使用:

代码语言:javascript
复制
Tag.SelectedItem, RelativeSource={RelativeSource AncestorType=ContextMenu}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12168871

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档