Skip to content

Instantly share code, notes, and snippets.

@reejk
Last active April 12, 2018 21:14
Show Gist options
  • Save reejk/5964207259a9345b98ccaf155df8bbf1 to your computer and use it in GitHub Desktop.
Save reejk/5964207259a9345b98ccaf155df8bbf1 to your computer and use it in GitHub Desktop.
AvaloniaUI: Resource in DataTemplate not found
<Window xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:AvaloniaBugTest;assembly=AvaloniaBugTest"
Title="AvaloniaBugTest" Width="500" Height="500">
<Window.Resources>
<local:SharedBrush x:Key="ResBackground" Value="White" />
<local:SharedBrush x:Key="ResForeground" Value="Blue" />
</Window.Resources>
<DockPanel>
<ListBox Width="200">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock
Text="{Binding Value}"
Background="{Binding Source={DynamicResource ResBackground},Path=Value}"
Foreground="{Binding Source={StaticResource ResForeground},Path=Value}" />
</DataTemplate>
</ListBox.ItemTemplate>
<ListBox.Items>
<local:ListItemViewModel Value="Item 1" />
<local:ListItemViewModel Value="Item 2" />
<local:ListItemViewModel Value="Item 3" />
</ListBox.Items>
</ListBox>
<StackPanel>
<DropDown SelectedItem="{Binding Source={StaticResource ResBackground},Path=Value,Mode=TwoWay}">
<DropDown.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Color}" Foreground="{Binding}" />
</DataTemplate>
</DropDown.ItemTemplate>
<DropDown.Items>
<SolidColorBrush Color="White" />
<SolidColorBrush Color="Black" />
<SolidColorBrush Color="Red" />
<SolidColorBrush Color="Blue" />
<SolidColorBrush Color="Green" />
</DropDown.Items>
</DropDown>
<DropDown SelectedItem="{Binding Source={StaticResource ResForeground},Path=Value,Mode=TwoWay}">
<DropDown.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Color}" Foreground="{Binding}" />
</DataTemplate>
</DropDown.ItemTemplate>
<DropDown.Items>
<SolidColorBrush Color="White" />
<SolidColorBrush Color="Black" />
<SolidColorBrush Color="Red" />
<SolidColorBrush Color="Blue" />
<SolidColorBrush Color="Green" />
</DropDown.Items>
</DropDown>
</StackPanel>
</DockPanel>
</Window>
using System.ComponentModel;
using System.Runtime.CompilerServices;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Diagnostics.ViewModels;
using Avalonia.Markup.Xaml;
using Avalonia.Markup.Xaml.Data;
using Avalonia.Media;
using AvaloniaBugTest.Annotations;
namespace AvaloniaBugTest
{
public class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
#if DEBUG
this.AttachDevTools();
#endif
}
private void InitializeComponent()
{
var b = new Binding();
AvaloniaXamlLoader.Load(this);
}
}
public class ListItemViewModel : ViewModelBase
{
private string _value;
public string Value
{
get { return _value; }
set { RaiseAndSetIfChanged(ref _value, value); }
}
}
public class SharedBrush : ViewModelBase
{
private IBrush _value;
public IBrush Value
{
get { return _value; }
set { RaiseAndSetIfChanged(ref _value, value); }
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment