在WPF中隐藏列表框项目 [英] Hiding A Listbox Item in WPF

查看:73
本文介绍了在WPF中隐藏列表框项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ListBox控件,其中填充了几个ListBox项.每个项目都包含一个继续"按钮和一个推迟"按钮.单击推迟"按钮后,我想隐藏该ListBox项(在本例中显示为一行).我目前拥有的代码似乎没有任何效果.

I have a ListBox control populated with several ListBox items. Each item contains a "Proceed" button and a "Postpone" button. I would like to hide that ListBox item (presented as a row in my case) once the "Postpone" button is clicked. The code I have currently doesn't seem to have any effect.

XAML:

<ListBox.ItemContainerStyle>
         <Style TargetType="{x:Type ListBoxItem}">
               <Style.Triggers>
                   <DataTrigger Binding="{Binding PostponeClicked}" Value="1">
                       <Setter Property="IsEnabled" Value="False"/>
                   </DataTrigger>
                </Style.Triggers>
         </Style>
</ListBox.ItemContainerStyle>

C#:

 private void PostponeThirdPartyUpdatesButton_Click(object sender, RoutedEventArgs e)
    {
        DataTrigger d = new DataTrigger();
        d.Binding = new Binding("PostponeClicked");
        d.Value = 1;

        var context = ((FrameworkElement)sender).DataContext as Tuple<RegScan_ThirdParty.InstalledApplicationFromRegistryScan, RegScan_ThirdParty.ManifestRequiredApplication, RegScan_ThirdParty.RequiredApplicationState>;

        Button ThirdPartyPostponeButton = sender as Button;
        ThirdPartyPostponeButton.IsEnabled = false;

        if (context != null)
        {
            RegScan_ThirdParty.registryApplicationPostponeWorkflow(context);
        }

        ThirdPartyPostponeButton.IsEnabled = true;

    }

推荐答案

我不得不解决一次相同的问题.列表框中的每个项目都应该是一个对象.由于我不知道您的对象类型是什么,我们现在将其称为MyObject.在MyObject类中,您将放入Proceed和Postpone命令.

I had to address the same thing once. Each item in your list box should be an object. We'll call it MyObject for now, since I have no idea what your object type is. In the MyObject class, you'll put your Proceed and Postpone commands.

//ViewModelBase implements INotifyPropertyChanged, which allows us to call RaisePropertyChanged, and have the UI update
class MyObject : ViewModelBase
{
    private bool isNotPostponed = true;
    public bool IsNotPostponed
    {
        get { return isNotPostponed; }
        set 
        { 
            isNotPostponed = value; 
            RaisePropertyChanged("IsNotPostponed");
        }
    }

    private Command postponeCommand;
    public Command PostponeCommand
    {

        get 
        {
            if (postponeCommand == null)
                postponeCommand = new Command(PostponeCommand);
            return postponeCommand; 
        }

    }

    private void Postpone(object x)
    {
        IsNotPostponed = false;
    }

    //similar code for Proceed Command
}

然后在显示列表框的视图的视图模型中,创建一个可以绑定到列表框(或要使用的任何集合)的列表.我在下面的XAML中将其称为MyObjectsList. (我没有显示该对象所在的ViewModel代码,但我假设您具有绑定到ListBox的代码.)然后在ItemsControl.ItemTemplate中,绑定到List中的每个MyObject.

Then in the viewmodel of the view that displays the listBox, create a List that you can bind to your listbox (or whatever collection you want to use). I called it MyObjectsList in the XAML below. (I'm not showing the ViewModel code where this object lives, but I assume you have code for binding to the ListBox.) Then in your ItemsControl.ItemTemplate, bind to each MyObject in your List.

<ItemsControl ItemsSource="{Binding MyObjectsList}">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <DataTemplate.Resources>
                <BooleanToVisibilityConverter x:Key="boolToVis"/>
            </DataTemplate.Resources>
            <StackPanel Visibility="{Binding IsNotPostponed, Converter={StaticResource boolToVis}}">
                <Button Command="{Binding PostponeCommand}" Content="Postpone"/>
                <Button Command="{Binding ProceedCommand}" Content="Proceed"/>
            </StackPanel>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

单击Postpone后,该命令将执行Postpone(),它将IsNotPostponed设置为false.将IsNotPostponed设置为false时,RaisePropertyChanged通知UI IsNotPostponed已更改(您需要实现INotifyPropertyChanged接口.)最后,当UI获取更改通知时,它将布尔值转换为Visibility.正确=>可见,错误=>崩溃.

When Postpone is clicked, the command will execute Postpone(), which will set IsNotPostponed to false. On setting IsNotPostponed to false, RaisePropertyChanged tells the UI that IsNotPostponed changed (you need to implement the INotifyPropertyChanged interface.) Lastly, when the UI gets the change notification, it converts the bool to a Visibility. True => Visible, False => Collapsed.

这篇关于在WPF中隐藏列表框项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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