Itemscontrol中的ICommand Button绑定

时间:2015-01-26 13:04:51

标签: c# wpf xaml

让xaml.cs文件包含我的ViewModel的ObservableCollection。我现在已经实现了一个命令绑定到一个按钮单击,它在viewmodel中调用我的函数。问题是我没有在我的按钮点击功能

中获取列表中的项目

XAML

<ItemsControl ItemsSource="{Binding ConditionList}" AlternationCount="{Binding ConditionList.Count}">
 <ItemsControl.ItemTemplate>
  <DataTemplate>
   <WrapPanel>
    <Button Content="{Binding}" Command="{Binding DataContext.DeleteCondition, 
    RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}" CommandParameter="{Binding}" />
   </WrapPanel>
  </DataTemplate>
 </ItemsControl.ItemTemplate>
</ItemsControl>

请注意我的按钮位于ItemControl

VM

    private void DoDeleteCondition(object parameter)
    {
      //  if (parameter != null)
      //      ...
    }

    public ICommand DeleteCondition
    {
        get
        {
            if (_DeleteCondition == null) 
                _DeleteCondition = new RelayCommand(o => DoDeleteCondition(o));
            return _DeleteCondition;
        }
    }

2 个答案:

答案 0 :(得分:2)

您需要创建RelayCommand<T>,其中T是ConditionList中的项目。然后,您将在execute方法中获取参数。

答案 1 :(得分:0)

我觉得你的绑定设置有点落后。

在ItemsControl中你想要:

  1. 您的收藏中的项目以及单击单个项目时将执行的一个命令
  2. 或者您想要在其他地方的单个项目上执行的可能命令列表(意味着该集合显示在某个父元素上,因此您可以以某种方式绑定到单个项目)?
  3. ...或者您可能为集合中的每个项目定义了单独的命令......? (那么,你的集合中的元素是如何实现的?)
  4. 取决于你的答案:

    1:

     <ItemsControl ItemsSource="{Binding Path=MyObservableCollection}" >
                    <ItemsControl.ItemTemplate>
                        <DataTemplate>
                            <WrapPanel>
                                <Button Content="{Binding}"
                                        Command="{Binding Path=DataContext.DeleteCondition, RelativeSource={RelativeSource AncestorType=AncestorWithYourViewModelAsDataContext}}" 
                                        CommandParameter="{Binding}" />
                            </WrapPanel>
                        </DataTemplate>
                    </ItemsControl.ItemTemplate>
                </ItemsControl>
    

    2:

    <ItemsControl ItemsSource="{Binding Path=ConditionList}" >
                <ItemsControl.ItemTemplate>
                    <DataTemplate>
                        <WrapPanel>
                            <Button Content="{Binding}" 
                                    Command="{Binding Path=MyConditionalCommand}"
                                    CommandParameter="{BindingToTheElementOfYourCllectionThatYouWantToActUpon}" />
                        </WrapPanel>
                    </DataTemplate>
                </ItemsControl.ItemTemplate>
            </ItemsControl>
    

    ViewModel中的示例实现:

    private List<ConditionalCommand> _ConditionList;
    
            public List<ConditionalCommand> ConditionList
            {
                get { return _ConditionList; }
                set
                {
                    if (_ConditionList != value)
                    {
                        _ConditionList = value;
                        OnPropertyChanged("ConditionList");
                    }
                }
            }
    

    ...

    class ConditionalCommand
    {
            public ICommand MyConditionalCommand { get; set; }
            public string Name { get; set; }
            public override string ToString()
            {
                return Name;
            }
    
    }
    

    ...

     this.ConditionList = new List<ConditionalCommand>();
      this.ConditionList.Add(new ConditionalCommand{ MyConditionalCommand = DeleteCondition , Name="Delete"});
      this.ConditionList.Add(new ConditionalCommand{ MyConditionalCommand = DeleteSpecial, Name="Delete special" });
    

    ...

    private void DoDeleteCondition(object parameter)
        {
          //  if (parameter != null)
          //      ...
        }
    
        public ICommand DeleteCondition
        {
            get
            {
                if (_DeleteCondition == null) 
                    _DeleteCondition = new RelayCommand(o => DoDeleteCondition(o));
                return _DeleteCondition;
            }
        }
    
    // DeleteSpecial implemented in similar manner...