问题描述
我有以下其中有一个网格视图代码的分成3列即可。
,而我有代码中的一个问题是。
当多个数据检索。
在3列中的所有数据是重叠的。
如何修改下面的代码,例如它会显示一个又一个的下面。
//定义表格列,大小
电网调度=新的Grid();
的foreach(在timeSplit VAR时间)
{
timeList =时间;
//第1列持有时间表
ColumnDefinition scheduleTimeColumn =新ColumnDefinition的时间();
GridLength timeGrid =新GridLength(110);
scheduleTimeColumn.Width = timeGrid;
schedule.ColumnDefinitions.Add(scheduleTimeColumn);
//文本块,显示时间表
的TextBlock timeTxtBlock =新TextBlock的时间();
timeTxtBlock.Text =时间;
//设置报警标签文本块属性 - 保证金,字号
timeTxtBlock.FontSize = 28;
timeTxtBlock.Margin =新厚度(0,20,0,0);
//设置将保持时间表
Grid.SetColumn的时间列(timeTxtBlock,0);
schedule.Children.Add(timeTxtBlock);
}
的foreach(在titleSplit VAR标题)
{
titleList =称号;
//第2栏保持时间表
ColumnDefinition scheduleTitleColumn的标题=新ColumnDefinition();
GridLength titleGrid =新GridLength(500);
scheduleTitleColumn.Width = titleGrid;
schedule.ColumnDefinitions.Add(scheduleTitleColumn);
//文本块,显示时间表
的TextBlock titleTxtBlock =新TextBlock的标题();
如果(title.Length→10)
{
串strTitle = title.Substring(0,10)+......;
titleTxtBlock.Text = strTitle;
}
,否则
{
titleTxtBlock.Text =称号;
}
//设置报警标签文本块属性 - 保证金,字号
titleTxtBlock.FontSize = 28;
titleTxtBlock.Margin =新厚度(60,20,0,0);
//设置将保持时间表
Grid.SetColumn的标题栏(titleTxtBlock,1);
schedule.Children.Add(titleTxtBlock);
//scheduleListBox.Items.Add(schedule);
}
的foreach(在categorySplit VAR类别)
{
所属分类=类别;
//第3栏保持时间表
ColumnDefinition categoryImageColumn的图片类别=新ColumnDefinition();
GridLength catImgnGrid =新GridLength(70);
categoryImageColumn.Width = catImgnGrid;
schedule.ColumnDefinitions.Add(categoryImageColumn);
的TextBlock categoryTxtBlock =新的TextBlock();
categoryTxtBlock.Text =类别;
//设置范畴图像及其属性 - 余量,宽度,高度,名称,背景,字体大小
图片categoryImage =新的图像();
categoryImage.Margin =新厚度(-50,15,0,0);
categoryImage.Width = 50;
categoryImage.Height = 50;
如果(类==优先级)
{
categoryImage.Source =新的BitmapImage(新的URI(/ AlarmClock的;组件/图片/ exclamination_mark.png,UriKind.Relative)) ;
}
,否则
如果(类==收藏)
{
categoryImage.Source =新的BitmapImage(新的URI(/ AlarmClock的;组件/图片/ star_full.png,UriKind.Relative));
}
Grid.SetColumn(categoryImage,2);
schedule.Children.Add(categoryImage);
}
scheduleListBox.Items.Add(时间表);
}
Quickhorn的回答大多是正确的。问题是你要创建一个大的电网,而不是对列表中的每个项目一行。这里是你的代码,它使用一个模型对象和数据绑定,而不是一个简单的例子。
这样,您就可以轻松风格的XAML一切,这使事情变得简单多了。
代码背后: MainPage.xaml.cs中
公共部分类的MainPage:的PhoneApplicationPage
{
私人的ObservableCollection<赛程> _schedules;
公众的MainPage()
{
的InitializeComponent();
的String [] = timeSplit新的字符串[] {One1,Two2,Three3};
的String [] = titleSplit新的字符串[] {标题1,标题2,TITLE3};
的String [] = categorySplit新的字符串[] {优先级,收藏,另一个};
_schedules =新的ObservableCollection<赛程>();
的for(int i = 0; I< timeSplit.Length;我++)
{
_schedules.Add(CreateSchedule(timeSplit [I],titleSplit [I],categorySplit [一世] ) );
}
scheduleListBox.ItemsSource = _schedules;
}
私人附表CreateSchedule(字符串时,字符串标题,串类)
{
计划时间表=新附表
{
时间=时间,
标题=标题,
类别=类别
};
如果(类==优先级)
{
schedule.ImageSource =/AlarmClock;component/Images/exclamination_mark.png;
}
,否则如果(类==收藏)
{
schedule.ImageSource =/AlarmClock;component/Images/star_full.png;
}
返回时间表;
}
}
公共类计划
{
公共字符串时间{获得;组; }
公共字符串名称{搞定;组; }
公共字符串等级{搞定;组; }
公共字符串的ImageSource {搞定;组; }
}
MainPage.xaml中
<列表框
X:NAME =scheduleListBox>
< ListBox.ItemTemplate>
<&DataTemplate的GT;
<网格和GT;
< Grid.ColumnDefinitions>
< ColumnDefinition WIDTH =自动/>
< ColumnDefinition WIDTH =自动/>
< ColumnDefinition WIDTH =自动/>
< /Grid.ColumnDefinitions>
< TextBlock的
文本={结合时间}/>
< TextBlock的
文本={结合标题}
Grid.Column =1/>
< StackPanel的
方向=横向
Grid.Column =2>
< TextBlock的
文本={绑定类别}/>
<图像
来源={结合的ImageSource}/>
< / StackPanel的>
< /网格和GT;
< / DataTemplate中>
< /ListBox.ItemTemplate>
< /列表框>
I have a grid view code below which have divided into 3 column.But i have a problem with the code is that.When multiple data is retrieved.All the data in the 3 column is overlapping.How can i modify the below code such as it will show one after another below it.
//Define grid column, size
Grid schedule = new Grid();
foreach (var time in timeSplit)
{
timeList = time;
//Column 1 to hold the time of the schedule
ColumnDefinition scheduleTimeColumn = new ColumnDefinition();
GridLength timeGrid = new GridLength(110);
scheduleTimeColumn.Width = timeGrid;
schedule.ColumnDefinitions.Add(scheduleTimeColumn);
//Text block that show the time of the schedule
TextBlock timeTxtBlock = new TextBlock();
timeTxtBlock.Text = time;
//Set the alarm label text block properties - margin, fontsize
timeTxtBlock.FontSize = 28;
timeTxtBlock.Margin = new Thickness(0, 20, 0, 0);
//Set the column that will hold the time of the schedule
Grid.SetColumn(timeTxtBlock, 0);
schedule.Children.Add(timeTxtBlock);
}
foreach (var title in titleSplit)
{
titleList = title;
//Column 2 to hold the title of the schedule
ColumnDefinition scheduleTitleColumn = new ColumnDefinition();
GridLength titleGrid = new GridLength(500);
scheduleTitleColumn.Width = titleGrid;
schedule.ColumnDefinitions.Add(scheduleTitleColumn);
//Text block that show the title of the schedule
TextBlock titleTxtBlock = new TextBlock();
if (title.Length > 10)
{
string strTitle = title.Substring(0, 10) + "....";
titleTxtBlock.Text = strTitle;
}
else
{
titleTxtBlock.Text = title;
}
//Set the alarm label text block properties - margin, fontsize
titleTxtBlock.FontSize = 28;
titleTxtBlock.Margin = new Thickness(60, 20, 0, 0);
//Set the column that will hold the title of the schedule
Grid.SetColumn(titleTxtBlock, 1);
schedule.Children.Add(titleTxtBlock);
//scheduleListBox.Items.Add(schedule);
}
foreach (var category in categorySplit)
{
categoryList = category;
//Column 3 to hold the image category of the schedule
ColumnDefinition categoryImageColumn = new ColumnDefinition();
GridLength catImgnGrid = new GridLength(70);
categoryImageColumn.Width = catImgnGrid;
schedule.ColumnDefinitions.Add(categoryImageColumn);
TextBlock categoryTxtBlock = new TextBlock();
categoryTxtBlock.Text = category;
//set the category image and its properties - margin, width, height, name, background, font size
Image categoryImage = new Image();
categoryImage.Margin = new Thickness(-50, 15, 0, 0);
categoryImage.Width = 50;
categoryImage.Height = 50;
if (category == "Priority")
{
categoryImage.Source = new BitmapImage(new Uri("/AlarmClock;component/Images/exclamination_mark.png", UriKind.Relative));
}
else
if (category == "Favourite")
{
categoryImage.Source = new BitmapImage(new Uri("/AlarmClock;component/Images/star_full.png", UriKind.Relative));
}
Grid.SetColumn(categoryImage, 2);
schedule.Children.Add(categoryImage);
}
scheduleListBox.Items.Add(schedule);
}
Quickhorn's answer is mostly right. The issue is you're creating one big grid instead of a row for each item in the list. Here's a simplified example of your code which uses a model object and databinding instead.
This way you can style everything in the xaml easily and it makes things much simpler.
Code Behind: MainPage.xaml.cs
public partial class MainPage : PhoneApplicationPage
{
private ObservableCollection<Schedule> _schedules;
public MainPage()
{
InitializeComponent();
string[] timeSplit = new string[] { "One1", "Two2", "Three3" };
string[] titleSplit = new string[] { "Title1", "Title2", "Title3" };
string[] categorySplit = new string[] { "Priority", "Favourite", "Another" };
_schedules = new ObservableCollection<Schedule>();
for ( int i = 0; i < timeSplit.Length; i++ )
{
_schedules.Add( CreateSchedule( timeSplit[i], titleSplit[i], categorySplit[i] ) );
}
scheduleListBox.ItemsSource = _schedules;
}
private Schedule CreateSchedule( string time, string title, string category )
{
Schedule schedule = new Schedule
{
Time = time,
Title = title,
Category = category
};
if ( category == "Priority" )
{
schedule.ImageSource = "/AlarmClock;component/Images/exclamination_mark.png";
}
else if ( category == "Favourite" )
{
schedule.ImageSource = "/AlarmClock;component/Images/star_full.png";
}
return schedule;
}
}
public class Schedule
{
public string Time { get; set; }
public string Title { get; set; }
public string Category { get; set; }
public string ImageSource { get; set; }
}
MainPage.xaml
<ListBox
x:Name="scheduleListBox">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<TextBlock
Text="{Binding Time}"/>
<TextBlock
Text="{Binding Title}"
Grid.Column="1"/>
<StackPanel
Orientation="Horizontal"
Grid.Column="2">
<TextBlock
Text="{Binding Category}"/>
<Image
Source="{Binding ImageSource}"/>
</StackPanel>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
这篇关于电网的Windows Phone 7的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!