Created
December 12, 2019 15:10
-
-
Save tluyben/f4f55807e056dae5e077752addc5fbd9 to your computer and use it in GitHub Desktop.
ListView with Checkbox - not working
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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