private void Hangman_OnLoaded()
{
const int btnSize = 25;
for (var i = 65; i <= 90; i++)
{
var btn = new Button { Content = (char)i };
btn.Width = btn.Height = btnSize;
var margin = btn.Margin;
margin.Left += btnSize + 100;
btn.Margin = margin;
GridMain.Children.Add(btn);
}
}
So, I have this code block and i want to create alphabet with ASCII. I have a Grid named GridMain. I want to display A-Z. When I debug, I see that buttons are created in one nest. What should I use to display these letters next to each other?
Since these are buttons, that has single character ( atleast based on your question ), The size of the items will always be the same. So i would suggest you to use GridView with DataTemplate to load ASCII Characters as Buttons.
change your Hangman_OnLoaded to below.
private void Hangman_OnLoaded()
{
List<char> bindingData = new List<char>();
for (var i = 65; i <= 90; i++)
{
bindingData.Add((char)i);
}
variableGrid.DataContext = bindingData;
}
Below will be your GridView.
<GridView x:Name="variableGrid" ItemsSource="{Binding }">
<GridView.ItemTemplate>
<DataTemplate>
<Button Content="{Binding ''}" />
</DataTemplate>
</GridView.ItemTemplate>
</GridView>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With