0

I have this code on my xaml and works fine (ListView component)

<ListView.ItemsPanel>
  <ItemsPanelTemplate>
    <StackPanel Orientation="Horizontal"></StackPanel>
  </ItemsPanelTemplate>

I´m trying to replicate on code behind using this answer Create DataTemplate in code behind, but i could not make it work (the Russell´s answer). Any help will be appreciated. Thanks!

Edit:

ListView listView = new ListView();
listView.ItemsPanel = GetItemsPanelTemplate();

private ItemsPanelTemplate GetItemsPanelTemplate()
{
  string xaml = @"<ItemsPanelTemplate   xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
                            <ListView.ItemsPanel>
                            <ItemsPanelTemplate>
                            <StackPanel Orientation=""Horizontal""></StackPanel>
                            </ItemsPanelTemplate>
                            </ListView.ItemsPanel>
                </ItemsPanelTemplate>";
  return XamlReader.Parse(xaml) as ItemsPanelTemplate;
}
Patroni
  • 23
  • 5
  • 1
    Show us the code you wrote that doesn't work, and we'll tell you what's wrong with it. – Mike Strobel Feb 06 '18 at 16:10
  • I edit the question – Patroni Feb 06 '18 at 16:18
  • You have to actually read the error message... please compare the upper xaml with your code-xaml-string. The latter has `ListView.ItemsPanel` nested within `ItemsPanelTemplate`, which ofcourse doesn't work. – grek40 Feb 06 '18 at 16:23
  • *Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, __a specific problem or error__ and the shortest code necessary to reproduce it in the question itself.* – grek40 Feb 06 '18 at 16:26

1 Answers1

1

Your code would work if you stripped out the ListView.ItemsPanel element and the inner ItemsPanelTemplate element:

private ItemsPanelTemplate GetItemsPanelTemplate()
{
    return XamlReader.Parse(
        @"<ItemsPanelTemplate xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'>
            <StackPanel Orientation='Horizontal' IsItemsHost='True' />
          </ItemsPanelTemplate>") as ItemsPanelTemplate;
}

However, the preferred way, based on the answer you linked, would be:

private ItemsPanelTemplate GetItemsPanelTemplate()
{
    var factory = new FrameworkElementFactory(typeof(StackPanel));

    factory.SetValue(StackPanel.OrientationProperty, Orientation.Horizontal);
    factory.SetValue(Panel.IsItemsHostProperty, true);

    return new ItemsPanelTemplate { VisualTree = factory };
}
Mike Strobel
  • 25,075
  • 57
  • 69
  • If i could ask you one more thing...to insert more properties such as alignment and size to this component, how would it be? using this same schema XamlReader, im trying to understand... – Patroni Feb 06 '18 at 16:42
  • 1
    You should only be using `XamlReader` for something that _cannot_ be written equivalently in C#. Most Xaml code translates cleanly to C#, but templates are a special case. To change any other property, just change it directly on the instance, e.g., `listView.Width = 250` or `listView.HorizontalAlignment = HorizontalAlignment.Center`. – Mike Strobel Feb 06 '18 at 16:46
  • 1
    Sure, this make a lot of sense... I understand now, thanks again. – Patroni Feb 06 '18 at 16:48