ListBox 不突出显示生成的项目(itemsSource)适用于设计期间添加的 ListBoxItems [英] ListBox does not highlight generated items(itemsSource) works fine for ListBoxItems aded during design

查看:13
本文介绍了ListBox 不突出显示生成的项目(itemsSource)适用于设计期间添加的 ListBoxItems的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在弹出窗口中有一个列表框.它绑定到一个简单的字典.我还有一个 ItemContainerStyle 主题列表框亮点.如果在设计时添加 ListBoxItems,则选择样式有效,但在分配 ItemsSource 时相同的样式不起作用.为了排除故障,我将其剥离为准系统,但问题仍然存在.下面是我的代码,启动它,然后点击 ShowPopup 打开 Popup,首先你会看到在设计时添加的项目,如果你点击Add ItemsSource"它会显示运行时项目.您可以在设计时项中看到选择,而不是在运行时项中.(通过 itemssource 生成的项目).

I have a ListBox in a popup. It’s bound to a simple Dictionary. I also have a ItemContainerStyle to theme listbox highlights. If I add ListBoxItems at design time, the selection style works, but the same style does not work when I assign ItemsSource. To troubleshoot I stripped it to barebones, and problem persists. Below is the code I have, Launch it, and click on ShowPopup to open Popup, first you will see items added in design time, and if you click on "Add ItemsSource" it will show run time items. You can see the selection in design time items, and not in run time items. (items generated via itemssource).

有什么想法吗?我错过了什么?

Any ideas? What am I missing?

<phone:PhoneApplicationPage   
    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"  
    xmlns:controls="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls"    
    x:Class="ObservableListSample.MainPage"  
    mc:Ignorable="d" d:DesignWidth="728" d:DesignHeight="480"  
    FontFamily="{StaticResource PhoneFontFamilyNormal}"  
    FontSize="{StaticResource PhoneFontSizeNormal}"  
    Foreground="{StaticResource PhoneForegroundBrush}"  
    SupportedOrientations="PortraitOrLandscape" Orientation="Portrait"  
    shell:SystemTray.IsVisible="True"    
    xmlns:toolkit="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls.Toolkit">   

    <!--LayoutRoot is the root grid where all page content is placed-->  
    <Grid x:Name="LayoutRoot" Background="Transparent">   
        <!--ContentPanel - place additional content here-->  
        <Grid x:Name="ContentPanel" Margin="12,0,12,0">   
            <controls:Pivot Margin="8" Title="pivot">   
                <controls:PivotItem Margin="12" Header="Popup">   
                    <Grid Margin="12">   
                        <Button Content="Show Popup" Margin="1,0,0,0" VerticalAlignment="Bottom" Click="Button_Click"/>   
                    </Grid>  
                </controls:PivotItem>  
            </controls:Pivot>  
            <Popup x:Name="LocPopup"  
                   Margin="12,120,12,12">   
                <StackPanel Background="{StaticResource PhoneBackgroundBrush}"  
                            Height="610"  
                            Width="432">   
                    <Button   
                               Content="Add ItemsSource"  
                                Click="Button_Click"  
                               VerticalAlignment="Top" Margin="12" />  
                    <ListBox x:Name="LibraryLocations" Height="480">   
                        <ListBox.Resources>    <DataTemplate x:Key="DataTemplate1">   
                                <ListBoxItem Content="{Binding Value}" Tag="{Binding Key.Name}"/>   
                            </DataTemplate>  
                        </ListBox.Resources>  
                        <ListBox.ItemTemplate>  
                            <StaticResource ResourceKey="DataTemplate1"/>   
                        </ListBox.ItemTemplate>  
                        <ListBoxItem Content="ListBoxItem 1" />  
                        <ListBoxItem Content="ListBoxItem 2" />  
                        <ListBoxItem Content="ListBoxItem 3" />  
                        <ListBoxItem Content="ListBoxItem 4" />  
                        <ListBoxItem Content="ListBoxItem 5" />  
                        <ListBoxItem Content="ListBoxItem 6" />  
                    </ListBox>  
                </StackPanel>  
            </Popup>  
        </Grid>  
    </Grid>  
</phone:PhoneApplicationPage>  

C#

using System;   
using System.Collections.Generic;   
using System.Linq;   
using System.Net;   
using System.Windows;   
using System.Windows.Controls;   
using System.Windows.Documents;   
using System.Windows.Input;   
using System.Windows.Media;   
using System.Windows.Media.Animation;   
using System.Windows.Shapes;   
using Microsoft.Phone.Controls;   
using System.Collections.ObjectModel;   

namespace ObservableListSample   
{   

    public partial class MainPage : PhoneApplicationPage   
    {   
         // Constructor   
        public MainPage()   
        {   
            InitializeComponent();   
        }   

        private void Button_Click(object sender, RoutedEventArgs e)   
        {   
            if (!LocPopup.IsOpen)   
                LocPopup.IsOpen = true;   
            else  
            {   
                Dictionary<string, string> items = new Dictionary<string, string>();   
                for (int i = 0; i < 20; i++)   
                {   
                    items.Add(i.ToString(), "Item " + i.ToString());   
                }   
                if (LibraryLocations.ItemsSource == null)   
                    LibraryLocations.Items.Clear();   
                LibraryLocations.ItemsSource = items;   
            }   
        }   

        protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)   
        {   
            if (LocPopup.IsOpen)   
            {   
                LocPopup.IsOpen = false;   
                e.Cancel = true;   
            }   
            else  
            {   
                base.OnBackKeyPress(e);   
            }   
        }   

    }     

}  

推荐答案

我在您的示例中没有看到任何 ItemContainerStyle.你真的需要在 ListBox.ItemTemplate 中使用 ListBoxItem 吗?试试这个:

I don't see any ItemContainerStyle in your sample. Do you really need using ListBoxItem in the ListBox.ItemTemplate? Try this one instead:

<DataTemplate x:Key="DataTemplate1">
   <TextBlock Text="{Binding Value}" Tag="{Binding Key.Name}"/>
</DataTemplate>

这篇关于ListBox 不突出显示生成的项目(itemsSource)适用于设计期间添加的 ListBoxItems的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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