Reputation: 107
i am adding "MiniTextBlock" Text to my list box through below code, and when i click on that list box item then it will show to "ShowTextBlock" and selected item is highlighted in listbox, but if "Show TextBlock" Text is changed then selected item is still highlighted so i want it should deselect automatically. For this purpose i am using this answer, but it only work when i add list box item directly through Xaml, if i am using template binding it is not working.
XAML
<ListBox x:Name="FavoritesListBox" VerticalAlignment="Center"
Background="Transparent" Height="150"
ScrollViewer.HorizontalScrollBarVisibility="Auto"
ScrollViewer.VerticalScrollBarVisibility="Disabled"
SelectionChanged="FavoritesListBox_SelectionChanged">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal" />
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Visibility="Visible" x:Name="FavoritesListBoxTextBlock"
FontSize="30" Text="{Binding MyLists}"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<Button Name="AddToFavoriteButton" Click="AddToFavoriteButton_Click" />
<TextBLock Name="MiniTextBlock" /> <!-- This will Contain diffrent texts -->
<TextBLock Name="ShowTextBlock" /> <!-- This will show list box selected item, but text can be change from other source so listbox selected item should deselect automatically -->
C#
Constructor
IsolatedStorageFile Settings1 = IsolatedStorageFile.GetUserStoreForApplication();
MyDataList listobj = new MyDataList();
On Initializing
public MainPage()
{
this.InitializeComponent();
//Populating ListBox items
if (Settings1.FileExists("MyStoreItems"))
{
using (IsolatedStorageFileStream fileStream = Settings1.OpenFile("MyStoreItems", FileMode.Open))
{
DataContractSerializer serializer = new DataContractSerializer(typeof(MyDataList));
listobj = (MyDataList)serializer.ReadObject(fileStream);
}
}
FavoritesListBox.ItemsSource = listobj;
//Checking whether selected item is equal to show textblock or not.
DispatcherTimer timer = new DispatcherTimer { Interval = TimeSpan.FromSeconds(0) };
timer.Tick += delegate (object sender, object e)
{
var selectedItem = FavoritesListBox.SelectedItem;
if (selectedItem != null && selectedItem.ToString() != ShowTextBlock.Text)
{
FavoritesListBox.SelectedIndex = -1; //but it deselect item even if selected selected item is equal to Show Text Block.
}
};
timer.Start();
}
Codes
private void AddToFavoriteButton_Click(object sender, RoutedEventArgs e)
{
listobj.Add(new MyData { MyLists = MiniTextBlock.Text });
//MiniTextBlock Which contains simple digit like 35 which will goto ListBox through this button
using (IsolatedStorageFileStream fileStream = Settings1.OpenFile("MyStoreItems", FileMode.Create))
{
DataContractSerializer serializer = new DataContractSerializer(typeof(MyDataList));
serializer.WriteObject(fileStream, listobj);
}
}
public class MyData
{
public string MyLists { get; set; }
}
public class MyDataList : ObservableCollection<MyData> //for storing mydata class items with type of list
{
}
//Selection Change for hint purpose
private void FavoriteListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
MyData selecteddata = (sender as ListBox).SelectedItem as MyData;
if (selecteddata != null)
{
ShowTextBlock.Text = selecteddata.MyLists.ToString());
using (IsolatedStorageFileStream fileStream = Settings1.OpenFile("MySelectedStoreItem", FileMode.Create))
{
DataContractSerializer serializer = new DataContractSerializer(typeof(MyData));
serializer.WriteObject(fileStream, selecteddata);
}
}
}
Upvotes: 1
Views: 1176
Reputation: 13448
You can use the SelectedValue
property to select / unselect items based on the ShowTextBlock.Text
value. However, I'm not 100% clear on your intended data flow. If you describe in more detail, which events should lead to what kind of displayed data / selection, I can update the answer with more details.
<ListBox x:Name="FavoritesListBox"
SelectedValuePath="MyLists"
SelectedValue="{Binding ElementName=ShowTextBlock,Path=Text,Mode=OneWay}">
If you want to compare the selected item to the textblock contents, cast the selected item to your datatype and compare its text property MyLists
if (selectedItem != null && ((MyData)selectedItem).MyLists != ShowTextBlock.Text)
{
// ...
}
Upvotes: 1