重置wpf中的binding's源
本文关键字:wpf 中的 binding 重置 | 更新日期: 2023-09-27 18:02:32
我有这样一个列表框:
<ListBox DockPanel.Dock="Left" HorizontalAlignment="Left" Width="150"
ItemsSource="{Binding PcConfigurations, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding}">
<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseDoubleClick">
<cmd:EventToCommand Command="{Binding LocalConfigurationCommand}"
CommandParameter="{Binding}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</TextBlock>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
当我运行应用程序时,我在输出中得到了这个错误:
System.Windows.Data Error: 40 : BindingExpression path error: 'LocalConfigurationCommand' property not found on 'object' ''PcConfiguration_0CC914504C64AE357F440BEA28C5F73FD3627331B5E407B6D7DD75076453D393' (HashCode=20396001)'. BindingExpression:Path=LocalConfigurationCommand; DataItem='PcConfiguration_0CC914504C64AE357F440BEA28C5F73FD3627331B5E407B6D7DD75076453D393' (HashCode=20396001); target element is 'EventToCommand' (HashCode=27147755); target property is 'Command' (type 'ICommand')
我猜这是因为
的当前路径EventToCommand Command="{Binding LocalConfigurationCommand}"....
查找的是列表框在其绑定中设置的位置。
所以我想问我怎么能把我的ViewModel的位置,所以它可以找到那个命令,但在CommandParameter={Binding}
的位置仍然是当前项目从ListBox。
视图模型实际上是ListBox
的数据上下文,您可以通过使用RelativeSource
来访问ListBox
:
<cmd:EventToCommand Command="{Binding Path=DataContext.LocalConfigurationCommand, RelativeSource={RelativeSource AncestorType=ListBox}}"
CommandParameter="{Binding}"/>