1

Goal: I am trying to create a wrap panel that has children that are bound to an observable collection.

Current Expected Behavior: I expect to see 3 nested wrap panels that have an ellipse, a text block, a label and a checkbox.

Problem: My wrap panel and contents are not displayed at runtime. (Note: "Test" and "Test 2" Labels outside of the itemscontrol do display as expected.)

I have read this and it doesn't seem to solve my problem.

Code Behind

using MVVM_SandBox.Models;
using MVVM_SandBox.ViewModels;

namespace MVVM_SandBox
{
    public partial class MainWindow
    {     
        public MainViewModel VMMain = new MainViewModel();


        public MainWindow()
        {
            VMMain.SomeItemModelBlahs = new System.Collections.ObjectModel.ObservableCollection<ItemModelBlah>() { new ItemModelBlah() { Label = "blah0" }, new ItemModelBlah() { Label = "blah1", CoolStuff = true }, new ItemModelBlah() { Label = "blah2" } };
            InitializeComponent();


        }
    }
}

XAML

<Window x:Name="winMain" x:Class="MVVM_SandBox.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

                     xmlns:viewmodels="clr-namespace:MVVM_SandBox.ViewModels"
                     Title="MVVM SandBox" Height="600" Width="800" AllowDrop="True">
    <Window.DataContext>
        <viewmodels:MainViewModel></viewmodels:MainViewModel>
    </Window.DataContext>
    <StackPanel>
        <Label Width="Auto" Height="Auto">Test</Label>
        <ItemsControl ItemsSource="{Binding SomeItemModelBlahs}" Margin="10,10,10,10">
                <ItemsControl.ItemsPanel>
                    <ItemsPanelTemplate>
                        <WrapPanel x:Name="WrapPanelOfModelItems" Margin="10,10,10,10" Width="400" Height="200" IsItemsHost="True" MinWidth="200" MinHeight="200">
                        </WrapPanel>
                    </ItemsPanelTemplate>
                </ItemsControl.ItemsPanel>
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <StackPanel Width="100" Height="100" Margin="10">
                        <Ellipse Width="10" Height="10" Fill="Aqua"></Ellipse>
                        <TextBlock Margin="10" Text="{Binding Label}"></TextBlock>
                        <Label>kjasdkjalsdjklsad</Label>
                        <CheckBox IsChecked="{Binding CoolStuff}">
                            <Label>Hello</Label> 
                        </CheckBox>
                    </StackPanel>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
            </ItemsControl>
        <Label Height="Auto" Width="Auto">Test 2</Label>
    </StackPanel>

</Window>

View Model

using MVVM_SandBox.Models;
using System.Collections.ObjectModel;


namespace MVVM_SandBox.ViewModels
{
    //[ImplementPropertyChanged]
    public class MainViewModel
    {
        public ObservableCollection<ItemModelBlah> SomeItemModelBlahs { get; set; }

        public MainViewModel()
        {

        }
    }
}

Model

namespace MVVM_SandBox.Models
{
    public class ItemModelBlah
    {
        public string Label { get; set; }
        public bool CoolStuff { get; set; }
    }
}
Community
  • 1
  • 1
TheColonel26
  • 2,618
  • 7
  • 25
  • 50

1 Answers1

2

The code is creating two instances of MainViewModel: once in the code behind, and again in the XAML. The code behind instance has a non-null ObservableCollection, while the instance in the XAML is set as the DataContext.

I would suggest removing the viewmodels:MainViewModel from the XAML, and creating it solely in code:

public partial class MainWindow
{     
    public MainViewModel VMMain = new MainViewModel();

    public MainWindow()
    {
        DataContext = VWMain;
        InitializeComponent();
    }
}

Also, it's a better design to set up the ObservableCollection from within the view model itself:

class MainViewModel
{
    public ObservableCollection<ItemModelBlah> SomeItemModelBlahs { get; private set; }

    public MainViewModel()
    {
        SomeItemModelBlahs = new ObservableCollection<ItemModelBlah>()
        {
            new ItemModelBlah() { Label = "blah0" },
            new ItemModelBlah() { Label = "blah1", CoolStuff = true },
            new ItemModelBlah() { Label = "blah2" }
        };
    }
}
Andy
  • 30,088
  • 6
  • 78
  • 89