首先英语不是我的母语,而且标题可能太糟糕了,所以我表示歉意,主持人可以根据需要随意编辑标题。
我在Receipt类中有createGrpButtons方法,该方法实例化了CreateGrpButtons类,并使用返回的List在窗体上创建按钮。如果我在方法内部使用相同的代码,则可以使用,但是当我将代码移入类时,它将停止工作。列表项仍被传递(调试时可以看到它们),但未创建按钮。
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>
有人知道我在哪里搞砸了吗?
答案 0 :(得分:2)
即使stPanelHeight
和stPanelWidth
值已正确提供给方法,该方法仍直接访问stPanel.ActualHeight
和stPanel.ActualWidth
。