ListBox复选框命令绑定

时间:2018-03-04 15:53:06

标签: c# wpf xaml mvvm mvvm-light

我无法弄清楚这一点。我以为我有正确的装订设置,但它没有开火。所以我有一个观点:

var nvc = tup.Aggregate(new NameValueCollection(),
  (seed, current) =>
  {
    seed.Add(current.Item1.ToString(), current.Item2.ToString());
    return seed;
  });

foreach (var item in nvc)
{
  Console.WriteLine($"Key = {item} Value = {nvc[item.ToString()]}");
}

我想在每次选中At C:\MRMReport.ps1:74 char:11 + #requires -runasadministrator + ~~~~~~~~~~~~~~~~~~~ Parameter runasadministrator requires an argument. + CategoryInfo : ParserError: (:) [], ParseException + FullyQualifiedErrorId : ParameterRequiresArgument 中的复选框时触发命令。我在我的视图模型上创建了一个命令,如下所示:

<ListBox x:Name="EquipmentViewsListBox"
         ItemsSource="{Binding EquipmentViews, UpdateSourceTrigger=PropertyChanged}"
         SelectionMode="Extended"
         BorderThickness="0"
         Height="150"
         Margin="5,5,10,10">
    <ListBox.ItemContainerStyle>
        <Style TargetType="ListBoxItem">
            <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
        </Style>
    </ListBox.ItemContainerStyle>
    <ListBox.ItemTemplate>
        <DataTemplate>
            <CheckBox IsChecked="{Binding IsSelected, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" 
                      Command="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=ListBox}, Path=DataContext.ViewSelected}" 
                      CommandParameter="{Binding RelativeSource={RelativeSource Self}}" 
                      Content="{Binding Name}"/>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

所以我理解当我执行ListBox时,该项的上下文变为public class SdddViewModel : ViewModelBase { public SdddModel Model { get; set; } public RelayCommand<ViewWrapper> ViewSelected { get; set; } public SdddViewModel(SdddModel model) { Model = model; ViewSelected = new RelayCommand<ViewWrapper>(OnViewSelected); } private void OnViewSelected(ViewWrapper obj) { var asd = obj; } } ,所以在我的情况下是一个类对象ListBox.ItemTemplate。对于内容的ListBoxItem绑定以及ViewWrapper属性,这都可以正常工作。这是检查项目时未触发的命令。我将相对祖先设置为NameIsSelected但仍未发生任何事情。想法?

1 个答案:

答案 0 :(得分:2)

问题是CommandParameter不匹配。您已将其声明为CommandParameterViewWrapper,但您使用CheckBox发送了RelativeSource Self类型的参数。将CommandParameter更改为{Binding},这意味着它会发送DataContext ListBoxItemViewWrapper

您可以使用Snoop检测到此绑定错误。