1

I am implementing a list box, when i select an item in list box it appears on a text block "MiniTextBlock" , but i want when text block text is changed manually or textblock text is not equal to selected item in list box then, that selected item should be deselected from list box.

DispatcherTimer timer = new DispatcherTimer { Interval = TimeSpan.FromSeconds(0.4) };
timer.Tick += delegate (object sender, object e)
{

    if(selectedItem != null && selectedItem.ToString() != MiniTextBlock.Text)
    {
        FavoritesListBox.SelectedIndex = -1;
    }
};
timer.Start();

every thing looks correct but it is deselect even if Textblock text and selected item is same.

Full Sample Codes

XAML

<StackPanel HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
        <TextBlock Name="MiniTextBlock" Text="35" FontSize="50" VerticalAlignment="Top" HorizontalAlignment="Center"/>

        <ListBox Name="FavoritesListBox" VerticalAlignment="Center">
            <ListBoxItem>
                <TextBlock Text="36" FontSize="30"/>
            </ListBoxItem>
            <ListBoxItem>
                <TextBlock Text="35" FontSize="30"/>
            </ListBoxItem>
            <ListBoxItem>
                <TextBlock Text="34" FontSize="30"/>
            </ListBoxItem>
        </ListBox>
    </StackPanel>

C#

public MainPage()
{
    this.InitializeComponent();
    DispatcherTimer timer = new DispatcherTimer { Interval = TimeSpan.FromSeconds(0.4) };
    timer.Tick += delegate (object sender, object e)
    {
        var selectedItem = FavoritesListBox.SelectedItem;

        if (selectedItem != null && selectedItem.ToString() != MiniTextBlock.Text)
        {
            FavoritesListBox.SelectedIndex = -1;
        }
    };
    timer.Start();
}

OUTPUT

Output

Arpit Jain
  • 107
  • 11
  • Probably the fact that you set the index, results in the fact that you trigger the event again, and that `SelectedItem` now is `null`. – Willem Van Onsem Oct 14 '17 at 14:17
  • I think on your event change the ddl value is getting cleared. Alternatively, you can check this before your condition. if(!string.IsNullorEmpty(FavoritesListBox.SelectedItem.ToString())){if(FavoritesListBox.SelectedItem.ToString() != MiniTextBLock.Text) { FavoritesListBox.SelectedIndex = -1; }} – Deepak Oct 14 '17 at 14:19
  • may be possible because i insert it in DispatcherTimer so it will check if itemchanged in text block or not, how can i deal with it – Arpit Jain Oct 14 '17 at 14:20
  • @Deepak checking ....... – Arpit Jain Oct 14 '17 at 14:21

2 Answers2

2

My guess is that you trigger the event again by setting SelectedIndex to -1, as a result SelectedItem is now null. Anyway, in that case a quick fix is to guard the if statement with a possible null:

var selectedItem = FavoritesListBox.SelectedItem;

if(selectedItem != null && selectedItem.ToString() != MiniTextBLock.Text)
{
    FavoritesListBox.SelectedIndex = -1;
}
Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555
2

Since you're not binding a source to your ListBox, the SelectedItem is actually a ListBoxItem, not a string. You'll need to drill down and find the actual text like this:

timer.Tick += delegate (object sender, object e)
{
    var selectedItem = (ListBoxItem)FavoritesListBox.SelectedItem;

    if (selectedItem == null)
    {
        return;
    }

    var tb = (TextBlock)selectedItem.Content;

    if (tb.Text != MiniTextBlock.Text)
    {
        FavoritesListBox.SelectedIndex = -1;
    }
};
Laith
  • 6,071
  • 1
  • 33
  • 60
  • In - var tb = (TextBlock)selectedItem.Content; , "Content" is not a available or not valid – Arpit Jain Oct 15 '17 at 07:49
  • Wow ! its working now , Thank's YoU ;) actually i will use it in list box which contain a text Textblock with text binding under a data template. i make above sample fo simplyfy my question let see it will work on that – Arpit Jain Oct 15 '17 at 07:57
  • Its through an exception if i used in my actual list box(Question Updated with actual text block) looks like because of i use item template > datatamplate > textblock can you look for this – Arpit Jain Oct 15 '17 at 08:08
  • okay i am updating more it is a storage which contain all items even it app is exiting actually i use this method http://bsubramanyamraju.blogspot.in/2014/01/how-to-store-listbox-items-into.html – Arpit Jain Oct 15 '17 at 08:28
  • Ok. Whatever your list is, you need to know that `SelectedItem` will be of that type. So if `ItemsSource = List`, `FavoritesListBox.SelectedItem` will be of type `string`. Got it? – Laith Oct 15 '17 at 09:24
  • yes my item is string so i convert SelectedItem.ToString but can you explain with one more update answer please :(.... old answer is work if i use directly listboxitem so dont remove it also – Arpit Jain Oct 15 '17 at 10:01
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/156767/discussion-between-arpit-jain-and-laith). – Arpit Jain Oct 15 '17 at 10:16
  • i asked seprated question for my new problem – Arpit Jain Oct 27 '17 at 07:40