C# 你知道为什么从类中返回的列表项不起作用吗?
首先,英语不是我的母语,标题可能很糟糕,所以我道歉,版主可以随意编辑标题 我在类receive中有方法createGrpButtons,它实例化类createGrpButtons,并使用返回的列表在表单上创建按钮。如果我在方法中使用相同的代码,它就会工作,但当我将代码移动到类中时,它就会停止工作。列表项仍然被传递(我可以在去bug时看到它们),但是按钮没有被创建C# 你知道为什么从类中返回的列表项不起作用吗?,c#,wpf,C#,Wpf,首先,英语不是我的母语,标题可能很糟糕,所以我道歉,版主可以随意编辑标题 我在类receive中有方法createGrpButtons,它实例化类createGrpButtons,并使用返回的列表在表单上创建按钮。如果我在方法中使用相同的代码,它就会工作,但当我将代码移动到类中时,它就会停止工作。列表项仍然被传递(我可以在去bug时看到它们),但是按钮没有被创建 public class TodoItem { public string Content
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;
}
}
我可以看到创建了一些项目,我可以看到项目属性,但表单上并没有创建按钮
然而,如果我这样做(方法中的所有内容),那么按钮就会出现在表单上
//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;
//计算容器的大小以确定按钮的数量
int btnMinimumHeightSize=40;
整数分隔Y=0//按钮之间的分隔
int btnNumberCreated=((Convert.ToInt16(stPanel.ActualHeight)/btnMinimumHeightSize));
列表BTN=新列表();
对于(int i=0;i
按钮通过以下方式绑定:
<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>
有人知道我在哪里搞砸了吗?即使
stPanelHeight
和stPanelWidth
值正确地提供给了方法,该方法仍然直接访问stPanel.ActualHeight
和stPanel.ActualWidth
即使将stPanelHeight
和stPanelWidth
值正确提供给该方法,该方法仍然直接访问stPanel.ActualHeight
和stPanel.ActualWidth
啊,这是一堆令人敬畏的代码!我认为你应该能够找到问题的确切位置,只询问你代码的特定部分。@marsze至少这不是其中一个按钮不起作用的原因这里是代码:在这里插入整个900行代码文件。我注意到的一件事是,即使你通过stPanelHeight
和stPanelWidth
对于该方法,您仍然可以直接在该方法内部访问stPanel.ActualHeight
和stPanel.ActualWidth
。原因只有一个:btnList.ItemSources在设置之后重置对不起。。。当一切都在时,方法按钮被绘制在表单上,当我从类(实例化)调用列表时,按钮没有被绘制,但返回的列表是相同的(至少在我看来是相同的)。没有错误,它只是不会绘制按钮。啊,这是一个可怕的代码!我认为你应该能够找到问题的确切位置,只询问你代码的特定部分。@marsze至少这不是其中一个按钮不起作用的原因这里是代码:在这里插入整个900行代码文件。我注意到的一件事是,即使你通过stPanelHeight
和stPanelWidth
对于该方法,您仍然可以直接在该方法内部访问stPanel.ActualHeight
和stPanel.ActualWidth
。原因只有一个:btnList.ItemSources在设置之后重置对不起。。。当一切都在时,方法按钮被绘制在表单上,当我从类(实例化)调用列表时,按钮没有被绘制,但返回的列表是相同的(至少在我看来是相同的)。没有错误,它只是不会绘制这些按钮。
<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>