Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/301.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 取消选择列表框中的选定项_C#_Uwp_Windows 10 Universal - Fatal编程技术网

C# 取消选择列表框中的选定项

C# 取消选择列表框中的选定项,c#,uwp,windows-10-universal,C#,Uwp,Windows 10 Universal,我正在通过下面的代码将“MiniTextBlock”文本添加到我的列表框中,当我单击该列表框项目时,它将显示为“ShowTextBlock”,并且所选项目在列表框中高亮显示,但如果“show TextBlock”文本被更改,则所选项目仍然高亮显示,因此我希望它应该自动取消选择。 为此,我正在使用它,但它只在我直接通过Xaml添加列表框项时起作用,如果我使用模板绑定,它就不起作用 XAML <ListBox x:Name="FavoritesListBox" VerticalAlignmen

我正在通过下面的代码将“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 -->
关于初始化

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);
        }
    }
}
private void AddToFavoriteButton\u单击(对象发送者,路由目标)
{
Add(newmydata{MyLists=MiniTextBlock.Text});
//MiniTextBlock包含简单的数字,如35,它将通过此按钮转到列表框
使用(IsolatedStorageFileStream fileStream=Settings1.OpenFile(“MyStoreItems”,FileMode.Create))
{
DataContractSerializer serializer=新的DataContractSerializer(typeof(MyDataList));
WriteObject(fileStream,listobj);
}
}
公共类MyData
{
公共字符串MyLists{get;set;}
}
公共类MyDataList:ObservableCollection//用于存储列表类型的mydata类项
{
}
//出于提示目的更改选择
private void FavoriteListBox_SelectionChanged(对象发送者,SelectionChangedEventArgs e)
{
MyData selecteddata=(发件人作为列表框)。选择EdItem作为MyData;
如果(selecteddata!=null)
{
ShowTextBlock.Text=selecteddata.MyLists.ToString();
使用(IsolatedStorageFileStream fileStream=Settings1.OpenFile(“MySelectedStoreItem”,FileMode.Create))
{
DataContractSerializer serializer=新的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}">

如果show text block text发生更改,则selected item(所选项目)不会取消选择,我遵循此示例将项目存储在列表框中,我正在使用time ticker检查所选项目是否等于show textblock(显示文本块),如您在本问题中所见,如果直接添加列表框项目,效果很好,但如果我在当前问题中使用模板绑定,则会出现异常。我不知道您具体做了什么,但我将
ShowTextBlock
更改为
TextBox
进行测试,当我向其中写入一些文本时,会根据我的输入选择/取消选择列表框项目。顺便问一下:如果你想在选择一个项目时更改
ShowTextBlock.Text
,你应该更改
Binding.Mode
-但这就是为什么我说你应该详细描述数据流(编辑你的问题而不是写评论)你检查了我之前的问题吗?@ArpitJain是的,但它(1)您的问题应该是独立的,因此请在您当前的问题中提供所有必要的信息(2)它是不同的,因为您不使用那边的
ItemsSource
,(3)它是不相关的,因为如果您专注于所需的结果,则可以使用更好的代码/xaml结构。您可以评论“但它通过异常”-请包括异常消息和堆栈跟踪。您可以在应该使用
((MyData)selectedItem)的地方使用
selectedItem.ToString()
。MyList
@grek40对不起,我的错误,我再次测试了它,它取消了对项目的选择,即使所选项目等于显示文本块。现在我把所有的东西都放在这个问题上,这样你们就可以很容易地理解Summary=一个按钮,它使用mydata类的独立存储来阻止文本到列表框。如果我在列表框中点击该项,它将显示在showtextblock上,但有一种情况是直接从OnLine更改showtextblock文本(与此问题无关),因此列表框中的选定项应再次取消选中
if(selectedItem!=null&&selectedItem.ToString()!=ShowTextBlock.Text)
不好,因为
ToString()
不返回
MyData.MyLists
@grek40 oh!我忘了那条评论它是有效的。。谢谢你:)这就是我犯的错误。您可以更新您的答案,以便我将其标记为答案
<ListBox x:Name="FavoritesListBox"
         SelectedValuePath="MyLists"
         SelectedValue="{Binding ElementName=ShowTextBlock,Path=Text,Mode=OneWay}">
if (selectedItem != null && ((MyData)selectedItem).MyLists != ShowTextBlock.Text)
{
    // ...
}