locked
How to add style to a ListViewItem through code? RRS feed

  • Question

  • ListViewItem newItem = new ListViewItem();

    After creating an instance of ListViewItem, how do I add to the Style component?

    I have my own local resource and I want to assign it to the "newItem". I assume it would be along the lines of 

    newItem.Style = // something here
    What needs to be added on the RHS to do so?
    Sunday, August 18, 2013 7:45 PM

Answers

  • Something along the lines of this:

     

    <ListView x:Name="myListView">
       <ListView.ItemTemplate>
           <DataTemplate>
                 <TextBlock Text="{Binding}" /> <!--put whatever content you need here for each item -->
           </DataTemplate>
       </ListView.ItemTemplate>
    </ListView>

    and in code you define an observable collection of your data. Example of a simple data model that just contains strings:
    var data = new ObservableCollection<string>();
    data.Add("First item");
    data.Add("Second item");
    data.Add("Third item");
    myListView.ItemsSource = data;

    then if you need to add or remove an item, you just call add or remove on 'data'. No reason to mess with ListViewItem, Styles etc at all. You code-behind should just deal with the data, and you xaml view with how that data should be rendered. Basically try and avoid putting UI in your code.


    /Morten
    twitter: http://www.twitter.com/dotMorten
    blog: http://www.sharpgis.net

    • Marked as answer by Anne Jing Thursday, August 22, 2013 8:18 AM
    Monday, August 19, 2013 6:26 AM

All replies

  • Grab the resources property of where the style is defined. For instance if it's defined in app.xaml it would be:

    newItem.Style = App.Resources["localResourceKey"] as Style;

    However I would recommend against using ListViewItem and the ListView.Items property, and instead work with data collections on the ListView.ItemsSource property, and define an ItemTemplate on your listview. It's a much cleaner approach when dealing with dynamic data.


    /Morten
    twitter: http://www.twitter.com/dotMorten
    blog: http://www.sharpgis.net


    Sunday, August 18, 2013 7:50 PM
  • Thanks very much.

    How would I go about implementing the method you suggested?

    Sunday, August 18, 2013 8:08 PM
  • Something along the lines of this:

     

    <ListView x:Name="myListView">
       <ListView.ItemTemplate>
           <DataTemplate>
                 <TextBlock Text="{Binding}" /> <!--put whatever content you need here for each item -->
           </DataTemplate>
       </ListView.ItemTemplate>
    </ListView>

    and in code you define an observable collection of your data. Example of a simple data model that just contains strings:
    var data = new ObservableCollection<string>();
    data.Add("First item");
    data.Add("Second item");
    data.Add("Third item");
    myListView.ItemsSource = data;

    then if you need to add or remove an item, you just call add or remove on 'data'. No reason to mess with ListViewItem, Styles etc at all. You code-behind should just deal with the data, and you xaml view with how that data should be rendered. Basically try and avoid putting UI in your code.


    /Morten
    twitter: http://www.twitter.com/dotMorten
    blog: http://www.sharpgis.net

    • Marked as answer by Anne Jing Thursday, August 22, 2013 8:18 AM
    Monday, August 19, 2013 6:26 AM