在WPF中隐藏列表框项

时间:2014-03-20 05:18:49

标签: c# wpf xaml listbox

我有一个ListBox控件,其中填充了多个ListBox项。每个项目都包含一个"继续"按钮和"推迟"按钮。一旦"推迟"我想隐藏那个ListBox项目(在我的情况下显示为一行)。单击按钮。我目前的代码似乎没有任何效果。

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;

    }

1 个答案:

答案 0 :(得分:1)

我必须解决同样的事情。列表框中的每个项目都应该是一个对象。我们暂时将其称为MyObject,因为我不知道您的对象类型是什么。在MyObject类中,您将执行Proceed和Postpone命令。

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

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

<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获取更改通知时,它会将bool转换为Visibility。 True =&gt;可见,错误=&gt;折叠。