WPF - set the ItemsSource of List View based on some condition

倖福魔咒の 提交于 2020-01-17 04:58:06

问题


Is it possible to set the ItemsSource of a list view based on Some condition. I tried the following, but it didn't work.

   <ListView ItemsSource="{Binding FirstTypeOfSource}"  .....>
        <ItemsControl.ItemContainerStyle>
                <Style >
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding Category}" Value="All">
                            <Setter Property="ItemsSource" Value="SecondTypeOfCategory"/>
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </ItemsControl.ItemContainerStyle>
   </ListView>

Can someone help to figure out the solution ?


回答1:


Try this

Add a name to a ListView.

<ListView x:Name="someName" ItemsSource="{Binding FirstTypeOfSource}" .....>

Use element name to find desired DependencyProperty:

<DataTrigger Binding="{Binding Category}" Value="All">
    <Setter TargetName="someName" Property="ItemsSource" Value="SecondTypeOfCategory"/>
</DataTrigger>



回答2:


This should do it

    <ListView ItemsSource="{Binding FirstTypeOfSource}">
        <ListView.Style>
            <Style TargetType="ListView">
                <Style.Triggers>
                    <DataTrigger Binding="{Binding Category}" Value="All">
                        <Setter Property="ItemsSource" Value="{Binding SecondTypeOfCategory}"/>
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </ListView.Style>
    </ListView>


来源:https://stackoverflow.com/questions/33039056/wpf-set-the-itemssource-of-list-view-based-on-some-condition

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!