ARTICLE AD BOX
I am new to WPF and the MVVM format but I am trying to bind a listbox to a ObservableCollection<Item> Which Item is a custom type written within my ViewModel namespace. Within my View xaml I am able to bind the listbox's ItemSource to my "Items" property using: ItemSource="{Binding Items}" and it sees it. But for the DataTemplate property "DataType" I can not set it to "{x:Type viewmodel:Item}" as vs studio throws an error saying The name "Item" does not exist in the namespace "clr-namespace:SBCM.MVVM.ViewModel"
This is my PackManagerView.xaml
<UserControl x:Class="SBCM.MVVM.View.PackManagerView" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:local="clr-namespace:SBCM.MVVM.View" xmlns:core="clr-namespace:SBCM.Core" xmlns:viewmodel="clr-namespace:SBCM.MVVM.ViewModel" mc:Ignorable="d" d:DesignHeight="505" d:DesignWidth="700" Loaded="UserControl_Loaded" > <UserControl.DataContext> <viewmodel:PackManagerViewModel/> </UserControl.DataContext> ... <ListBox Background="#37306e" Grid.Row="1" Grid.ColumnSpan="2" Grid.RowSpan="5" Margin="10" Name="packs_Listbox" ItemsSource="{Binding Items}"> <ListBox.ItemTemplate> <DataTemplate DataType="{x:Type viewmodel:Item}"> </DataTemplate> </ListBox.ItemTemplate> </ListBox> ... </UserControl>and within my PackManagerViewModel Class here is what I have down
namespace SBCM.MVVM.ViewModel; class PackManagerViewModel { public ObservableCollection<Item> Items { get; } = new(); public PackManagerViewModel() { } ... } public class Item { public Item() { } public string IniLocation { get; set; } public string Title { get; set; } public string Id { get; set; } public Range IdRange { get; set; } public string ImageSrc { get; set; } }Using the mvvm format, all the views are in SBCM.MVVM.View namespace and the backend stuff that I'm trying to run is in SBCM.MVVM.ViewModel namespace. I have also tried doing the binding by throwing all the backend code into PackManagerView.xaml.cs but I can't even get it to bind to Items doing it that way. Like I said I am new to WPF and the MVVM format so I am at a loss here.
New contributor
Jacob Bender is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
2
Explore related questions
See similar questions with these tags.
