0

I'm using MaterialDesignInXamlToolkit and I actually had a similar issue yesterday which I was able to solve using this: ReSharper WPF error: "Cannot resolve symbol "MyVariable" due to unknown DataContext"

MainWindowViewModel.cs

public class MainWindowViewModel
{
    public MainWindowViewModel(ISnackbarMessageQueue snackbarMessageQueue)
    {
        if (snackbarMessageQueue == null) throw new ArgumentNullException(nameof(snackbarMessageQueue));

        DemoItems = new[]
        {
            new DemoItem("Home", new Home(),
                new []
                {
                    new DocumentationLink(DocumentationLinkType.Wiki, $"{ConfigurationManager.AppSettings["GitHub"]}/wiki", "WIKI"),
                    DocumentationLink.DemoPageLink<Home>()
                }

                ),

        };
    }

    public DemoItem[] DemoItems { get; }
}

DemoItem.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;

namespace WpfApp12.Domain
{
    public class DemoItem : INotifyPropertyChanged
    {
        private string _name;
        private object _content;
        private ScrollBarVisibility _horizontalScrollBarVisibilityRequirement;
        private ScrollBarVisibility _verticalScrollBarVisibilityRequirement;
        private Thickness _marginRequirement = new Thickness(16);

        public DemoItem(string name, object content, IEnumerable<DocumentationLink> documentation)
        {
            _name = name;
            Content = content;
            Documentation = documentation;
        }

        public string Name
        {
            get { return _name; }
            set { this.MutateVerbose(ref _name, value, RaisePropertyChanged()); }
        }

        public object Content
        {
            get { return _content; }
            set { this.MutateVerbose(ref _content, value, RaisePropertyChanged()); }
        }

        public ScrollBarVisibility HorizontalScrollBarVisibilityRequirement
        {
            get { return _horizontalScrollBarVisibilityRequirement; }
            set { this.MutateVerbose(ref _horizontalScrollBarVisibilityRequirement, value, RaisePropertyChanged()); }
        }

        public ScrollBarVisibility VerticalScrollBarVisibilityRequirement
        {
            get { return _verticalScrollBarVisibilityRequirement; }
            set { this.MutateVerbose(ref _verticalScrollBarVisibilityRequirement, value, RaisePropertyChanged()); }
        }

        public Thickness MarginRequirement
        {
            get { return _marginRequirement; }
            set { this.MutateVerbose(ref _marginRequirement, value, RaisePropertyChanged()); }
        }

        public IEnumerable<DocumentationLink> Documentation { get; }

        public event PropertyChangedEventHandler PropertyChanged;

        private Action<PropertyChangedEventArgs> RaisePropertyChanged()
        {
            return args => PropertyChanged?.Invoke(this, args);
        }
    }
}

XAML (error under Binding DemoItems):

 <ListBox x:Name="DemoItemsListBox" Margin="0 16 0 16" SelectedIndex="0"                         
                         ItemsSource="{Binding DemoItems}"
                         PreviewMouseLeftButtonUp="UIElement_OnPreviewMouseLeftButtonUp">


                    <ListBox.ItemTemplate>
                        <DataTemplate DataType="domain:DemoItem">
                            <TextBlock Text="{Binding Name}" Margin="32 0 32 0" />
                        </DataTemplate>
                    </ListBox.ItemTemplate>
                </ListBox>

I can compile but nothing ("Home") is showing in the ListBox. When I try to find the DataContext, there is no "MainWindowViewModel" object to link it to attach it with exactly?

Black Panther
  • 145
  • 1
  • 12

0 Answers0