Skip to content

Instantly share code, notes, and snippets.

@JohanLarsson
Last active December 18, 2015 23:29
Show Gist options
  • Save JohanLarsson/5862308 to your computer and use it in GitHub Desktop.
Save JohanLarsson/5862308 to your computer and use it in GitHub Desktop.
Generates the Xaml if you want quick and dirty
[TestCase(typeof(Stats), "StatsProp")]
public void CreateListView(Type type, string bindingPopertyName)
{
PropertyInfo[] propertyInfos = type.GetProperties();
var listView = new XElement("ListView");
listView.Add(new XAttribute("ItemsSource", string.Format(@"{{Binding {0}}}", bindingPopertyName)));
var view = new XElement("ListView.View");
var gridView = new XElement("GridView");
foreach (var propertyInfo in propertyInfos)
{
var column = new XElement("GridViewColumn");
column.Add(new XAttribute("Header", propertyInfo.Name));
column.Add(new XAttribute("DisplayMemberBinding", string.Format(@"{{Binding {0}}}", propertyInfo.Name)));
gridView.Add(column);
}
view.Add(gridView);
listView.Add(view);
Console.Write(listView.ToString());
}
/// <summary>
/// Generates the Xaml for showing all properties of a class
/// </summary>
/// <param name="type"></param>
/// <param name="bindingPopertyName"></param>
[TestCase(typeof(Stats), "StatsProp")]
public void CreatePropertyGrid(Type type, string bindingPopertyName)
{
PropertyInfo[] propertyInfos = type.GetProperties();
var grid = new XElement("Grid");
grid.Add(new XAttribute("DataContext", string.Format(@"{{Binding {0}}}", bindingPopertyName)));
var rows = new XElement("Grid.RowDefinitions");
foreach (var propertyInfo in propertyInfos)
{
var rowDef = new XElement("RowDefinition");
rowDef.Add(new XAttribute("Height", @"Auto"));
rows.Add(rowDef);
}
grid.Add(rows);
var columns = new XElement("Grid.ColumnDefinitions");
var col1 = new XElement("ColumnDefinition");
col1.Add(new XAttribute("Width", "Auto"));
var col2 = new XElement("ColumnDefinition");
columns.Add(col1);
columns.Add(col2);
grid.Add(columns);
for (int index = 0; index < propertyInfos.Length; index++)
{
var propertyInfo = propertyInfos[index];
var label = new XElement("Label", propertyInfo.Name);
label.Add(new XAttribute("Grid.Row", index.ToString()));
label.Add(new XAttribute("Grid.Column", 0.ToString()));
grid.Add(label);
var textBox = new XElement("TextBox");
textBox.Add(new XAttribute("Grid.Row", index.ToString()));
textBox.Add(new XAttribute("Grid.Column", 1.ToString()));
textBox.Add(new XAttribute("Text", string.Format(@"{{Binding {0}}}", propertyInfo.Name)));
grid.Add(textBox);
}
Console.Write(grid.ToString());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment