通过longlistselector循环获取项目模板中的复选框 [英] Loop through longlistselector to get checkbox in itemtemplate
问题描述
< phone:LongListSelector Background =Transparentx:Name =DocSummaries ItemTemplate ={StaticResource DataTemplate_Header}ItemsSource ={Binding DocumentHeaders}Margin =33,0,-5,0/>
以及该列表使用此itemtemplate:
< Viewbox x:Name =viewboxHeaderTag =0>
< StackPanel Orientation =Verticalx:Name =listStackpanelHeight =330Width =410Margin =0,10,0,8>
< Grid x:Name =HeaderGridMargin =0,0,0,0Height =325Width =410Background =#FF4A4A4A>
< Grid.RowDefinitions>
< RowDefinition Height =Auto/>
< RowDefinition Height =Auto/>
< RowDefinition Height =Auto/>
< RowDefinition Height =Auto/>
< RowDefinition Height =Auto/>
< RowDefinition Height =Auto/>
< /Grid.RowDefinitions>
< Grid.ColumnDefinitions>
< ColumnDefinition Width =Auto/>
< ColumnDefinition Width =*/>
< ColumnDefinition Width =150/>
< /Grid.ColumnDefinitions>
<! -
< / TextBlock>
< / TextBlock> - >
< CheckBox x:Name =chkSelectGrid.Row =0Grid.Column =0Grid.RowSpan =6VerticalAlignment =CenterWidth =AutoChecked =chkSelect_Checked未选中= chkSelect_Unchecked/>
< StackPanel Grid.Row =0Grid.Column =2Margin =15,0,0,5x:Name =DurationpanelBackground =#FF4A4A4AHorizontalAlignment =右方向=水平>
TextBlock TextWrapping = Wrap Margin =2FontSize =22x:Name =txtDurationText ={Binding Duration}Foreground ={Binding Urgency,Converter = {StaticResource converttocolor} }HorizontalAlignment =Right>< / TextBlock>
< / StackPanel>
TextBlock Grid.Row =1Grid.Column =1Tap =btnViewDetail_TapMargin =0Grid.ColumnSpan =2TextWrapping =WrapText = {Binding DocDesc}FontSize =30Foreground =#FFDFDFDF>
< / TextBlock>
< / TextBlock>
< / TextBlock>
TextBlock Grid.Row =4Grid.Column =1Margin =0,5,0,0Foreground =#FFDFDFDFFontSize =16Text = {Binding Info1Label}>< / TextBlock>
TextBlock Grid.Row =5Grid.Column =1Foreground =#FFDFDFDFFontSize =16Text ={Binding Info2Label}>< / TextBlock> ;
TextBlock Grid.Row =6Grid.Column =1Foreground =#FFDFDFDFFontSize =16Text ={Binding Info3Label}>< / TextBlock> ;
< / Grid>
< / StackPanel>
< / Viewbox>
现在我的问题是,我想获取longlistselector中item模板的复选框,如果用户点击一个复选框,其他复选框将被取消选中。
在下面的代码中,我得到了longlistselector,但现在我不知道如何获取复选框形式的itemtemplate。
foreach(LayoutRoot.Children中的var thisthing)
{
if(thisthing。 GetType()== typeof(Grid))
{
Grid myGrid =(thisthing as Grid);
foreach(myGrid.Children中的变量Mylist)
{
if(Mylist.GetType()== typeof(LongListSelector))
{
LongListSelector FoundList =(Mylist as LongListSelector);
$ b code $
如果有人可以帮助我,我将不胜感激。
使用Visual Studio 2012,c#Windows phone 8应用程序
虽然在代码隐藏中你可以像尝试过那样做,但是我建议你去寻找一个MVVM实现。
代码将更加清洁可维护。
这个想法是,您将创建一个 MainViewModel
其中包含 ChildViewModel
的列表。
每个 ChildViewModel
包含您在 ItemTemplate
中需要的所有信息,包括 boolean
属性, CheckBox
将被绑定。
将 LongListSelector.ItemsSource
绑定到 MainViewModel 的
ChildViewModels
/ code>。
通过这种方式,您可以轻松拦截 CheckBox
取消选中(通过布尔属性Setter),并将信息传递给其他ChildViewModels以供更改。
编辑,根据您的评论指示代码你可以创建一个函数来搜索 以下是函数: I have This Long list selector : and that list uses this itemtemplate : Now my problem is, i want to get the checkbox in that itemtemplate that is in the longlistselector, so that if the user clicks on a checkbox, the other checkboxes get unchecked. in the code below i get the longlistselector, but now i have no idea how to get the checkbox form the itemtemplate. if anyone can help me here i would appreciate it. Using Visual Studio 2012, c# Windows phone 8 Application While you can certainly do that in the code-behind like you tried, I advice you to look for an MVVM implementation.
The code will be much cleaner an maintainable. The idea is that you would create a That way, you can easily intercept the moment that a EDIT, based on your comment indicating that the code has to live in the code-behind: You could create a function that will search for Here is the function:
这篇关于通过longlistselector循环获取项目模板中的复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋! ChecBoxes
递归地借助于 VisualTreeHelper
类:
pre $ LongListSelector FoundList =( Mylist as LongListSelector);
SearchElement(FoundList);
var count = VisualTreeHelper.GetChildrenCount(targeted_control);
if(count> 0)
{
for(int i = 0; i< count; i ++)
{
var child = VisualTreeHelper.GetChild targeted_control,i);
if(child is CheckBox)//只搜索ChecBoxes
{
CheckBox targeted_element =(CheckBox)child;
// check / uncheck
}
else
{
SearchElement(child);
}
}
}
else
{
return;
}
}
<phone:LongListSelector Background="Transparent" x:Name="DocSummaries" ItemTemplate="{StaticResource DataTemplate_Header}" ItemsSource="{Binding DocumentHeaders}" Margin="33,0,-5,0"/>
<Viewbox x:Name="viewboxHeader" Tag="0">
<StackPanel Orientation="Vertical" x:Name="listStackpanel" Height="330" Width="410" Margin="0,10,0,8">
<Grid x:Name="HeaderGrid" Margin="0,0,0,0" Height="325" Width="410" Background="#FF4A4A4A" >
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="150"/>
</Grid.ColumnDefinitions>
<!--<TextBlock x:Name="txtduration" TextWrapping="Wrap" RenderTransformOrigin="0.5,0.5" FontSize="{StaticResource PhoneFontSizeNormal}" Text="{Binding Transaction.Duration}" Foreground="{Binding Transaction.Urgency, Converter={StaticResource converttocolor}}" TextAlignment="Left" Width="60" Height="28" Canvas.Left="-26" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="-18,0,0,25" >
</TextBlock>
<TextBlock x:Name="txtdays" TextWrapping="Wrap" Text="days" Foreground="{Binding Transaction.Urgency, Converter={StaticResource converttocolor}}" RenderTransformOrigin="0.5,0.5" FontSize="{StaticResource PhoneFontSizeSmall}" TextAlignment="Left" Width="45" Height="28" Canvas.Left="-26" HorizontalAlignment="Left" VerticalAlignment="Center" Margin="3,0,0,10" >
</TextBlock>-->
<CheckBox x:Name="chkSelect" Grid.Row="0" Grid.Column="0" Grid.RowSpan="6" VerticalAlignment="Center" Width="Auto" Checked="chkSelect_Checked" Unchecked="chkSelect_Unchecked"/>
<StackPanel Grid.Row="0" Grid.Column="2" Margin="15,0,0,5" x:Name="Durationpanel" Background="#FF4A4A4A" HorizontalAlignment="Right" Orientation="Horizontal">
<TextBlock TextWrapping="Wrap" Margin="2" FontSize="22" x:Name="txtDuration" Text="{Binding Duration}" Foreground="{Binding Urgency, Converter={StaticResource converttocolor}}" HorizontalAlignment="Right"></TextBlock>
<TextBlock TextWrapping="Wrap" Margin="2,4,2,2" FontSize="22" Foreground="{Binding Urgency, Converter={StaticResource converttocolor}}" Text="days" HorizontalAlignment="Right"></TextBlock>
</StackPanel>
<TextBlock Grid.Row="1" Grid.Column="1" Tap="btnViewDetail_Tap" Margin="0" Grid.ColumnSpan="2" TextWrapping="Wrap" Text="{Binding DocDesc}" FontSize="30" Foreground="#FFDFDFDF" >
</TextBlock>
<TextBlock TextWrapping="Wrap" Margin="0" Grid.Row="2" Grid.ColumnSpan="2" Grid.Column="1" Text="{Binding Created}" FontSize="16" Foreground="#FFDFDFDF">
</TextBlock>
<TextBlock TextWrapping="Wrap" Grid.Row="3" Grid.ColumnSpan="2" Grid.Column="1" Margin="0" Text="{Binding Actioned}" Foreground="#FFDFDFDF" FontSize="16">
</TextBlock>
<TextBlock Grid.Row="4" Grid.Column="1" Margin="0,5,0,0" Foreground="#FFDFDFDF" FontSize="16" Text="{Binding Info1Label}"></TextBlock>
<TextBox Grid.Row="4" HorizontalAlignment="Stretch" Grid.Column="2" Height="55" FontSize="16" Margin="0,0,0,-15" Text="{Binding Info1Value}"></TextBox>
<TextBlock Grid.Row="5" Grid.Column="1" Foreground="#FFDFDFDF" FontSize="16" Text="{Binding Info2Label}" ></TextBlock>
<TextBox Grid.Row="5" HorizontalAlignment="Stretch" Grid.Column="2" Height="55" FontSize="16" Margin="0,0,0,-15" Text="{Binding Info2Value}"></TextBox>
<TextBlock Grid.Row="6" Grid.Column="1" Foreground="#FFDFDFDF" FontSize="16" Text="{Binding Info3Label}"></TextBlock>
<TextBox Grid.Row="6" HorizontalAlignment="Stretch" Grid.Column="2" Height="55" FontSize="16" Margin="0,0,0,-15" Text="{Binding Info3Value}"></TextBox>
</Grid>
</StackPanel>
</Viewbox>
foreach (var thisthing in LayoutRoot.Children)
{
if (thisthing.GetType() == typeof(Grid))
{
Grid myGrid = (thisthing as Grid);
foreach (var Mylist in myGrid.Children)
{
if (Mylist.GetType() == typeof(LongListSelector))
{
LongListSelector FoundList = (Mylist as LongListSelector);
}
}
}
}
MainViewModel
that contains a List of ChildViewModel
.
Each ChildViewModel
contain all the information you need in the ItemTemplate
, including a boolean
property that the CheckBox
will be bound on.
You bind the LongListSelector.ItemsSource
to the ChildViewModels
property of the MainViewModel
.CheckBox
is checked/unchecked (via the boolean property Setter) and pass the info to the other ChildViewModels for them to change.ChecBoxes
recursively with the help of the VisualTreeHelper
class:LongListSelector FoundList = (Mylist as LongListSelector);
SearchElement(FoundList);
private void SearchElement(DependencyObject targeted_control)
{
var count = VisualTreeHelper.GetChildrenCount(targeted_control);
if (count > 0)
{
for (int i = 0; i < count; i++)
{
var child = VisualTreeHelper.GetChild(targeted_control, i);
if (child is CheckBox) // Only search for ChecBoxes
{
CheckBox targeted_element = (CheckBox)child;
// check/uncheck
}
else
{
SearchElement(child);
}
}
}
else
{
return;
}
}