我有一個(gè)命令綁定到我擁有的菜單項(xiàng),我想傳遞多個(gè)參數(shù)。我嘗試過使用轉(zhuǎn)換器,但它似乎沒有返回任何內(nèi)容。我的轉(zhuǎn)換器public class AddConverter : IMultiValueConverter { public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture) { return values.Clone(); } public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture) { throw new NotImplementedException(); }}我的視圖模型包含我的命令class myViewModel: ViewModelBase {private RelayCommand testCommand; public ICommand TestCommand { get { if (testCommand == null) { testCommand = new RelayCommand((param) => test((object[])param)); } return testCommand ; } } //Only trying to print out one of the params as a test public void test(object parameter) { var values = (object[])parameter; int num1 = Convert.ToInt32((string)values[0]); MessageBox.Show(num1.ToString()); }我對(duì)菜單項(xiàng)的綁定//Using tags as a test<ContextMenu> <MenuItem Name="testing" Header="Move to Position 10" Command="{Binding TestCommand}" Tag="7"> <MenuItem.CommandParameter> <MultiBinding Converter="{StaticResource AddConverter}"> <Binding ElementName="testing" Path="Tag"/> <Binding ElementName="testing" Path="Tag"/> </MultiBinding> </MenuItem.CommandParameter> </MenuItem></ContextMenu>調(diào)試后,當(dāng)我打開包含菜單項(xiàng)的窗口時(shí),轉(zhuǎn)換器會(huì)啟動(dòng),此時(shí)值對(duì)象為空。然后,當(dāng)我選擇菜單項(xiàng)并觸發(fā)命令時(shí),當(dāng)我開始執(zhí)行時(shí),參數(shù)為空。我不明白為什么我的轉(zhuǎn)換器在我單擊菜單項(xiàng)之前就啟動(dòng)了,或者為什么這些值為空。
1 回答

墨色風(fēng)雨
TA貢獻(xiàn)1853條經(jīng)驗(yàn) 獲得超6個(gè)贊
嘗試將ElementName綁定的 替換為RelativeSource. 這對(duì)我有用:
<MenuItem Name="testing" Header="Move to Position 10" Command="{Binding TestCommand}" Tag="7">
<MenuItem.CommandParameter>
<MultiBinding Converter="{StaticResource AddConverter}">
<Binding Path="Tag" RelativeSource="{RelativeSource Self}"/>
<Binding Path="Tag" RelativeSource="{RelativeSource Self}"/>
</MultiBinding>
</MenuItem.CommandParameter>
</MenuItem>
另請(qǐng)注意,您應(yīng)該綁定到TestCommand屬性而不是字段testCommand。
- 1 回答
- 0 關(guān)注
- 140 瀏覽
添加回答
舉報(bào)
0/150
提交
取消