【问题标题】:Only Enable the last Remove Element Button of a ListBox [duplicate]仅启用列表框的最后一个删除元素按钮 [重复]
【发布时间】:2016-01-11 11:36:47
【问题描述】:

在我的 ListBox.ItemTemplate 中,我有一个 TextBlock 和一个 Remove 按钮,只有当它是列表框的最后一个元素时,才必须启用该按钮。

【问题讨论】:

  • 你的问题是什么?你有没有尝试过,或者你只需​​要我们来做?
  • 如果没有看到您的原始代码,我们将不知道如何对您现有的代码库进行更改。请发帖a minimal example of what needs to change,并详细说明需要修改的地方。

标签: c# wpf listbox


【解决方案1】:

为您创建了一个简单的示例。由于您想使用 Button Control,您可以使用 Command 来启用或禁用您的按钮。

查看:

<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:WpfApplication1"
    Title="MainWindow" Height="350" Width="525">
<Window.DataContext>
    <local:ParentViewModel />
</Window.DataContext>

<ListBox Width="400" Height="auto" ItemsSource="{Binding MyList,Mode=TwoWay}" >
    <ListBox.Style>
        <Style TargetType="{x:Type ListBox}">
            <Setter Property="ItemTemplate" >
                <Setter.Value>
                    <DataTemplate>
                        <Button Content="{Binding}" Padding="15,5" Margin="10,4"
                                Command="{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type ListBox}},Path=DataContext.RemoveButtonCommand}"
                                CommandParameter="{Binding}"
                                />
                    </DataTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </ListBox.Style>
</ListBox>

查看模型:

 public class ParentViewModel : INotifyPropertyChanged
{

    public ParentViewModel()
    {
        MyList = new List<string>();
        MyList.Add("1");
        MyList.Add("2");
        MyList.Add("3");
        MyList.Add("4");
        MyList.Add("5");
        MyList.Add("6");
        RemoveButtonCommand = new RelayCommand(param => this.RemoveCommandAction(param), param => this.CanExecuteRemoveButtonCommand(param));
    }

    public event PropertyChangedEventHandler PropertyChanged;

    public void OnPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    private List<string> myList;

    public List<string> MyList
    {
        get { return myList; }
        set 
        { 
            myList = value;
            OnPropertyChanged("MyList");
        }
    }


    public RelayCommand RemoveButtonCommand { get; set; }

    public void RemoveCommandAction(object param)
    {
        MyList.Remove((string)param);
        MyList = new List<string>(MyList);
    }

    public bool CanExecuteRemoveButtonCommand(object param)
    {
        return MyList[MyList.Count - 1] == ((string)param);
    }
}

中继命令:

Copy from here

我只是检查 Button 的内容是否是 CanExecute 逻辑中列表的最后一个元素。所以我在 CommandParameter 绑定中传递它。您当然可以使用 listboxitem 的索引,或者只是传递整个项目并进行比较。这是最简单的方法。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-09-29
    • 2018-03-10
    • 2011-06-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-17
    相关资源
    最近更新 更多