我正在开发一些Windows手机应用程序。我有这个代码......
for (int b = 1; b < 7; b++){
for (int c = 0; c < 7; c++){
date = new TextBlock();
date .Margin = new Thickness(1);
date .TextAlignment = TextAlignment.Center;
date .FontSize = 15;
date .HorizontalAlignment = HorizontalAlignment.Stretch;
date .VerticalAlignment = VerticalAlignment.Center;
date .Tapped += date_Taped;
string middle= TIME.days[a].ToString();
if (middle== "0") { middle= ""; }
date.Text = middle;
a++;
Grid.SetRow(date , b);
Grid.SetColumn(date , c);
gridDatum.Children.Add(date );
}
}
我正在为网格添加值,最后我得到了我的colendar,这里的问题是我想在所有这些dinamicly创建的文本块之一TAP并导航自己到页面idk。 2015年6月24日。任何想法?
答案 0 :(得分:0)
如果我理解正确,您希望能够轻松识别与所点击的TextBlock
对应的日期。
您可以使用Tag
的{{1}}属性来存储标识值,并在TextBlock
事件处理程序中对其进行检查。
date_Taped
或者......您可以使用for (int b = 1; b < 7; b++)
{
for (int c = 0; c < 7; c++)
{
date = new TextBlock();
date .Margin = new Thickness(1);
date .TextAlignment = TextAlignment.Center;
date .FontSize = 15;
date .HorizontalAlignment = HorizontalAlignment.Stretch;
date .VerticalAlignment = VerticalAlignment.Center;
date .Tag = new int[] { a, b, c };
date .Tapped += date_Taped;
string middle= TIME.days[a].ToString();
if (middle== "0") { middle= ""; }
date.Text = middle;
a++;
Grid.SetRow(date , b);
Grid.SetColumn(date , c);
gridDatum.Children.Add(date );
}
}
private void date_Taped(object sender, TappedRoutedEventArgs e)
{
var textBlock = sender as TextBlock;
var a = (textBlock.Tag as int[])[0];
var b = (textBlock.Tag as int[])[1];
var c = (textBlock.Tag as int[])[2];
// Do stuff with a, b and c
// Do other stuff
}
为Action
事件定义动态生成的委托。
Tapped