[英]Deselect Selected item in List Box
我正在通過下面的代碼將“MiniTextBlock”文本添加到我的列表框,當我單擊該列表框項目時,它將顯示為“ShowTextBlock”並且所選項目在列表框中突出顯示,但如果“Show TextBlock”文本被更改,則所選項目仍然突出顯示,所以我希望它應該自動取消選擇。 為此,我正在使用此答案,但它僅在我直接通過 Xaml 添加列表框項時才有效,如果我使用模板綁定,則它不起作用。
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#
構造函數
IsolatedStorageFile Settings1 = IsolatedStorageFile.GetUserStoreForApplication();
MyDataList listobj = new MyDataList();
初始化時
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();
}
代碼
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);
}
}
}
您可以使用SelectedValue
屬性根據ShowTextBlock.Text
值選擇/取消選擇項目。 但是,我對您預期的數據流不是 100% 清楚。 如果您更詳細地描述哪些事件應該導致顯示什么樣的數據/選擇,我可以用更多詳細信息更新答案。
<ListBox x:Name="FavoritesListBox"
SelectedValuePath="MyLists"
SelectedValue="{Binding ElementName=ShowTextBlock,Path=Text,Mode=OneWay}">
如果要將所選項目與文本塊內容進行比較,請將所選項目轉換為您的數據類型並比較其文本屬性MyLists
if (selectedItem != null && ((MyData)selectedItem).MyLists != ShowTextBlock.Text)
{
// ...
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.