Xaml 基于属性更改可见性绑定

Xaml 基于属性更改可见性绑定,xaml,uwp,uwp-xaml,Xaml,Uwp,Uwp Xaml,我的视图模型中有一个Foo对象列表。每个Foo对象都有一个属性shouldbeevisible,该属性根据某种逻辑返回true或false,用于判断该项是否应显示给用户 我创建了一个IValueConverter,它绑定到ShouldBeVisible属性以返回Visibility.Visible和collapsed 一切都很好,我只能适当地显示应该显示在StackPanel中的Foo项 现在我想在我的页面上添加一个标记为“Show all”的复选框,该复选框将绑定到我的ViewModel上的一

我的视图模型中有一个Foo对象列表。每个Foo对象都有一个属性shouldbeevisible,该属性根据某种逻辑返回true或false,用于判断该项是否应显示给用户

我创建了一个IValueConverter,它绑定到ShouldBeVisible属性以返回Visibility.Visible和collapsed

一切都很好,我只能适当地显示应该显示在StackPanel中的Foo项

现在我想在我的页面上添加一个标记为“Show all”的复选框,该复选框将绑定到我的ViewModel上的一个属性。当它被选中时,我想显示所有的Foo项,不管shouldbeevisible说什么,如果它被选中,则跟随shouldbeevisible

我不确定如何正确绑定,因为IValueConverter只能绑定到一个项目


是否有方法在运行时根据是否选中我的复选框来更新可见性绑定?

在WPF中,您可以使用
IMultiValueConverter
,但它不适用于UWP。在这种情况下,您可以使用

github上有一个具有此功能的示例:

安装在NuGet软件包中:
Cimbalino.Toolkit
Microsoft.Xaml.Behaviors.Uwp.Managed

下面是一个简化的例子。您应该在VisibilityConverter中验证传入参数

视图:


在更改VireModel上的
ShowAll
绑定属性时,您是否可以遍历对象列表并将它们全部标记为
shouldbeevisible
<Page
    x:Class="Q46430426.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:Q46430426"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:interactivity="using:Microsoft.Xaml.Interactivity"
    xmlns:behaviors="using:Cimbalino.Toolkit.Behaviors"
    mc:Ignorable="d"
    x:Name="page">
    <Page.Resources>
        <local:VisibilityConverter x:Key="VisibilityConverter" />
    </Page.Resources>
    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
        <Grid.RowDefinitions>
            <RowDefinition Height="30"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>

        <CheckBox IsChecked="{Binding ShowAll, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" Content="ShowAll"></CheckBox>

        <ItemsControl Grid.Row="1" ItemsSource="{Binding Items, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}">
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <Border Name="border" Margin="3" BorderThickness="2" BorderBrush="Black">
                        <interactivity:Interaction.Behaviors>
                            <behaviors:MultiBindingBehavior PropertyName="Visibility" Converter="{StaticResource VisibilityConverter}" >
                                <behaviors:MultiBindingItem Value="{Binding DataContext.ShouldBeVisible, RelativeSource={RelativeSource Mode=TemplatedParent}}"/>
                                <behaviors:MultiBindingItem Value="{Binding DataContext.ShowAll, ElementName=page}"/>
                            </behaviors:MultiBindingBehavior>
                        </interactivity:Interaction.Behaviors>
                        <TextBlock Text="{Binding Name}"></TextBlock>
                    </Border>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>
    </Grid>
</Page>
public class MainViewModel : ViewModelBase
{
    private bool _showAll;
    private ObservableCollection<Foo> _items;

    public MainViewModel()
    {
        Items = new ObservableCollection<Foo>()
    {
        new Foo(){Name = "Test1", ShouldBeVisible = true},
        new Foo(){Name = "Test2", ShouldBeVisible = false},
        new Foo(){Name = "Test3", ShouldBeVisible = true},
        new Foo(){Name = "Test4", ShouldBeVisible = false},
        new Foo(){Name = "Test5", ShouldBeVisible = true},
    };
    }

    public ObservableCollection<Foo> Items
    {
        get { return _items; }
        set { _items = value; RaisePropertyChanged(); }
    }

    public bool ShowAll
    {
        get { return _showAll; }
        set { _showAll = value; RaisePropertyChanged(); }
    }
}
public class VisibilityConverter : MultiValueConverterBase
{
    public override object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
    {
        var shouldBeVisible = (bool)values[0];
        var showAll = (bool)values[1];
        if (showAll || shouldBeVisible) return Visibility.Visible;
        return Visibility.Collapsed;
    }

    public override object[] ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}