admin管理员组

文章数量:1353119

I have a StoreViewModel, which implements INotifyPropertyChanged. And a property in it:

public ObservableCollection<ItemModel> ItemsCollection
{
    get => _itemsCollection;
    set => SetProperty(ref _itemsCollection, value);
}

I want to hide certain items from being displayed in a ListView, if ItemModel.IsAvailable property is False. I know it should be with ItemContainerStyle, something like this:

<ListView.ItemContainerStyle>
    <Style TargetType="ListViewItem">
        <interactivity:Interaction.Behaviors>
            <core:DataTriggerBehavior
                Binding="{Binding RelativeSource={ ... }, Path=Content.IsAvailable , Mode=OneWay}"
                Value="False">
                <core:ChangePropertyAction
                    PropertyName="Visibility"
                    Value="Collapsed" />
            </core:DataTriggerBehavior>
        </interactivity:Interaction.Behaviors>
    </Style>
</ListView.ItemContainerStyle>

What should be the RelativeSource? I know how to do it in WPF, but not in UWP

I have a StoreViewModel, which implements INotifyPropertyChanged. And a property in it:

public ObservableCollection<ItemModel> ItemsCollection
{
    get => _itemsCollection;
    set => SetProperty(ref _itemsCollection, value);
}

I want to hide certain items from being displayed in a ListView, if ItemModel.IsAvailable property is False. I know it should be with ItemContainerStyle, something like this:

<ListView.ItemContainerStyle>
    <Style TargetType="ListViewItem">
        <interactivity:Interaction.Behaviors>
            <core:DataTriggerBehavior
                Binding="{Binding RelativeSource={ ... }, Path=Content.IsAvailable , Mode=OneWay}"
                Value="False">
                <core:ChangePropertyAction
                    PropertyName="Visibility"
                    Value="Collapsed" />
            </core:DataTriggerBehavior>
        </interactivity:Interaction.Behaviors>
    </Style>
</ListView.ItemContainerStyle>

What should be the RelativeSource? I know how to do it in WPF, but not in UWP

Share Improve this question asked Mar 31 at 19:33 Alexey TitovAlexey Titov 3532 silver badges13 bronze badges 3
  • You could try RelativeSource Self, and could you please provide the relevant code of Listview so that we can better solve the problem for you? – Junjie Zhu - MSFT Commented Apr 1 at 3:15
  • 1 You can bind the Visibility property via an attached property as shown here: stackoverflow/a/33582406/1136211. – Clemens Commented Apr 1 at 20:14
  • You could try to filter your data source in your viewmodel instead of doing this in the LIstView. – Roy Li - MSFT Commented 2 days ago
Add a comment  | 

1 Answer 1

Reset to default 0

Since you are using MVVM, maybe solve this in another way. You could try to change the data source in the ViewModel instead of handing this in the view level. You could just pass a list which doesn't contains the item you want to show to the ListView as data source.

Something like this:

newList = new ObservableCollection<ItemModel>(ItemsCollection.Where(x => x.IsAvailable == true).ToList());

In this way, you don't need to change the XAML but just show it as you've already handled the data in your ViewModel.

本文标签: xamlHow to hide a ListView item in UWP depending on item39s propertyStack Overflow