仅启用列表框的最后一个“删除元素"按钮 [英] Only Enable the last Remove Element Button of a ListBox

查看:84
本文介绍了仅启用列表框的最后一个“删除元素"按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

解决方案

为您创建了一个简单的示例.由于要使用按钮控制",因此可以使用命令"来启用或禁用按钮.

查看:

<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);
    }
}

RelayCommand:

从此处复制

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

In my ListBox.ItemTemplate i have a TextBlock and a Remove button, the button must be enabled only if it's the last element o the listbox.

解决方案

Created a quick example for you. since you are want to use Button Control you can use Command to enable or disable your button.

View:

<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>

View Model:

 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);
    }
}

RelayCommand:

Copy from here

I'm Just checking if the content of the Button is the last element of the list in CanExecute logic.so I'm passing that in CommandParameter binding. You can surely use the Index of listboxitem or just pass the whole item and compare that. it's the easiest way of doing this.

这篇关于仅启用列表框的最后一个“删除元素"按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆