取消选择列表框

时间:2017-10-27 05:32:32

标签: c# uwp windows-10-universal

我通过下面的代码将“MiniTextBlock”文本添加到我的列表框中,当我点击该列表框项目时,它将显示为“ShowTextBlock”并且所选项目在列表框中突出显示,但是如果“显示TextBlock”文本更改然后所选项目仍然突出显示所以我希望它应该自动取消选择。 为此我使用这个answer,但只有当我通过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);
        }
    }
}

1 个答案:

答案 0 :(得分:1)

您可以使用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)
{
    // ...
}