Windows Phone 7中怎么查找DataTemplate中的控件?

ztwz 2012-01-07 02:57:18
我做这样一个程序,读取联系人列表,然后选中联系人短信群发!
现在程序是实现了,但是曲线救国呀,性能不得行!
先说我的问题吧。
我在LongListSelector控件有用DataTemplate布局联系人如下

<DataTemplate x:Key="ItemsTemplate">
<Grid Margin="0,0,0,20" >
<Grid.ColumnDefinitions>
<ColumnDefinition Width="90"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="60"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="0.3*"/>
</Grid.RowDefinitions>
<!-- <Image x:Name="imgHead" Source="/Images/head.jpg" Grid.Row="0" Grid.RowSpan="2" Width="90" Height="90"/>-->
<TextBlock x:Name="tbName" Grid.Row="0" Grid.Column="1" FontSize="38" TextAlignment="Left" VerticalAlignment="Center" TextWrapping="Wrap" Text="{Binding Name}" />
<TextBlock x:Name="tbPhone" Grid.Row="1" Grid.Column="1" FontSize="25" TextAlignment="Left" VerticalAlignment="Center" TextWrapping="Wrap" Text="{Binding Phone}" />
<CheckBox Grid.Row="0" Grid.Column="2" Grid.RowSpan="2" IsChecked="{Binding IsCheck, Mode=TwoWay}" x:Name="cbCheck"/>
</Grid>
</DataTemplate>


在后台代码中我用下面方法获取选中的CheckBox

string phones = string.Empty;
sb.Clear();


if (VisualTreeHelper.GetChildrenCount(lsAllContacts) > 0)
{
IEnumerable<Grid> AllGrids = FindChildren<Grid>(this.lsAllContacts);//得到所有的Grid
foreach (var grid in AllGrids.ToList())
{
object objTbPhone = grid.FindName("tbPhone");
if (objTbPhone != null)
{
object objCheck = grid.FindName("cbCheck");
object objName = grid.FindName("tbName");

if (objCheck != null)
{
if (((CheckBox)objCheck).IsChecked == true)
{
smsComposeTask.To += ((TextBlock)objTbPhone).Text;

sb.Append(((TextBlock)objName).Text + ";");
}
}
}

}
}

FindChildren方法

public IEnumerable<T> FindChildren<T>(DependencyObject parent) where T : class
{
var count = VisualTreeHelper.GetChildrenCount(parent);
if (count > 0)
{
for (var i = 0; i < count; i++)
{
var child = VisualTreeHelper.GetChild(parent, i);
var t = child as T;
if (t != null)
yield return t;

var children = FindChildren<T>(child);
foreach (var item in children)
yield return item;
}
}
}


用这种方法得不到所有的CheckBox,只能得到部份,不知道是为什么,程序中是递归了子控件的呀。
现在我只能从longListSelector的ItemsSource中去判断我选中了哪些,点击全选的时候明显感觉慢半拍。
请问有更好的全选联系人的方法吗?
...全文
216 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
fallincloud 2012-01-08
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 ztwz 的回复:]

楼上瞎胡说,继续顶!
[/Quote]
。。。你明白我在说什么么?

你真的明白什么是绑定么
ztwz 2012-01-08
  • 打赏
  • 举报
回复
楼上瞎胡说,继续顶!
ztwz 2012-01-08
  • 打赏
  • 举报
回复
你看我代码了没?你看好我模板里双向绑定好了!你这回答跟我问的一点边都不沾!
fallincloud 2012-01-07
  • 打赏
  • 举报
回复
设置数据源的ischeck的属性为true即可

<CheckBox Grid.Row="0" Grid.Column="2" Grid.RowSpan="2" IsChecked="{Binding IsCheck, Mode=TwoWay}" x:Name="cbCheck"/>
</Grid>


8,737

社区成员

发帖
与我相关
我的任务
社区描述
WPF/Silverlight相关讨论
社区管理员
  • WPF/Silverlight社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧