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