How to find if a Item in a ListBox has the focus?

Posted by eitan barazani on Stack Overflow See other posts from Stack Overflow or by eitan barazani
Published on 2013-10-31T21:09:37Z Indexed on 2013/10/31 21:55 UTC
Read the original article Hit count: 226

I have a List box defined like this:

<ListBox x:Name="EmailList"
     ItemsSource="{Binding MailBoxManager.Inbox.EmailList}"
     SelectedItem="{Binding SelectedMessage, Mode=TwoWay}"
     Grid.Row="1">

    <ListBox.ItemTemplate>
        <DataTemplate>
            <usrctrls:MessageSummary />
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

The UserControl is defined like this:

<UserControl x:Class="UserControls.MessageSummary"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"                          xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
         mc:Ignorable="d"
         d:DesignHeight="300"
         d:DesignWidth="600">

<UserControl.Resources>
</UserControl.Resources>

<Grid
      HorizontalAlignment="Left">
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="50" />
        <ColumnDefinition Width="*" />
    </Grid.ColumnDefinitions>

    <CheckBox Grid.Column="0"
              VerticalAlignment="Center" />

    <Grid Grid.Column="1" Margin="0,0,12,0">
        <Grid.RowDefinitions>
            <RowDefinition />
            <RowDefinition />
            <RowDefinition />
        </Grid.RowDefinitions>

        <Grid Grid.Row="0"
              Grid.Column="0"
              HorizontalAlignment="Stretch">

            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="30" />
                <ColumnDefinition Width="*" />
                <ColumnDefinition Width="80" />
                <ColumnDefinition Width="80" />
            </Grid.ColumnDefinitions>

            <Image x:Name="FlaggedImage"
                   Grid.Column="0"
                   Width="20"
                   Height="10"
                   Margin="0"
                   VerticalAlignment="Center"
                   HorizontalAlignment="Center"
                   Source="/Assets/ico_flagged_white.png" />

            <TextBlock x:Name="Sender"
                       Grid.Column="1"
                       Text="{Binding EmailProperties.DisplayFrom}"
                       Style="{StaticResource TextBlock_SenderRowTitle}"
                       HorizontalAlignment="Left" VerticalAlignment="Center" />

            <Grid x:Name="ImagesContainer"
                  Grid.Column="2" VerticalAlignment="Center">

                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition Width="*" />
                </Grid.ColumnDefinitions>

                <Image x:Name="ImgImportant"
                       Grid.Column="0"
                       Width="20"
                       Height="20"
                       VerticalAlignment="Center"
                       HorizontalAlignment="Center"
                       Source="ms-appx:///Assets/ico_important_red.png" />

                <Image x:Name="ImgFolders"
                       Grid.Column="1"
                       Width="20"
                       Height="20"
                       VerticalAlignment="Center"
                       HorizontalAlignment="Center"
                       Source="ms-appx:///Assets/ico_ico_addtofolder.png" />

                <Image x:Name="ImgAttachment"
                       Grid.Column="2"
                       Width="20"
                       Height="20"
                       VerticalAlignment="Center"
                       HorizontalAlignment="Center"
                       Source="ms-appx:///Assets/ico_attachment_lightgray.png" />

                <Image x:Name="ImgFlag"
                       Grid.Column="3"
                       Width="20"
                       Height="20"
                       VerticalAlignment="Center"
                       HorizontalAlignment="Center"
                       Source="ms-appx:///Assets/ico_flag.png" />
            </Grid>

            <TextBlock x:Name="Time"
                       Grid.Column="3"
                       Text="{Binding EmailProperties.DateReceived, Converter={StaticResource EmailHeaderTimeConverter}}"
                       TextAlignment="Center"
                       FontSize="16" VerticalAlignment="Center" Margin="0" />
        </Grid>

        <TextBlock Grid.Row="1"
                   Text="{Binding EmailProperties.Subject}"
                   TextTrimming="WordEllipsis"
                   Margin="0,10" />

        <TextBlock Grid.Row="2"
                   Text="{Binding EmailProperties.Preview}"
                   TextTrimming="WordEllipsis" />
    </Grid>

</Grid>

The MessageSummary is a UserControl. I would like to bind the foreground color of the Items of the ListBox to whether the item is the one selected in the list box, i.e. I would like the Item's foreground color to be Black if not selected and White if the item is selected.

How can it be done?

Thanks,

© Stack Overflow or respective owner

Related posts about c#

Related posts about xaml