今天使用wpf的prism開發項目時,需要有在ListBox中的ListBoxItem觸發命令的需求,
在創建ListBoxItem的DataTemplate時,將prism的viewmodel中定義的command綁定到DataTemplate的Command上,
如下:
<DataTemplate x:Key="CanIdTemplate" DataType="{x:Type sys:String}">
<materialDesign:Chip Content="{Binding}" Margin="5,5,0,0" IsDeletable="True"
DeleteCommand="{Binding CanIdChipDeleteCommand}">
</materialDesign:Chip>
</DataTemplate>
但在運行時發現 CanIdChipDeleteCommand
沒有觸發。
然後將 materialDesign:Chip
單獨放到其他控件容器裏運行時可以正確觸發命令。
經試驗分析,應該是因爲 DataTemplate 與 數據模板運行時加載所在的控件 的上下文是獨立的,
而默認綁定情況下在prism的ViewModel中定義的CanIdChipDeleteCommand
命令在 DataTemplate 中的上下文找不到。
可以通過修改綁定方式,將命令綁定的目標關聯到與 ViewModel 所在的同一個上下文中。
修改代碼如下:
<UserControl x:Class="VrmsServer.Desktop.Views.Functions.Configure.TboxCanIdSettingControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:prism="http://prismlibrary.com/"
xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
xmlns:converter="clr-namespace:VrmsServer.Desktop.Converter"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
Background="#d7d7d7" x:Name="userControl"
prism:ViewModelLocator.AutoWireViewModel="True">
<UserControl.Resources>
<DataTemplate x:Key="CanIdTemplate" DataType="{x:Type sys:String}">
<materialDesign:Chip Content="{Binding}" Margin="5,5,0,0"
IsDeletable="True"
DeleteCommand="{Binding ElementName=userControl, Path=DataContext.CanIdChipDeleteCommand}"
DeleteCommandParameter="{Binding}">
</materialDesign:Chip>
</DataTemplate>
</UserControl.Resources>
</UserControl>
代碼修改如上後,可以正確觸發 ViewModel
中的 CanIdChipDeleteCommand
命令了。