0

I wonder what the best design is to show different content dynamically in a list view.

What I want is (e.g.) depending on a radio button, a list view be filled with items (setting ItemsSource, changing column width, names and the row information itself).

What I'm currently doing is dynamically changing all the list view properties.

I wonder if it would be better to use multiple list views (maybe even as user controls), all on the same window position and hide all non-used.

Also, since I'm using MVVM Light/WPF I want to easily connect it to the XAML code if possible.

3
  • 1
    If you change Content and want the ListView to follow that change in displaying data why don't you change DataTemplate whenever you change data?
    – Maximus
    Dec 17, 2014 at 12:20
  • 1
    Bind ObservableCollection to your radio button and change this collection according to your actions. Check out this component github.com/snmslavk/WPFOnScreenKeyboard . The data of buttons changing dynamicly
    – slava
    Dec 17, 2014 at 12:21
  • Thanks for your answers. I think both might use the same solution. (why not make it an answer, so I can 'award' you the points). It looks better than hiding widgets. And the example looks very good (I now have ugly code behind). Dec 17, 2014 at 12:27

1 Answer 1

2

If changes in ItemsSource occur in time (or you want change it depending on some conditions like RadioButton's IsChecked property) you need to reflect on change your DataTemplate. The way out is to create DataTrigger which will swap your DataTemplates based on some criteria.

   <ListView>
        <ListView.Style>
            <Style TargetType="ListView">
                <Setter Property="ItemsSource" Value="{Binding InitalSource}"/>
                <Setter Property="ItemTemplate" Value="{StaticResource InitialDataTemplate}"/>
                <Style.Triggers>
                    <DataTrigger Binding="{Binding ElementName=RadioButton, Path=IsChecked}" Value="True">
                        <Setter Property="ItemsSource" Value="{Binding AnotherSource}"/>
                        <Setter Property="ItemTemplate" Value="{StaticResource AnotherDataTemplate}"/>
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </ListView.Style>
    </ListView>

Whenever condition of DataTrigger stops being true all changes made by Setters vanish, by default, therefore you do not need to add another DataTrigger in case of False value for RadioButton's IsChecked.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.