locked
[UWP] How to change ListView DataTemplate in dependency of a variable RRS feed

  • Question

  • Hello,

    On my Page code behind I have an variable:

    string mode; //values "Editable", "Checkable"

    And I have an ListView.

    I want to change ListViewItem data template in dependecy of variable "mode" (not in dependency of an ListViewItem).

    How to code such behavior?

    Monday, July 17, 2017 10:49 PM

Answers

  • Hi BitSmithy,

    Some suggestions for you :

    First , you can use DataTemplateSelector and define the different DataTemplates in XMAL .

    Then ,since you don't want to change listviewItem by listviewItem ,just return the default dataTemplate in the Method "SelectTemplateCore",

    protected override DataTemplate SelectTemplateCore(object item, DependencyObject container)
    {
      return DataTemplate1;
    }

    At last, when the variable changes, the dataTemplate can be changed  in dependency of  it.

    if (mode == "Editable")
    {
       itemGridView.ItemTemplate = (DataTemplate)Resources["DataTemplate1"];
    }
    else if (mode == "Checkable")
    {
       itemGridView.ItemTemplate = (DataTemplate)Resources["DataTemplate2"];
    }

    Best regards,

    Cole



    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Tuesday, July 18, 2017 7:43 AM
  • Hi BitSmithy,

    I'm sorry I didn't make myself clear.

    protected override DataTemplate SelectTemplateCore(object item, DependencyObject container)
    {
       return DataTemplate1;
    }

    The Method above just returns the initial datatemplate for all items.

    When you use this code "itemGridView.ItemTemplate = (DataTemplate)Resources["DataTemplate2"];" , it will change the datatemplate for all items .

    Click the Button, change the mode ,and all items will be changed.

    Best regards,

    Cole


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Thursday, July 20, 2017 1:55 AM

All replies

  • Hi BitSmithy,

    Is DataTemplateSelector what you are looking for?

    Refer: Dynamically choose DataTemplate in WinRT

    If that is not what you are looking for, could you please describe in details what's expected behaviors when the mode is Editable, and what do you expect when it changes to Checkable?


    I build UWPs: Arrnage Pro, Cloud Resource Tools

    Tuesday, July 18, 2017 5:05 AM
  • Hi BitSmithy,

    Some suggestions for you :

    First , you can use DataTemplateSelector and define the different DataTemplates in XMAL .

    Then ,since you don't want to change listviewItem by listviewItem ,just return the default dataTemplate in the Method "SelectTemplateCore",

    protected override DataTemplate SelectTemplateCore(object item, DependencyObject container)
    {
      return DataTemplate1;
    }

    At last, when the variable changes, the dataTemplate can be changed  in dependency of  it.

    if (mode == "Editable")
    {
       itemGridView.ItemTemplate = (DataTemplate)Resources["DataTemplate1"];
    }
    else if (mode == "Checkable")
    {
       itemGridView.ItemTemplate = (DataTemplate)Resources["DataTemplate2"];
    }

    Best regards,

    Cole



    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Tuesday, July 18, 2017 7:43 AM
  • >> Then ,since you don't want to change listviewItem by listviewItem ,just return the default dataTemplate in the Method "SelectTemplateCore"....

    It looks fine but if I change mode I want to change all items template - not only one item.

    Changing mode is on button click event.

    For example if I change mode for checkable i want all items to have CheckBox and TextBlock.

    If I change mode to editable i want all items to have editable TextBox.

    How to change all items template at one time. Should I use iteration for every item or there is any way to do it at once without iteration.

    Wednesday, July 19, 2017 8:02 PM
  • Hi BitSmithy,

    I'm sorry I didn't make myself clear.

    protected override DataTemplate SelectTemplateCore(object item, DependencyObject container)
    {
       return DataTemplate1;
    }

    The Method above just returns the initial datatemplate for all items.

    When you use this code "itemGridView.ItemTemplate = (DataTemplate)Resources["DataTemplate2"];" , it will change the datatemplate for all items .

    Click the Button, change the mode ,and all items will be changed.

    Best regards,

    Cole


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Thursday, July 20, 2017 1:55 AM