Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to give a new location to a button in c#?

Tags:

c#

button

xaml

uwp

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?

like image 866
M.Kesküll Avatar asked Dec 04 '25 16:12

M.Kesküll


1 Answers

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>
like image 68
AVK Avatar answered Dec 06 '25 06:12

AVK