虽然这个问题非常类似于其他人发布的,但它有一些扭曲!我在一个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文件:
<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的一份副本:
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接口:
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;}
}发布于 2012-08-29 01:15:58
我将通过上下文菜单标记保留DataContext和隧道。
<!-- Keep complete ListBox, allows acces of selected item and higher up DataContext -->
<StackPanel Tag="{Binding RelativeSource={RelativeSource AncestorType=ListBox}}"><ContextMenu DataContext="{Binding PlacementTarget.DataContext, RelativeSource={RelativeSource Self}}"
Tag="{Binding PlacementTarget.Tag, RelativeSource={RelativeSource Self}}">因此,现在DataContext仍然是您的项目,如果您需要选择的项,请使用:
Tag.SelectedItem, RelativeSource={RelativeSource AncestorType=ContextMenu}https://stackoverflow.com/questions/12168871
复制相似问题