C# WPF设置listbox中项目的可见性-代码隐藏
我有一个列表框(添加在代码隐藏中),我想在Items.Count>4时设置最后一个项目的可见性。我有一个满是按钮的列表框C# WPF设置listbox中项目的可见性-代码隐藏,c#,wpf,button,listbox,C#,Wpf,Button,Listbox,我有一个列表框(添加在代码隐藏中),我想在Items.Count>4时设置最后一个项目的可见性。我有一个满是按钮的列表框 private void movingOfButtons(ListBox list, int index) { Control previousButton = (Control)list.Items.GetItemAt(list.Items.Count - 1); int itemCounts = ktery.Items.Count;
private void movingOfButtons(ListBox list, int index)
{
Control previousButton = (Control)list.Items.GetItemAt(list.Items.Count - 1);
int itemCounts = ktery.Items.Count;
indexButton = index;
// create new buttons
addNewButton("Sound");
addNewButton("addSound");
mainCycle(list);
// this does not work - it show only messageBox
if (itemCounts >= 4)
{
MessageBox.Show("" + previousButton.Name);
previousButton.Visibility = Visibility.Collapsed;
}
(list).Items.RemoveAt(index);
}
此previous按钮。可见性不起作用
错误在哪里?我发现了错误:
我必须“刷新”列表框中的项目…)
尝试Visibility.hidden调用MessageBox后,您必须单击“确定”才能继续下一步。Show else UI线程将等待,您将永远不会设置可见性:):@devhedgehog这不是解决方案。。。当我删除该MessageBox时,它也不起作用。:)值得一猜,呵呵。我怎么知道你在干什么?你没有发布太多关于你到底在做什么的信息。给我们看更多的代码。previousButton是否为空?你有例外吗?看看您的输出窗口,我们都知道mainCycle在做什么?这个问题将如何帮助那些偶然发现这个具有类似可见性问题的线程的人?Idk。。很高兴你解决了这个问题。@devhedgehog我只发布了重要的代码。但ok:addNewButton使用名称、索引创建新按钮,此方法将该按钮放入全局列表。。mainCycle只是将这些按钮从列表放到列表框。。。这就是全部。。。但我认为这并不重要因此,每次添加按钮时,您都会通过执行类似于ListBox.ItemsSource=myList的操作来重置ListBox?那太糟糕了。如果丢失SelectedItem,则会丢失滚动位置。这就像把椅子从一个要坐的人身上拿开一样,哈哈。这很卑鄙。你有没有想过使用ObservableCollection?没有。我正在通过Add或RemoveAt和via indexes添加和删除按钮。这个名单的存在还有另一个原因。但是哈哈,这真是离题了
mainCycle(list);
previousButton = (Control)list.Items.GetItemAt(list.Items.Count - 1);
if (itemCounts >= 4)
{
MessageBox.Show("" + previousButton.Name);
previousButton.Visibility = Visibility.Collapsed;
}