<ListBox margin="0,355,70,205" name="WeekSummaryListBox" DataContext="{Binding}"> <ListBox.ItemTemplate> <DataTemplate> <StackPanel OrIEntation="Horizontal"> <TextBlock textwrapPing="Wrap" WIDth="150" Text="{Binding Amount}" Foreground="Orange"></TextBlock> <TextBlock textwrapPing="Wrap" WIDth="150" Text="{Binding Currency}" Foreground="Orange"></TextBlock> </StackPanel> </DataTemplate> </ListBox.ItemTemplate> <ListBox.Template> <ControlTemplate> <ScrollVIEwer HorizontalScrollbarVisibility="Visible"> <ItemsPresenter /> </ScrollVIEwer> </ControlTemplate> </ListBox.Template></ListBox>
在上面的列表框中,控件项目是垂直显示的.我想在列表框控件中显示项目.所以我使用以下代码.
<ListBox.ItemsPanel> <ItemsPanelTemplate> <StackPanel OrIEntation="Horizontal"/> </ItemsPanelTemplate></ListBox.ItemsPanel>
但是当我将两个文本块放在stackpanel中时,它会出错.为此,我使用以下代码
<StackPanel OrIEntation="Horizontal"> <TextBlock textwrapPing="Wrap" WIDth="150" Text="{Binding Amount}" Foreground="Orange"></TextBlock> <TextBlock textwrapPing="Wrap" WIDth="150" Text="{Binding Currency}" Foreground="Orange"></TextBlock> </StackPanel>
我正在尝试以下代码.在下面的代码中我收到错误
<ListBox margin="0,205" name="WeekSummaryListBox" DataContext="{Binding}"> <ListBox.ItemsPanel> <ItemsPanelTemplate> <StackPanel OrIEntation="Horizontal"> <TextBlock textwrapPing="Wrap" WIDth="150" Text="{Binding Amount}" Foreground="Orange"></TextBlock> <TextBlock textwrapPing="Wrap" WIDth="150" Text="{Binding Currency}" Foreground="Orange"></TextBlock> </StackPanel> </ItemsPanelTemplate> </ListBox.ItemsPanel> <ListBox.Template> <ControlTemplate> <ScrollVIEwer HorizontalScrollbarVisibility="Visible"> <ItemsPresenter /> </ScrollVIEwer> </ControlTemplate> </ListBox.Template></ListBox>
我收到错误“无法显式修改Panel的子集合,用作ItemsConnel的ItemsPanel.ItemsControl生成Panel的子元素”
我使用以下函数来显示ListBox中的数据:
public voID WeekSumValue(int TransactionType_ID){ UserInterfaceManager UserInterfaceManagerObj = new UserInterfaceManager(); List<UserInterfaceManager.TotalSummary> WeekSummary = new List<UserInterfaceManager.TotalSummary>(); ObservableCollection<AmountCurrency> WeekIncomeSumCollection = new ObservableCollection<AmountCurrency>(); WeekSummary = UserInterfaceManagerObj.LoaDWeekSum(Selectedbuttonname,TransactionType_ID,selectedDate); foreach (UserInterfaceManager.TotalSummary vWeekSummary in WeekSummary) { WeekIncomeSumCollection.Add(new AmountCurrency(vWeekSummary.Amount,vWeekSummary.Currency)); } if (WeekIncomeSumCollection.Count != 0 && SummaryComboBox.Selectedindex == 0) { WeekSummaryListBox.ItemsSource = WeekIncomeSumCollection; } else if (WeekIncomeSumCollection.Count != 0 && SummaryComboBox.Selectedindex == 2) { MonthSummaryListBox.ItemsSource = WeekIncomeSumCollection; } else { ObservableCollection<TextBlock> norecordsCollection = new ObservableCollection<TextBlock>(); TextBlock norecordsTextBlock = new TextBlock(); norecordsTextBlock.Text = "No record found"; norecordsTextBlock.FontSize = 25; norecordsTextBlock.Foreground = new SolIDcolorBrush(colors.Gray); norecordsCollection.Add(norecordsTextBlock); if (SummaryComboBox.Selectedindex == 0) WeekSummaryListBox.ItemsSource = norecordsCollection; if (SummaryComboBox.Selectedindex == 2) MonthSummaryListBox.ItemsSource = norecordsCollection; }}
在上面的函数中,数据是动态传递的.可以有两个,三个或更多记录也可以没有记录.我将此动态数据绑定到列表框内的文本块
我使用上面代码中使用的以下类
public class AmountCurrency{ public int Amount { get; set; } public String Currency { get; set; } public AmountCurrency(int Amount,String Currency) { this.Amount = Amount; this.Currency = Currency; }}
我应该如何将上面两个textbock放在列表框中,这将显示项目的范围?能否请您提供我可以解决上述问题的任何代码或链接?如果我做错了什么,请指导我.
解决方法 它看起来像你在模板的错误部分内有两个文本块.这两个文本块应该在ListBox.ItemTemplate中,而不是在ListBox.ItemsPanelTemplate中:
<ListBox ItemsSource="{Binding Items}"> <ListBox.ItemsPanel> <ItemsPanelTemplate> <StackPanel OrIEntation="Horizontal"></StackPanel> </ItemsPanelTemplate> </ListBox.ItemsPanel> <ListBox.ItemTemplate> <DataTemplate> <StackPanel margin="0,17" WIDth="300" OrIEntation="Horizontal"> <TextBlock textwrapPing="Wrap" WIDth="150" Text="{Binding Amount}" Foreground="Orange"></TextBlock> <TextBlock textwrapPing="Wrap" WIDth="150" Text="{Binding Currency}" Foreground="Orange"></TextBlock> </StackPanel> </DataTemplate> </ListBox.ItemTemplate></ListBox>
如果您想了解此控件的工作原理,请从working “basic” control which shows items开始,然后查看将ItemsPanelTemplate添加到该工作示例.
总结以上是内存溢出为你收集整理的silverlight – 如何在列表框控件中水平显示项目?全部内容,希望文章能够帮你解决silverlight – 如何在列表框控件中水平显示项目?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)