大佬教程收集整理的这篇文章主要介绍了silverlight – 如何在列表框控件中水平显示项目?,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
<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的子元素”
public void WeekSumValue(int transactionType_ID) { UserInterfaceManager UserInterfaceManagerObj = new UserInterfaceManager(); List<UserInterfaceManager.@R_418_10586@lSumMary> WeekSumMary = new List<UserInterfaceManager.@R_418_10586@lSumMary>(); ObservableCollection<amountCurrency> WeekIncomeSumCollection = new ObservableCollection<amountCurrency>(); WeekSumMary = UserInterfaceManagerObj.LoadWeekSum(SELEctedButtonName,transactionType_ID,SELEctedDatE); foreach (UserInterfaceManager.@R_418_10586@lSumMary 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>
如果您想了解此控件的工作原理,请从@L_262_62@开始,然后查看将ItemsPanelTemplate添加到该工作示例.
以上是大佬教程为你收集整理的silverlight – 如何在列表框控件中水平显示项目?全部内容,希望文章能够帮你解决silverlight – 如何在列表框控件中水平显示项目?所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。