2017-09-26 8 views
2

ビューモデルにFooオブジェクトのリストがあります。各Fooオブジェクトには、そのアイテムをユーザーに表示するかどうかのロジックに基づいてtrueまたはfalseを返したShouldBeVisibleというプロパティがあります。プロパティに基づいて可視性バインディングを変更します。

ShouldBeVisibleプロパティにバインドしてVisibility.Visibleを返し、折り畳まれたIValueConverterを作成しました。

すべてが素晴らしいですし、私はStackPanelに表示されるはずのFoo項目だけを適切に表示できます。

"View all"と表示されたページに、自分のViewModelのプロパティにバインドするチェックボックスを追加します。それがチェックされると、ShouldBeVisibleが何を言っていても、ShouldBeVisibleに従うのがチェックされていない場合でも、すべてのFooアイテムを表示したいと思います。

IValueConverterは1つのアイテムにのみバインドできるので、これを正しくバインドする方法が不明です。

実行時に可視性のバインディングを更新する方法は、チェックボックスをオンにしているかどうかに基づいていますか?

+1

、あなたはを通して繰り返すことができませんでした'ShouldBeVisible'として適切にマークしてください。 – Lindsay

答えて

0

WPFではIMultiValueConverterを使用できますが、UWPでは使用できません。この場合、Cimbalino Toolkitを使用できます。 githubの上で

は、この機能を持つ例です。

https://github.com/Cimbalino/Cimbalino-Phone-Toolkit/tree/master/samples/MultiBinding/MultiBinding

はNuGetパッケージでインストールします。 Cimbalino.ToolkitMicrosoft.Xaml.Behaviors.Uwp.Managed

以下は簡単な例です。 VisibilityConverterで入力パラメータを検証する必要があります。

ビュー:

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

のViewModel:

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

VisibilityConverter:VireModel上の `ShowAll`バウンドプロパティの変更に

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(); 
    } 
} 
関連する問題