Windows phone 7 如何向列表框中的选定项添加复选标记

Windows phone 7 如何向列表框中的选定项添加复选标记,windows-phone-7,Windows Phone 7,我是windows phone 7的新手,我有一个疑问: 如何向动态创建的列表框中的选定项添加复选标记。如果用户单击列表框中的另一项,复选标记将其位置移动到选定项。怎么做?我的代码如下所示: XAML代码: <ListBox Height="669" HorizontalAlignment="Left" Name="listBox1" VerticalAlignment="Top" Width="479" Margin="1,-3.5,0,0" SelectionChanged="list

我是windows phone 7的新手,我有一个疑问: 如何向动态创建的列表框中的选定项添加复选标记。如果用户单击列表框中的另一项,复选标记将其位置移动到选定项。怎么做?我的代码如下所示: XAML代码:

<ListBox Height="669" HorizontalAlignment="Left" Name="listBox1" VerticalAlignment="Top" Width="479" Margin="1,-3.5,0,0" SelectionChanged="listBox1_SelectionChanged" Background="White">
            <ListBox.ItemTemplate>
                <DataTemplate >
                    <Border BorderThickness="0,0,0,1.2" BorderBrush="Black" Width="480" >
                    <StackPanel Background="White" Name="stackpanel1" Width="480" Orientation="Horizontal">
                        <TextBlock Text="{Binding name}"  Height="62" Width="390" FontSize="40" FontFamily="Arial" Foreground="Black" TextAlignment="Left" VerticalAlignment="Center" />
                    </StackPanel>
                    </Border>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>

请用一些代码指导我。谢谢。

为数据模板创建用户控件。在用户控件中,添加检查图像或荧光灯,并在用户点击相应项时切换其可见性。我也是这么做的。我希望有帮助

如下所示:

用户控件

                <Border BorderThickness="0,0,0,1.2" BorderBrush="Black" Width="480" >
                <StackPanel Background="White" Name="stackpanel1" Width="480" Orientation="Horizontal">
                    <Image x:Name="checkImg" Source="check" Visibility="Collapsed">
                    <TextBlock Text="{Binding name}"  Height="62" Width="390" FontSize="40" FontFamily="Arial" Foreground="Black" TextAlignment="Left" VerticalAlignment="Center" />
                </StackPanel>
                </Border>
selectionchaged
事件中,将项目提取为
ListBoxItem
,并切换其可见性


存储所选项目的旧项目,选择其他项目时,将旧项目的可见性设置为false,将新项目设置为true。

为数据模板创建用户控件。在用户控件中,添加检查图像或荧光灯,并在用户点击相应项时切换其可见性。我也是这么做的。我希望有帮助

如下所示:

用户控件

                <Border BorderThickness="0,0,0,1.2" BorderBrush="Black" Width="480" >
                <StackPanel Background="White" Name="stackpanel1" Width="480" Orientation="Horizontal">
                    <Image x:Name="checkImg" Source="check" Visibility="Collapsed">
                    <TextBlock Text="{Binding name}"  Height="62" Width="390" FontSize="40" FontFamily="Arial" Foreground="Black" TextAlignment="Left" VerticalAlignment="Center" />
                </StackPanel>
                </Border>
selectionchaged
事件中,将项目提取为
ListBoxItem
,并切换其可见性


存储所选项目的旧项目,当您选择其他项目时,将旧项目的可见性设置为false,将新项目设置为true。

纯粹基于XAML的解决方案如下:

特定于应用程序的代码如下所示:

<ListBox ItemsSource="{Binding}">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding}"/>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

并且列表框的样式将使用XAML术语(复制粘贴并将下面的'someIcon.png'更改为要使用的图标名称):


看得见的
更新1:

下面添加一个图片来说明我对构建操作的评论


一个纯粹基于XAML的解决方案是:

特定于应用程序的代码如下所示:

<ListBox ItemsSource="{Binding}">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding}"/>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

并且列表框的样式将使用XAML术语(复制粘贴并将下面的'someIcon.png'更改为要使用的图标名称):


看得见的
更新1:

下面添加一个图片来说明我对构建操作的评论


如何更改图像的可见性。我尝试了以下方法:ListBoxItem selectedItem=this.listBox1.ItemContainerGenerator.ContainerFromItem(this.listBox1.selectedItem)作为ListBoxItem;选择editem.Visibility=Visibility.Visible;但是,我看不见它。我在selectionChanged事件中编写了此代码。请帮助我…使用用户控件。将所选项目解析为usercontrol
UserControl uc=选择editem作为UserControl
然后您将能够访问
uc.checkImg.Visibility=Visibility.Collapsed。
为此,您应该创建
用户
<phone:PhoneApplicationPage.Resources>
    <Style TargetType="ListBoxItem">
        <Setter Property="Background" Value="Transparent"/>
        <Setter Property="BorderThickness" Value="0"/>
        <Setter Property="BorderBrush" Value="Transparent"/>
        <Setter Property="Padding" Value="0"/>
        <Setter Property="HorizontalContentAlignment" Value="Left"/>
        <Setter Property="VerticalContentAlignment" Value="Top"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="ListBoxItem">
                    <Border x:Name="LayoutRoot" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" HorizontalAlignment="{TemplateBinding HorizontalAlignment}" VerticalAlignment="{TemplateBinding VerticalAlignment}">
                        <VisualStateManager.VisualStateGroups>
                            <VisualStateGroup x:Name="CommonStates">
                                <VisualState x:Name="Normal"/>
                                <VisualState x:Name="MouseOver"/>
                                <VisualState x:Name="Disabled">
                                    <Storyboard>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="LayoutRoot">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource TransparentBrush}"/>
                                        </ObjectAnimationUsingKeyFrames>
                                        <DoubleAnimation Duration="0" To=".5" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="ContentContainer"/>
                                    </Storyboard>
                                </VisualState>
                            </VisualStateGroup>
                            <VisualStateGroup x:Name="SelectionStates">
                                <VisualState x:Name="Unselected"/>
                                <VisualState x:Name="Selected">
                                    <Storyboard>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="ContentContainer">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="Green"/>
                                        </ObjectAnimationUsingKeyFrames>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)" Storyboard.TargetName="SelectionIcon">
                                            <DiscreteObjectKeyFrame KeyTime="0">
                                                <DiscreteObjectKeyFrame.Value>
                                                    <Visibility>Visible</Visibility>
                                                </DiscreteObjectKeyFrame.Value>
                                            </DiscreteObjectKeyFrame>
                                        </ObjectAnimationUsingKeyFrames>
                                    </Storyboard>
                                </VisualState>
                            </VisualStateGroup>
                        </VisualStateManager.VisualStateGroups>
                    <StackPanel Orientation="Horizontal">
                        <Border Width="80" Height="80">
                            <Image Name="SelectionIcon" Source="someIcon.png" Visibility="Collapsed"/>
                        </Border>
                        <ContentControl x:Name="ContentContainer" ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" Foreground="{TemplateBinding Foreground}" HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}"/>
                    </StackPanel>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</phone:PhoneApplicationPage.Resources>