Bodul
Bodul

Reputation: 185

Do you have any idea why returned List items from Class not working?

First of all English is not my native language and title is probably horrible so I appologize, and moderators feel free to edit title if you want.

I have method createGrpButtons in class Receipt, which instantiate class CreateGrpButtons, and use returned List to create buttons on form. If I use same code inside method it works, but when i move code into the class it stop working. List items are still passed(i can see them while debbuging), but buttons are not created.

 public class TodoItem
        {
            public string Content { get; set; }
            public string Margin { get; set; }
            public string Tag { get; set; }
            public int Height { get; set; }
            public int Width { get; set; }

        }

          private void createGrpButtons()
        {

            int stPanelHeight = (Convert.ToInt16(stPanel.ActualHeight));
            int stPanelWidth = (Convert.ToInt16(stPanel.ActualWidth));

            GenerateGrpButtons btnGenGrp = new GenerateGrpButtons();
            btnList.ItemsSource = btnGenGrp.CreateGrpButtons(70, 0, stPanelHeight, stPanelWidth);

        }

And here is createGrpButton Class

class GenerateGrpButtons:frmReceipt 
    {

            public List<TodoItem> CreateGrpButtons( int btnMinimumHeightSize, int separationY, int pnlHeight, int pnlWidth)
        {
            //Calculate size of container to determine numbers of button
            //int btnMinimumHeightSize = 40;
            //int separationY = 0; //separation between buttons

            int btnNumberCreated = (pnlHeight / btnMinimumHeightSize);

            List<TodoItem> btns = new List<TodoItem>();

            for (int i = 0; i < btnNumberCreated; i++)
            {
                if (i == btnNumberCreated - 1)
                {
                    var HeightTmp = (Convert.ToDouble(stPanel.ActualHeight)) - (btnMinimumHeightSize * i);
                    btns.Add(new TodoItem() { Content = "ˇˇˇˇ", Height = Convert.ToInt16(HeightTmp), Width = Convert.ToInt16(stPanel.ActualWidth), Tag = "LastGrp", Margin = "0,0,0,0" });
                }
                else
                {
                    btns.Add(new TodoItem() { Content = "Group " + i, Height = btnMinimumHeightSize, Width = Convert.ToInt16(stPanel.ActualWidth), Tag = "Grp" + Convert.ToString(i), Margin = "1," + separationY + ",0,0" });
                }
            }

            return btns;
         }
    }

When I debug here:

 btnList.ItemsSource = btnGenGrp.CreateGrpButtons(70, 0, stPanelHeight, stPanelWidth);

I can see that there are some number of items created, and i can see item properties, but buttons are not created on form.

However if i do this (everything in method), then buttons appear on the form.

    //Calculate size of container to determine numbers of button
    int btnMinimumHeightSize = 40;
    int separationY = 0; //separation between buttons

    int btnNumberCreated = ((Convert.ToInt16(stPanel.ActualHeight) / btnMinimumHeightSize));

    List<TodoItem> btns = new List<TodoItem>();

    for (int i = 0; i < btnNumberCreated; i++)
    {
        if (i == btnNumberCreated - 1)
        {
            var HeightTmp = (Convert.ToDouble(stPanel.ActualHeight)) - (btnMinimumHeightSize * i);
            btns.Add(new TodoItem() { Content = "ˇˇˇˇ", Height = Convert.ToInt16(HeightTmp), Width = Convert.ToInt16(stPanel.ActualWidth), Tag = "LastGrp", Margin = "0,0,0,0" });
        }
        else
        {
            btns.Add(new TodoItem() { Content = "Group " + i, Height = btnMinimumHeightSize, Width = Convert.ToInt16(stPanel.ActualWidth), Tag = "Grp" + Convert.ToString(i), Margin = "1," + separationY + ",0,0" });
        }
    }
    btnList.ItemsSource = btns;

Buttons are binded through this:

 <StackPanel  Grid.Column="1" Grid.Row="1" Name="stPanel" HorizontalAlignment="Stretch" >
                <ItemsControl Name="btnList">
                    <ItemsControl.ItemTemplate>
                        <DataTemplate>
                            <Button Content ="{Binding Content}" Height="{Binding Height}" Width="{Binding Width}" Tag="{Binding Tag}" Margin="{Binding Margin}" HorizontalAlignment="Center"  Click="ClickHandlerGrp" />
                        </DataTemplate>
                    </ItemsControl.ItemTemplate>
                </ItemsControl>
            </StackPanel>

Anybody have a clue on where did I messed up?

Upvotes: 0

Views: 76

Answers (1)

C.Evenhuis
C.Evenhuis

Reputation: 26446

Even though the stPanelHeight and stPanelWidth values are correctly supplied to the method, the method still accesses stPanel.ActualHeight and stPanel.ActualWidth directly.

Upvotes: 2

Related Questions