Skip to content

Instantly share code, notes, and snippets.

@LennardF1989
Created January 23, 2016 00:08
Show Gist options
  • Save LennardF1989/59a42c7be474061f14bd to your computer and use it in GitHub Desktop.
Save LennardF1989/59a42c7be474061f14bd to your computer and use it in GitHub Desktop.
StackOverflow #1
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Windows;
namespace TestApplication
{
public interface INameable
{
string Name { get; set; }
}
public class BaseViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
public class Quest : BaseViewModel, INameable
{
private string _name;
public string Name
{
get { return _name; }
set
{
_name = value;
OnPropertyChanged();
}
}
public BindingList<Dialogue> Dialogues { get; set; }
public Quest()
{
Dialogues = new BindingList<Dialogue>();
Dialogues.ListChanged += (sender, args) =>
{
OnPropertyChanged(nameof(Dialogues));
};
}
}
public class Dialogue : BaseViewModel, INameable
{
private string _name;
public string Name
{
get { return _name; }
set
{
_name = value;
OnPropertyChanged();
}
}
}
public partial class MainWindow : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public INameable SelectedObject
{
get { return _selectedObject; }
set
{
_selectedObject = value;
OnPropertyChanged();
}
}
public BindingList<Quest> Quests
{
get { return _quests; }
set
{
_quests = value;
OnPropertyChanged();
}
}
private BindingList<Quest> _quests;
private INameable _selectedObject;
public MainWindow()
{
Quests = new BindingList<Quest>();
Quests.ListChanged += (sender, args) =>
{
OnPropertyChanged(nameof(Quests));
};
InitializeComponent();
_treeView.SelectedItemChanged += (sender, args) =>
{
if (args.NewValue is INameable)
{
SelectedObject = args.NewValue as INameable;
}
};
}
private void AddQuest(object sender, RoutedEventArgs e)
{
Quests.Add(new Quest
{
Name = "Quest"
});
}
private void AddDialogue(object sender, RoutedEventArgs e)
{
if (_treeView.SelectedItem is Quest)
{
(_treeView.SelectedItem as Quest).Dialogues.Add(new Dialogue
{
Name = "Dialogue"
});
}
}
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
}
<Window x:Class="TestApplication.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:testApplication="clr-namespace:TestApplication"
mc:Ignorable="d"
Title="MainWindow" Width="256" DataContext="{Binding RelativeSource={RelativeSource Self}}" SizeToContent="WidthAndHeight">
<DockPanel LastChildFill="True">
<StackPanel DockPanel.Dock="Top" Orientation="Horizontal">
<Button Content="Add Quest" Click="AddQuest" />
<Button Content="Add Dialogue" Click="AddDialogue" />
<TextBox Text="{Binding SelectedObject.Name, UpdateSourceTrigger=PropertyChanged}" MinWidth="100" />
</StackPanel>
<TreeView x:Name="_treeView" x:FieldModifier="private">
<TreeViewItem IsExpanded="True">
<TreeViewItem.Header>
<TextBlock Text="Project" Margin="5,0,0,0" />
</TreeViewItem.Header>
<TreeViewItem ItemsSource="{Binding Quests}" IsExpanded="True">
<TreeViewItem.Header>
<TextBlock Text="Quests" Margin="5,0,0,0" />
</TreeViewItem.Header>
<TreeViewItem.Resources>
<HierarchicalDataTemplate DataType="{x:Type testApplication:Quest}" ItemsSource="{Binding Dialogues}">
<TextBlock Text="{Binding Name}" Margin="5,0,0,0" />
</HierarchicalDataTemplate>
<DataTemplate DataType="{x:Type testApplication:Dialogue}">
<TextBlock Text="{Binding Name}" Margin="5,0,0,0" />
</DataTemplate>
</TreeViewItem.Resources>
</TreeViewItem>
</TreeViewItem>
</TreeView>
</DockPanel>
</Window>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment