如何列表项选择添加到列表框 [英] How to add List item Selector to ListBox

查看:116
本文介绍了如何列表项选择添加到列表框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前的工作在Windows Phone 8应用程序,我有TextBlock的一个列表框与边框作为背景给它,当我在列表框中的项目单击没有任何迹象显示给用户,他作为所选项目如何选中的项目颜色添加到它?



,也是我需要选择ListBox中的,而且,任何项目选择的是它的背景颜色应更改多个项目。



下面是我的代码:

 <电网X:名称=ListBoxLayoutGrid.Row =2保证金=4,0,0,0> 
<列表框名称=列表框
Horizo​​ntalContentAlignment =拉伸
VerticalContentAlignment =拉伸
的SelectionChanged =TopicListboxSelectionChanged
ScrollViewer.VerticalScrollBarVisibility =已禁用 >
< ListBox.ItemTemplate>
<&DataTemplate的GT;
< StackPanel的名称=AnswerCellBack点击=AnswerCellBack_Tap保证金=0,0,0,4方向=横向>
< BORDER NAME =BORDERCOLOR背景=#FFF2F4F7>
< TextBlock的名称=文本
WIDTH =456
填充=10,20,10,20
TextAlignment =中心
文本={绑定路径=值}
风格={StaticResource的AnswerTextStyle}/>
< /边框>
< / StackPanel的>
< / DataTemplate中>
< /ListBox.ItemTemplate>
< /列表框>
< /网格和GT;


解决方案

您需要使用SelectionMode属性来启用多项选择列表框。
或可以从Windows Phone的工具包使用LongListMultiSelector。



要更改选定项的背景颜色,更改列表框的样式模板讲到这里,
http://msdn.microsoft.com /en-us/library/cc278062%28v=vs.95%29.aspx



查找在ListBoxItem中样式这一行,并将其更改为你的颜色

 <矩形X:NAME =fillColor2透明度=0补=#FFBADDE9IsHitTestVisible =假半径=1半径=1/> 

您可以在App.xaml中的Application.Resources标记下申报的风格。



还是喜欢在你的页面,这些2种方式。



第一种方式。

 <列表框名称=lstbx> 
< ListBox.Style>
//你的风格
< /ListBox.Style>
< /列表框>



二的方式。



声明的样式隶属于标签电话:PhoneApplicationPage.Resources,像这样

 <电话:的PhoneApplicationPage 
X:类=测试。测试
的xmlns =http://schemas.microsoft.com/winfx/2006/xaml/presentation
的xmlns:X =http://schemas.microsoft.com/winfx/2006 / XAML
的xmlns:手机=CLR的命名空间:Microsoft.P​​hone.Controls;装配= Microsoft.P​​hone
的xmlns:SHELL =CLR的命名空间:Microsoft.P​​hone.Shell;装配=微软.Phone
的xmlns:D =http://schemas.microsoft.com/expression/blend/2008
的xmlns:MC =http://schemas.openxmlformats.org/markup-compatibility / 2006年的
的FontFamily ={StaticResource的PhoneFontFamilyNormal}
字号={StaticResource的PhoneFontSizeNormal}
前景={StaticResource的PhoneForegroundBrush}
SupportedOrientations =纵向方向= 纵向
MC:可忽略=D
壳:SystemTray.IsVisible =FALSE>

<电话:PhoneApplicationPage.Resources>
<风格X:键=MyListStyle的TargetType =ListBoxItem的>
//你的风格
< /样式和GT;
< /电话:PhoneApplicationPage.Resources>

<列表框名称=list1的风格={StaticResource的MyListStyle}
// ...


I am currently working on Windows Phone 8 Application, i have a ListBox with TextBlock with border as a background to it, when i click on the item in the list box there is no indication to the user that he as selected that item, how to add selected item color to it?

And also i need to select multiple items in the ListBox, Which ever the item is selected its background color should be changed.

Below is my code:

<Grid x:Name="ListBoxLayout" Grid.Row="2" Margin="4,0,0,0">
    <ListBox Name="listBox" 
             HorizontalContentAlignment="Stretch" 
             VerticalContentAlignment="Stretch" 
             SelectionChanged="TopicListboxSelectionChanged"
             ScrollViewer.VerticalScrollBarVisibility="Disabled">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <StackPanel Name="AnswerCellBack" Tap="AnswerCellBack_Tap" Margin="0,0,0,4" Orientation="Horizontal">
                    <Border Name="borderColor" Background="#FFF2F4F7">
                        <TextBlock Name="text"
                               Width="456"
                               Padding="10,20,10,20"
                               TextAlignment="Center"
                               Text="{Binding Path=Value}"
                               Style="{StaticResource AnswerTextStyle}"/>
                    </Border>
                </StackPanel>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>              
</Grid>

解决方案

You need to use SelectionMode property to enable multiple selection in listbox. Or you can use LongListMultiSelector from Windows Phone toolkit.

To change the background color of selected item, change the style template of listbox as mentioned here, http://msdn.microsoft.com/en-us/library/cc278062%28v=vs.95%29.aspx.

Find this line in the ListBoxItem Style, and change it to your color

<Rectangle x:Name="fillColor2" Opacity="0" Fill="#FFBADDE9" IsHitTestVisible="False" RadiusX="1" RadiusY="1"/>

You can declare styles in App.xaml under the Application.Resources tag.

Or like these 2 ways in your page.

First Way.

<ListBox Name="lstbx">
<ListBox.Style>
    // Your Style
</ListBox.Style>
</ListBox>

Second way.

Declare the styles under the tag phone:PhoneApplicationPage.Resources, like this.

<phone:PhoneApplicationPage
x:Class="Test.Test"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
SupportedOrientations="Portrait" Orientation="Portrait"
mc:Ignorable="d"
shell:SystemTray.IsVisible="False">

<phone:PhoneApplicationPage.Resources>
    <Style x:Key="MyListStyle"  TargetType="ListBoxItem">
        //your style
    </Style>
</phone:PhoneApplicationPage.Resources>

<ListBox Name="list1" Style="{StaticResource MyListStyle}"
//....

这篇关于如何列表项选择添加到列表框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆