Вы имеете , чтобы послушать и реагировать на ItemsGenerator.StatusChanged
Событие и ожидать, пока ItemContainers не сгенерированы, прежде чем можно будет получить доступ к ним с ContainerFromElement.
Поиск далее, я нашел поток на форуме MSDN от кого-то, у кого есть та же проблема. Это, кажется, ошибка в WPF, когда каждому установили GroupStyle. Решение состоит в том, чтобы плыть на плоскодонке доступ ItemGenerator после процесса рендеринга. Ниже код для Вашего вопроса. Я попробовал это, и это работает:
void ItemContainerGenerator_StatusChanged(object sender, EventArgs e)
{
if (listBox1.ItemContainerGenerator.Status
== System.Windows.Controls.Primitives.GeneratorStatus.ContainersGenerated)
{
listBox1.ItemContainerGenerator.StatusChanged
-= ItemContainerGenerator_StatusChanged;
Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Input,
new Action(DelayedAction));
}
}
void DelayedAction()
{
var i = listBox1.ItemContainerGenerator.ContainerFromIndex(1) as ListBoxItem;
// select and keyboard-focus the second item
i.IsSelected = true;
i.Focus();
}
The built in system for publishing pages is a little bit limited.
One thing that I find useful is with WinSCP, there is a featured called "Keep Remote Directory up to Date". What it will do is set a bunch of file system watchers for your local system and if you change something locally, it will auto upload it. Using that and publishing to a local directory makes things easy.