如何使用WPF CodeBehind在列表框中隐藏单个ListBoxItem



我正在创建一个持有笔记的list box。选择注释并双击时,它将打开编辑表格。这里有一个可以存档注释的选项。当注释存档时,不应在原始形式上可见。

我尝试了几件事,可以在下面看到。我似乎找不到拥有单个项目可见性的属性。

listBox.SelectedItem = Visibility.Collapsed;
listBox.SelectedItem.Visibility.Collapsed;

但是它们不起作用。任何建议都会赞赏!

尝试以下内容:

((ListBoxItem)listBox.SelectedItem).Visibility = Visibility.Collapsed;

listBox.SelectedItem将项目作为对象返回。您需要将其键入将其输入到ListBoxItem对象中,它允许您访问ListBoxItem的所有不同属性。

希望这对您有帮助/有效:)

*编辑 *

在C#中打字的堆栈跨流线应有助于解释我的含义。我还将尝试将其从该线程与此问题相关联。

铸造通常是告诉编译器,尽管它只知道一个值是一般类型,但您知道它实际上是一种更具体的类型。例如:

// 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

希望这有助于更详细地解释答案,还有很多资源可以帮助解释c#中的类型铸造和对象远胜于!

最新更新