作为列表框的列表框项目应该在选择其他项目的项目时清除其项目的选择?

时间:2013-07-26 10:10:21

标签: c# wpf listbox

我在主列表中有ListBox作为ListboxItem。在ListBox的一个项目中,我选择了一个项目。当我从其他项目ListBox中选择了另一个项目时,应清除此选择。代码如下:

<ListBox x:Name="lst" Grid.Row="3" BorderThickness="0" ScrollViewer.HorizontalScrollBarVisibility="Hidden" ItemsSource="{Binding MessageCollection}"  ScrollViewer.CanContentScroll="False" >
    <ListBox.ItemTemplate>
        <DataTemplate>
            <StackPanel Orientation="Vertical"  Width="{Binding RelativeSource= {RelativeSource FindAncestor, AncestorType={x:Type Grid}},Path=ActualWidth}" HorizontalAlignment="Stretch">
                <Border  Background ="#ffffff" BorderBrush="#dddddd" BorderThickness="0,1,0,0" Height="30" >
                    <TextBlock Text="{Binding Date, StringFormat={}{0:dd MMMM yyyy}}" VerticalAlignment="Center" FontWeight="Bold" FontSize="12" FontFamily="Tahoma"  Foreground="#7a7a7a" HorizontalAlignment="Center" />
                </Border>

                    <ListBox x:Name="lstMsgs" HorizontalAlignment="Stretch" ScrollViewer.HorizontalScrollBarVisibility="Hidden"  HorizontalContentAlignment="Stretch"  ItemsSource="{Binding MessageList}" SelectedItem="{Binding DataContext.SelectedMessage,Mode=TwoWay,ElementName=me}" Background="Transparent" BorderThickness="0">
                        <ListBox.ItemTemplate>
                            <DataTemplate>
                                <Border x:Name="brdItem" BorderBrush="#dddddd" BorderThickness="0,1,0,0" >
                                    <Border.Background>
                                        <LinearGradientBrush  StartPoint="0,0" EndPoint="0,1">
                                            <GradientStop Color="#f1f1f1" Offset="0"/>
                                            <GradientStop Color="#efefef" Offset="1"/>
                                        </LinearGradientBrush>
                                    </Border.Background>

                                    <Grid Height="30" HorizontalAlignment="Stretch" VerticalAlignment="Center" ScrollViewer.HorizontalScrollBarVisibility="Hidden"  >
                                            <Grid.ColumnDefinitions>
                                                <ColumnDefinition Width="10" />
                                                <ColumnDefinition Width="30" />
                                                <ColumnDefinition Width="10" />
                                                <ColumnDefinition Width="120" />
                                                <ColumnDefinition Width="10" />
                                                <ColumnDefinition Width="*" />
                                                <ColumnDefinition Width="10" />
                                                <ColumnDefinition Width="100" />
                                                <ColumnDefinition Width="30" />
                                            </Grid.ColumnDefinitions>
                                            <Image x:Name="img" Grid.Column="1" Visibility="Collapsed"  HorizontalAlignment="Center" Source="Images\icon1.png" Stretch="None" Margin="10,0,0,0"  />
                                            <TextBlock Text="Ershad Sheikh" VerticalAlignment="Center" Foreground="#2b2a2a" FontSize="12" Grid.Column="3" />
                                            <TextBlock Text="{Binding GroupMessage}" HorizontalAlignment="Left" Foreground="#7a7a7a" FontSize="11"  Grid.Column="5" VerticalAlignment="Center" TextTrimming="CharacterEllipsis" />
                                            <TextBlock Text="{Binding Date, StringFormat={}{0: hh:mm:ss tt}}" FontWeight="Bold" FontSize="12" Foreground="#2378a0"  Grid.Column="7" VerticalAlignment="Center" HorizontalAlignment="Right" />
                                        </Grid>
                                    </Border>
                                    <DataTemplate.Triggers>
                                        <DataTrigger Binding="{Binding Path=IsReplyPresent}" Value="True">
                                            <Setter TargetName="img" Property="Visibility" Value="Visible" />
                                        </DataTrigger>
                                        <DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ListBoxItem}}, Path=IsSelected}" Value="True">
                                            <Setter TargetName="brdItem" Property="Background" Value="#e5e9eb" >                                                    
                                            </Setter>
                                        </DataTrigger>
                                    </DataTemplate.Triggers>
                                </DataTemplate>
                            </ListBox.ItemTemplate>

                            <ListBox.Resources>
                                <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" 
                     Color="#e5e9eb"/>
                                <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" 
                     Color="Transparent" />
                                <SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}" Color="Black"/>
                            </ListBox.Resources>
                        </ListBox>
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
            <ListBox.Resources>
                <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" 
                       Color="#e5e9eb"/>
                <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" 
                     Color="Transparent" />
            </ListBox.Resources>
        </ListBox>

1 个答案:

答案 0 :(得分:1)

只要null失去焦点,您就可以明确SelectedItem ListBox,即您必须处理LostFocus事件。

您可以将事件处理程序添加到ListBox并在后面的代码中处理事件。

XAML更改:

<ListBox x:Name="lstMsgs" HorizontalAlignment="Stretch" ScrollViewer.HorizontalScrollBarVisibility="Hidden"  HorizontalContentAlignment="Stretch"  ItemsSource="{Binding MessageList}" SelectedItem="{Binding DataContext.SelectedMessage,Mode=TwoWay,ElementName=me}" Background="Transparent" BorderThickness="0" LostFocus="lstMsgs_LostFocus">

代码背后:

    private void lstMsgs_LostFocus(object sender, RoutedEventArgs e)
    {
        ListBox listBox = sender as ListBox;
        if (listBox != null)
        {
            listBox.SelectedItem = null;
        }
    }

方法2:

使用DependencyProperty处理LostFocus事件可以获得相同的结果。

ListBoxExtension.cs

using System.Windows;
using System.Windows.Controls;

namespace WpfApplication1
{
    public static class ListBoxExtension
    {
        public static readonly DependencyProperty NullSelectedItemOnLostFocusProperty =
            DependencyProperty.RegisterAttached("NullSelectedItemOnLostFocus", typeof(bool), typeof(ListBoxExtension), new UIPropertyMetadata(false, OnNullSelectedItemOnLostFocusChanged));

        public static bool GetNullSelectedItemOnLostFocus(DependencyObject d)
        {
            return (bool)d.GetValue(NullSelectedItemOnLostFocusProperty);
        }

        public static void SetNullSelectedItemOnLostFocus(DependencyObject d, bool value)
        {
            d.SetValue(NullSelectedItemOnLostFocusProperty, value);
        }

        private static void OnNullSelectedItemOnLostFocusChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            bool nullSelectedItemOnLostFocus = (bool)e.NewValue;

            ListBox listBox = d as ListBox;
            if (listBox != null)
            {
                if (nullSelectedItemOnLostFocus)
                    listBox.LostFocus += NullSelectedItem;
                else
                    listBox.LostFocus -= NullSelectedItem;
            }
        }

        public static void NullSelectedItem(object sender, RoutedEventArgs e)
        {
            ListBox listBox = sender as ListBox;
            if (listBox != null)
            {
                listBox.SelectedItem = null;
            }
        }
    }
}

XAML更改1:添加存在ListBoxExtension类的命名空间 - xmlns:loc =“clr-namespace:WpfApplication1”

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:loc="clr-namespace:WpfApplication1"
        Title="MainWindow" Height="350" Width="525">

XAML更改2:更改DependencyProperty - loc:ListBoxExtension.NullSelectedItemOnLostFocus =“True”

<ListBox x:Name="lstMsgs" HorizontalAlignment="Stretch" ScrollViewer.HorizontalScrollBarVisibility="Hidden"  HorizontalContentAlignment="Stretch"  ItemsSource="{Binding MessageList}" SelectedItem="{Binding DataContext.SelectedMessage,Mode=TwoWay,ElementName=me}" Background="Transparent" BorderThickness="0" loc:ListBoxExtension.NullSelectedItemOnLostFocus="True">