Skip to content

Instantly share code, notes, and snippets.

@tluyben

tluyben/ListView.cs

Created Dec 12, 2019
Embed
What would you like to do?
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
You can’t perform that action at this time.