突出显示列表框中的选定项目

发布于 2025-01-08 03:56:53 字数 6199 浏览 1 评论 0原文

我正在开发一个 Silverlight 应用程序,它有一个列表

<ListBox x:Name="_list_collection"
    SelectionChanged="SelectionChanged"
    ScrollViewer.VerticalScrollBarVisibility="Auto"
    SelectedItem="{Binding SelectedItem, Mode=TwoWay}"
    ItemsSource="{Binding Collection, Mode=TwoWay}"
    ItemTemplate="{StaticResource ScheduleListItemDataTemplate}"
    ItemContainerStyle="{StaticResource ScheduleListItemContainerDataTemplate}" Margin="10" />

itemsource 绑定到此

 public ObservableCollection<ScheduleDto> Collection
 {
     get { return _collection; }
     set
     {
         _collection = value;
         OnPropertyChanged("Collection");
     }
 }

并且 selecteditem 绑定到

public ScheduleDto SelectedItem
{
    get { return _selectedItem; }
    set
    {
        _selectedItem = value;
        OnPropertyChanged("SelectedItem");
    }
}

正在选择该项目,因为我可以看到有关它的一些详细信息填充在单独的视图中。我唯一的问题是该项目没有突出显示(蓝色背景)。我已经尝试过添加一个选择更改事件处理程序,如下所示

private void SelectionChanged(object sender, SelectionChangedEventArgs e)
{
   ListBoxItem selectedItem = (ListBoxItem)_list_collection.ItemContainerGenerator.ContainerFromIndex(_list_collection.SelectedIndex);

    VisualStateManager.GoToState(selectedItem, "Selected", true); 
}

但没有运气......有什么建议吗?

编辑:

这是 ItemTemplate

<DataTemplate x:Key="ScheduleListItemDataTemplate">
    <Grid VerticalAlignment="Stretch"
          d:DesignHeight="100">
        <Grid.Resources>
            <Converters1:ScheduleStatusConverter x:Key="ScheduleStatusConverter"/>
            <Converters1:DateToStringConverter x:Key="DateToStringConverter"/>
        </Grid.Resources>
        <Grid.RowDefinitions>
            <RowDefinition Height="50" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="250" />
            <ColumnDefinition Width="Auto"/>
        </Grid.ColumnDefinitions>
        <TextBlock Grid.ColumnSpan="3"
                    HorizontalAlignment="Stretch"
                   Text="{Binding Name}"         
                    Foreground="{Binding Converter={StaticResource ScheduleStatusConverter}}"
                   FontFamily="{StaticResource LabelTextStyle}"
                   FontSize="19" TextTrimming="WordEllipsis" VerticalAlignment="Center"/>
        <Grid Grid.Row="1">
            <Grid.RowDefinitions>
                <RowDefinition  />
                <RowDefinition />
            </Grid.RowDefinitions>
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="{Binding Path=[lblCreatedBy], Source={StaticResource Translations}}" VerticalAlignment="Center"/>
                <TextBlock Text=":" Margin="2,0,2,0" VerticalAlignment="Center"/>
                <TextBlock TextWrapping="Wrap"
                   Text="{Binding CreatedBy}"
                   FontFamily="{StaticResource LabelTextStyle}"
                   FontSize="16"
                   VerticalAlignment="Center"
                   Grid.Column="2"
                   Margin="0"
                   Grid.Row="1"
                   HorizontalAlignment="Right">
                    <TextBlock.Foreground>
                        <SolidColorBrush Color="{StaticResource FactualTextStyle}" />
                    </TextBlock.Foreground>
                </TextBlock>
            </StackPanel>
            <StackPanel Orientation="Horizontal" Grid.Row="1">
                <TextBlock Text="{Binding Path=[lblCoachingViewGridCreatedDate], Source={StaticResource Translations}}" VerticalAlignment="Center"/>
                <TextBlock Text=":" Margin="2,0,2,0" VerticalAlignment="Center"/>
                <TextBlock TextWrapping="Wrap"
                   Text="{Binding CreatedAt,Converter={StaticResource DateToStringConverter}}"
                   FontSize="16"
                   VerticalAlignment="Center"

                   HorizontalAlignment="Right"/>
            </StackPanel>
        </Grid>
        <Grid Grid.Column="2"
                   Margin="0"
                   Grid.Row="1" Visibility="{Binding}">
            <Grid.RowDefinitions>
                <RowDefinition  />
                <RowDefinition />
            </Grid.RowDefinitions>
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="{Binding Path=[lblUpdatedBy], Source={StaticResource Translations}}" VerticalAlignment="Center"/>
                <TextBlock Text=":" Margin="2,0,2,0" VerticalAlignment="Center"/>
                <TextBlock TextWrapping="Wrap"
                   Text="{Binding UpdatedBy}"
                   FontFamily="{StaticResource LabelTextStyle}"
                   FontSize="16"
                   VerticalAlignment="Center"
                   Grid.Column="2"
                   Margin="0"
                   Grid.Row="1"
                   HorizontalAlignment="Right">
                    <TextBlock.Foreground>
                        <SolidColorBrush Color="{StaticResource FactualTextStyle}" />
                    </TextBlock.Foreground>
                </TextBlock>
            </StackPanel>
            <StackPanel Orientation="Horizontal" Grid.Row="1">
                <TextBlock Text="{Binding Path=[lblUpdatedDate], Source={StaticResource Translations}}" VerticalAlignment="Center"/>
                <TextBlock Text=":" Margin="2,0,2,0" VerticalAlignment="Center"/>
                <TextBlock TextWrapping="Wrap"
                   Text="{Binding UpdatedAt,Converter={StaticResource DateToStringConverter}}"
                   FontSize="16"
                   VerticalAlignment="Center"

                   HorizontalAlignment="Right"/>
            </StackPanel>
        </Grid>
    </Grid>
</DataTemplate>

即使我删除样式模板,也不会选择第一个项目:/

I am developing a Silverlight application which has a list

<ListBox x:Name="_list_collection"
    SelectionChanged="SelectionChanged"
    ScrollViewer.VerticalScrollBarVisibility="Auto"
    SelectedItem="{Binding SelectedItem, Mode=TwoWay}"
    ItemsSource="{Binding Collection, Mode=TwoWay}"
    ItemTemplate="{StaticResource ScheduleListItemDataTemplate}"
    ItemContainerStyle="{StaticResource ScheduleListItemContainerDataTemplate}" Margin="10" />

The itemsource is bound to this

 public ObservableCollection<ScheduleDto> Collection
 {
     get { return _collection; }
     set
     {
         _collection = value;
         OnPropertyChanged("Collection");
     }
 }

And the selecteditem is bound to

public ScheduleDto SelectedItem
{
    get { return _selectedItem; }
    set
    {
        _selectedItem = value;
        OnPropertyChanged("SelectedItem");
    }
}

The item is being selected because I can see some details about it populate in a separate view. My only problem is that this item is not highlighted (blue background). I have tried it by adding a selectiong changed event handler which looks like this

private void SelectionChanged(object sender, SelectionChangedEventArgs e)
{
   ListBoxItem selectedItem = (ListBoxItem)_list_collection.ItemContainerGenerator.ContainerFromIndex(_list_collection.SelectedIndex);

    VisualStateManager.GoToState(selectedItem, "Selected", true); 
}

But no luck... Any advice?

Edit:

This is the ItemTemplate

<DataTemplate x:Key="ScheduleListItemDataTemplate">
    <Grid VerticalAlignment="Stretch"
          d:DesignHeight="100">
        <Grid.Resources>
            <Converters1:ScheduleStatusConverter x:Key="ScheduleStatusConverter"/>
            <Converters1:DateToStringConverter x:Key="DateToStringConverter"/>
        </Grid.Resources>
        <Grid.RowDefinitions>
            <RowDefinition Height="50" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="250" />
            <ColumnDefinition Width="Auto"/>
        </Grid.ColumnDefinitions>
        <TextBlock Grid.ColumnSpan="3"
                    HorizontalAlignment="Stretch"
                   Text="{Binding Name}"         
                    Foreground="{Binding Converter={StaticResource ScheduleStatusConverter}}"
                   FontFamily="{StaticResource LabelTextStyle}"
                   FontSize="19" TextTrimming="WordEllipsis" VerticalAlignment="Center"/>
        <Grid Grid.Row="1">
            <Grid.RowDefinitions>
                <RowDefinition  />
                <RowDefinition />
            </Grid.RowDefinitions>
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="{Binding Path=[lblCreatedBy], Source={StaticResource Translations}}" VerticalAlignment="Center"/>
                <TextBlock Text=":" Margin="2,0,2,0" VerticalAlignment="Center"/>
                <TextBlock TextWrapping="Wrap"
                   Text="{Binding CreatedBy}"
                   FontFamily="{StaticResource LabelTextStyle}"
                   FontSize="16"
                   VerticalAlignment="Center"
                   Grid.Column="2"
                   Margin="0"
                   Grid.Row="1"
                   HorizontalAlignment="Right">
                    <TextBlock.Foreground>
                        <SolidColorBrush Color="{StaticResource FactualTextStyle}" />
                    </TextBlock.Foreground>
                </TextBlock>
            </StackPanel>
            <StackPanel Orientation="Horizontal" Grid.Row="1">
                <TextBlock Text="{Binding Path=[lblCoachingViewGridCreatedDate], Source={StaticResource Translations}}" VerticalAlignment="Center"/>
                <TextBlock Text=":" Margin="2,0,2,0" VerticalAlignment="Center"/>
                <TextBlock TextWrapping="Wrap"
                   Text="{Binding CreatedAt,Converter={StaticResource DateToStringConverter}}"
                   FontSize="16"
                   VerticalAlignment="Center"

                   HorizontalAlignment="Right"/>
            </StackPanel>
        </Grid>
        <Grid Grid.Column="2"
                   Margin="0"
                   Grid.Row="1" Visibility="{Binding}">
            <Grid.RowDefinitions>
                <RowDefinition  />
                <RowDefinition />
            </Grid.RowDefinitions>
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="{Binding Path=[lblUpdatedBy], Source={StaticResource Translations}}" VerticalAlignment="Center"/>
                <TextBlock Text=":" Margin="2,0,2,0" VerticalAlignment="Center"/>
                <TextBlock TextWrapping="Wrap"
                   Text="{Binding UpdatedBy}"
                   FontFamily="{StaticResource LabelTextStyle}"
                   FontSize="16"
                   VerticalAlignment="Center"
                   Grid.Column="2"
                   Margin="0"
                   Grid.Row="1"
                   HorizontalAlignment="Right">
                    <TextBlock.Foreground>
                        <SolidColorBrush Color="{StaticResource FactualTextStyle}" />
                    </TextBlock.Foreground>
                </TextBlock>
            </StackPanel>
            <StackPanel Orientation="Horizontal" Grid.Row="1">
                <TextBlock Text="{Binding Path=[lblUpdatedDate], Source={StaticResource Translations}}" VerticalAlignment="Center"/>
                <TextBlock Text=":" Margin="2,0,2,0" VerticalAlignment="Center"/>
                <TextBlock TextWrapping="Wrap"
                   Text="{Binding UpdatedAt,Converter={StaticResource DateToStringConverter}}"
                   FontSize="16"
                   VerticalAlignment="Center"

                   HorizontalAlignment="Right"/>
            </StackPanel>
        </Grid>
    </Grid>
</DataTemplate>

Even if I remove the style template the first item isn't selected :/

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

鹿! 2025-01-15 03:56:53

我找到了答案...感谢@Haris Hasan

有一个名为SelectedUnfocusedVisual stete。只需从 Selected 复制属性,一切正常

<VisualState x:Name="SelectedUnfocused">
   <Storyboard>
      <DoubleAnimation Duration="0"
          To=".75"
          Storyboard.TargetProperty="Opacity"
          Storyboard.TargetName="fillColor2" />
      <ColorAnimation Duration="0"
          To="#FF8DC5F9"
          Storyboard.TargetProperty="(Shape.Fill).(SolidColorBrush.Color)"
          Storyboard.TargetName="fillColor2"
          d:IsOptimized="True" />
      <ColorAnimation Duration="0"
          To="#FFBCC0C0"
          Storyboard.TargetProperty="(Shape.Fill).(SolidColorBrush.Color)"
          Storyboard.TargetName="fillColor"
          d:IsOptimized="True" />
       <DoubleAnimation Duration="0"
          To="1"
          Storyboard.TargetProperty="(Rectangle.RadiusX)"
          Storyboard.TargetName="fillColor2"
          d:IsOptimized="True" />
    </Storyboard>
</VisualState>

I found the answer... Thanks to @Haris Hasan

There is a Visual stete called SelectedUnfocused. Just copied the properties from Selected and everything works fine

<VisualState x:Name="SelectedUnfocused">
   <Storyboard>
      <DoubleAnimation Duration="0"
          To=".75"
          Storyboard.TargetProperty="Opacity"
          Storyboard.TargetName="fillColor2" />
      <ColorAnimation Duration="0"
          To="#FF8DC5F9"
          Storyboard.TargetProperty="(Shape.Fill).(SolidColorBrush.Color)"
          Storyboard.TargetName="fillColor2"
          d:IsOptimized="True" />
      <ColorAnimation Duration="0"
          To="#FFBCC0C0"
          Storyboard.TargetProperty="(Shape.Fill).(SolidColorBrush.Color)"
          Storyboard.TargetName="fillColor"
          d:IsOptimized="True" />
       <DoubleAnimation Duration="0"
          To="1"
          Storyboard.TargetProperty="(Rectangle.RadiusX)"
          Storyboard.TargetName="fillColor2"
          d:IsOptimized="True" />
    </Storyboard>
</VisualState>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文