Skip to content

Instantly share code, notes, and snippets.

@tluyben
Created December 12, 2019 15:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tluyben/f4f55807e056dae5e077752addc5fbd9 to your computer and use it in GitHub Desktop.
Save tluyben/f4f55807e056dae5e077752addc5fbd9 to your computer and use it in GitHub Desktop.
ListView with Checkbox - not working
using Avalonia.Controls;
using Avalonia.Markup.Xaml;
using System.Collections.Generic;
using System.Linq;
using Avalonia.Data;
using Avalonia.Controls.Templates;
using Avalonia.Controls.Primitives;
using System;
namespace Playground
{
public class ListItem
{
public string Greet { get; set; }
public string Something { get; set; }
public bool? Done { get; set; }
public override string ToString()
{
return $"{Greet} {Something} = {Done}";
}
}
public class ListView : Window
{
public ListView()
{
InitializeComponent();
}
private void InitializeComponent()
{
AvaloniaXamlLoader.Load(this);
// Let's build up the content with normal model classes;
var modelList = new List<ListItem>()
{
new ListItem { Greet = "Hello", Something = "World", Done = false},
new ListItem { Greet = "Hi", Something = "Earth", Done = true },
new ListItem { Greet = "Howdy", Something = "Planet", Done = false }
};
// Create a List of Dictionaries from that:
var dictList = modelList.Select(i => i.GetType().GetProperties().ToDictionary(t => t.Name, t => t.GetValue(i))).ToList();
// Create a base panel
StackPanel mainStack = new StackPanel
{
Orientation = Orientation.Vertical
};
Content = mainStack;
// Create the list;
var listView = new ListBox
{
};
// now connect the items
listView.Items = dictList;
mainStack.Children.Add(listView);
// create a datatemplate:
var template = new FuncDataTemplate<Dictionary<string, object>>(x =>
{
var stack = new StackPanel
{
Orientation = Orientation.Horizontal
};
stack.Children.Add(new TextBlock
{
[!TextBlock.TextProperty] = new Binding("[Greet]")
}
);
stack.Children.Add(new TextBlock
{
[!TextBlock.TextProperty] = new Binding("[Something]")
}
);
stack.Children.Add(new CheckBox
{
[!ToggleButton.IsCheckedProperty] = new Binding("[Done]", BindingMode.TwoWay)
}
);
return stack;
});
// assign it
listView.ItemTemplate = template;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment