Прежде всего, английский не является моим родным языком, и заголовок, вероятно, ужасен, поэтому я прошу прощения, и модераторы могут свободно редактировать заголовок, если хотите.
У меня есть метод createGrpButtons в классе Receipt, который создает экземпляр класса CreateGrpButtonsи используйте возвращенный список для создания кнопок на форме.Если я использую тот же код внутри метода, он работает, но когда я перемещаю код в класс, он перестает работать.Элементы списка по-прежнему передаются (я вижу их при отладке), но кнопки не создаются.
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);
}
А вот класс createGrpButton
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;
}
}
Когда я отлаживаю здесь:
btnList.ItemsSource = btnGenGrp.CreateGrpButtons(70, 0, stPanelHeight, stPanelWidth);
Я вижу, что создано некоторое количество элементов, и я вижу свойства элемента, но кнопки не создаются в форме.
Однако, если я делаю это (все в методе), затем в форме появляются кнопки.
//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;
Кнопки связаны между собой:
<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>
У кого-нибудь есть подсказка, где я все испортил?