Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/292.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# 如何使用wpf codebehind在listbox中隐藏单个listboxitem?_C#_Wpf_Forms_Visibility_Listboxitem - Fatal编程技术网

C# 如何使用wpf codebehind在listbox中隐藏单个listboxitem?

C# 如何使用wpf codebehind在listbox中隐藏单个listboxitem?,c#,wpf,forms,visibility,listboxitem,C#,Wpf,Forms,Visibility,Listboxitem,我正在创建一个保存注释的列表框。选择并双击注释后,将打开一个编辑表单。这里有一个选项可以归档注释。存档笔记时,它不应在原始表单上可见 我已经尝试了以下几种方法。我似乎找不到一个属性可以保存单个项目的可见性 listBox.SelectedItem = Visibility.Collapsed; listBox.SelectedItem.Visibility.Collapsed; 但是,它们不起作用。 任何建议都将不胜感激 尝试以下操作: ((ListBoxItem)listBox.Select

我正在创建一个保存注释的
列表框
。选择并双击注释后,将打开一个编辑表单。这里有一个选项可以归档注释。存档笔记时,它不应在原始表单上可见

我已经尝试了以下几种方法。我似乎找不到一个属性可以保存单个项目的可见性

listBox.SelectedItem = Visibility.Collapsed;
listBox.SelectedItem.Visibility.Collapsed;
但是,它们不起作用。 任何建议都将不胜感激

尝试以下操作:

((ListBoxItem)listBox.SelectedItem).Visibility = Visibility.Collapsed;
listBox.SelectedItem
将项目作为对象返回。您需要将其类型转换为ListBoxItem对象。它允许您访问ListBoxItem的所有不同属性

希望这对你有帮助/有用:)

*编辑*

堆栈溢出线程,应该有助于解释我所说的强制转换是什么意思。我还将尝试将该线程的答案与此问题联系起来

强制转换通常是告诉编译器,尽管它只知道某个值是某种常规类型,但实际上它是一种更为特定的类型。例如:


希望这有助于更详细地解释答案,还有大量的资源可以帮助解释C#中的类型转换和对象,远比

单击listboxitem时如何打开表单?您可以将listboxitem的实例传递给表单,然后在用户存档时告诉它折叠。第二个表单在双击listbox上的某个项目后打开。我如何将其转换为listboxitem?抱歉,我意识到我在回答中没有特别清楚地说明这一点,这是我第一次尝试在这里回答问题!我会修改我的答案,让它更清楚。下面的堆栈溢出问题应该有助于更详细地解释强制转换:感谢Ben的帮助!:)最后我修改了我的SQL代码,这样列表框就不会填充归档的笔记。啊,好吧,这也是一个好主意,至少你希望知道这一点,以备将来再次出现问题时参考:)
// As previously mentioned, SelectedItem returns an object
object x = listBox.SelectedItem;

// We know that x really refers to a ListBoxItem so we can cast it to that.
// Here, the (ListBoxItem) is casting x to a ListBoxItem. 
ListBoxItem y = (ListBoxItem)x;

//This allows us to call the different methods and properties of a listbox item:
y.Visibility = Visibility.Collapsed;

//In my original answer I combined these three lines into one