0

From various questions, I know it is impossible to create DataTemplate from the code behind without using the XamlReader. So I want to ask if there is a way to programatically generates the UI for each Item in a ListView. I don't seem to find any relevant event handler or member of ListView for this purpose. Ideally, I want the ListView to invoke my handler code to generate UI for each data item it needs to display.

2
  • You can either generate UI and directly insert them into ListViewItems; or you can still create your normal data templates, generate UI and place them within those templates. Commented Jul 12, 2017 at 12:02
  • 1
    @JustinXL I added an answer based on your suggestion. Commented Jul 16, 2017 at 3:58

2 Answers 2

1

Imitating the official XamlTreeView sample, I have tried overriding some ListView method such as PrepareContainerForItemOverride but this won't work. The solution I found is as @JustinXL suggests: producing ListViewItem and insert them to the ListView->Items directly

//assume that items is a list of items we want to bind
myListView->Items->Clear();
for(auto i : items)
{
     ListViewItem^ v = ref new ListViewItem();
     v->Content = GenerateUIFor(i);
     myListView->Items->Append(v); // NOTE: a wrapping ListViewItem is required!
}

To support usual data binding, it would be best to make the data structure to cache the generated UI. For example,

ref class MyDataStructure
{
public:
    property ListViewItem^ Item
    {
        ListViewItem^ get()
        {
             if (_item == nullptr)
                 GenerateUI();
             return _item;
        }
    }
    void GenerateUI()
    {
        _item = ref new ListViewItem();
        _text_block = ref new TextBlock(); // sample
        _item->Content = _text_block;
        UpdateUI();
    }

    // Invoke this when changing the state of this object
    void UpdateUI()
    {
        if (_text_block != nullptr) // sample
        {
             _text_block->Text = this->ToString(); // sample
        }
    }
private:
    ListViewItem^ _item;
    TextBlock^ _text_block;
};

The downside of this is of course we can't make use of data virtualization. But it works properly for small set of data. For large set, one can use website's approach with Next and Prev button to load next page or go back to previous page.

Sign up to request clarification or add additional context in comments.

Comments

0

ItemContainerGenerator should let you construct the entire UI for an item inside a list view. Unfortunately there doesn't appear to be much in the way of non MSDN documentation/samples for this.

Alternatively if you can maintain a list of all the DataTemplates you might need to show, you could use a DataTemplateSelector to choose which DataTemplate you want to show for each individual item.

1 Comment

Well, I don't think it is possible with ItemContainerGenerator given I have experimented with overriding some method. There is no way to set a customized object anyway.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.