【问题标题】:list item selection-how to display a tick image on the selected item in the list box列表项选择 - 如何在列表框中的选定项上显示刻度图像
【发布时间】:2011-09-20 06:38:39
【问题描述】:

我有一个列表框,我必须在所选项目上显示一个勾号。我试过这个代码

列表框

 <ListBox Height="691" HorizontalAlignment="Left" Name="listBox1" Margin="-12,71,0,0" VerticalAlignment="Top" Width="480" SelectionChanged="listBox1_SelectionChanged">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <Border BorderThickness="0,1,0,0" BorderBrush="#FFC1BCBC" Width="490">
                        <Grid Height="70">
                            <TextBlock
                            Name="clients"
                            Margin="10,12,0,0"
                            Text="{Binding Name}" FontSize="24" FontWeight="SemiBold" Foreground="Black"></TextBlock>
                            <Image Height="30" Width="30"
                           HorizontalAlignment="Left" 
                           Name="imageTick" 
                           Stretch="Fill" 
                           VerticalAlignment="Center" 
                           Source="{Binding strAccountSelectedTickPath}"
                           Margin="380,0,0,0" Visibility="Collapsed"/>
                        </Grid>
                    </Border>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>

xaml.cs

 private void listBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {

        string clientId="";

        if (listBox_1.SelectedIndex >= 0)
        {



            (Application.Current as App).obj_subnodes = newlist[listBox1.SelectedIndex];
            if ((Application.Current as App).obj_subnodes.strAccountSelectedTickPath==""||(Application.Current as App).obj_subnodes.strAccountSelectedTickPath==null)
            {

                    if ( (Application.Current as App).obj_subnodes.strAccountSelectedTickPath = "")
                    {

                         (Application.Current as App).obj_subnodes.strAccountSelectedTickPath = "/sprinklr;component/Images/IsSelected.png";}
else{ (Application.Current as App).obj_subnodes.strAccountSelectedTickPath = ""; }
                initializeListBox();
            }
            NavigationService.Navigate(new Uri("/Home.xaml, UriKind.Relative));
        }
    }
 private void initializeListBox()
    {
        listBox1.ItemsSource = "";
        listBox1.ItemsSource = newlist;
    }

但问题是假设如果我在列表框中有两个项目说 item1 和 item2 并且第一次我选择了 item1 并且勾选标记显示在 item1 上,之后我选择了项目 2 并且勾选标记显示在 item2 上。但是item1 上的刻度线没有消失。我必须显示一个刻度线来指示所选项目,即我希望刻度图像显示在我单击的项目上。有什么解决方案吗?是否有任何选项可以控制访问列表框中的刻度图像。如果有一个选项,我可以使用 imagetick.visibility=visibility.collapsed。但我没有找到这样的选项。有什么解决方案请帮助我

【问题讨论】:

    标签: c# windows-phone-7 listbox


    【解决方案1】:

    实现这一点的最佳方法是更改​​ListBoxItem 的样式/模板,以便当它处于“选定”状态时,会显示一个刻度图像。

    您需要创建一个Style,为ListBoxItem 设置Template,在每个项目中添加您的刻度图像。有关如何执行此操作的详细信息,请参阅following blog post。您可以通过设置ItemContainerStyle 将此样式应用于您的ListBox

    要根据选择打开/关闭刻度线,您需要将VisualState 添加到您的ListBoxItem 模板中,如this blog post 中所述。

    【讨论】:

      【解决方案2】:

      下载最新的Silverlight Toolkit for Windows Phone(或通过 NuGet 安装)并使用MultiselectList 而不是常规的ListBox。 MultiselectList 支持为匹配邮件客户端的功能而设计的复选框。

      【讨论】:

        猜你喜欢
        • 2013-03-09
        • 2013-05-30
        • 2013-09-21
        • 2017-06-27
        • 2013-05-15
        • 1970-01-01
        • 2010-12-14
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多