WPF 列表框 dataContextChanged

发布于 2024-09-30 06:41:02 字数 740 浏览 4 评论 0原文

我的用户控件中有一个列表框,每当用户控件的数据上下文发生更改时,我想选择列表框中的第一项。 (列表框的 ItemsSource 绑定到 userControl dataContext :

<userControl>
     <ListBox Name="listBox_Resources" ItemsSource="{Binding Path=Resources}" DataContextChanged="listBox_Resources_DataContextChanged">                
      </ListBox>
</userControl>

private void listBox_Resources_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            MessageBox.Show(listBox_Resources.SelectedIndex.ToString() + " " + listBox_Resources.Items.Count.ToString());
            listBox_Resources.SelectedIndex = 0;          
        }

似乎 dataContextChanged 在填充列表框项目之前被触发,因为事件处理程序中的消息框将返回先前列表框项目的计数。 请帮我找到解决方案。 谢谢

I've a listbox in a userControl and I want to select the first item in the listbox whenever datacontext of my userControl is changed. (listbox's ItemsSource is Bind to userControl dataContext :

<userControl>
     <ListBox Name="listBox_Resources" ItemsSource="{Binding Path=Resources}" DataContextChanged="listBox_Resources_DataContextChanged">                
      </ListBox>
</userControl>

private void listBox_Resources_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            MessageBox.Show(listBox_Resources.SelectedIndex.ToString() + " " + listBox_Resources.Items.Count.ToString());
            listBox_Resources.SelectedIndex = 0;          
        }

it seems that dataContextChanged is fired before the listbox items is populated because my messagebox in the eventhandler will return me count of the previous listbox items.
please help me finding the solution.
thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

尽揽少女心 2024-10-07 06:41:02

尝试一下

private void listBox_Resources_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
{
    EventHandler eventHandler = null;
    eventHandler = new EventHandler(delegate
    {
        if (listBox_Resources.ItemContainerGenerator.Status == GeneratorStatus.ContainersGenerated)
        {
            listBox_Resources.SelectedIndex = 0;
            listBox_Resources.ItemContainerGenerator.StatusChanged -= eventHandler;
        }
    });
    listBox_Resources.ItemContainerGenerator.StatusChanged += eventHandler;
}

如果您在最后一行放置断点

listBox_Resources.ItemContainerGenerator.StatusChanged += eventHandler;

并在调试器中观察 listBox_Resources.ItemContainerGenerator.Status 的值,它应该显示为“ContainersGenerate”。如果您随后在委托 EventHanler 中添加一个断点,

if (listBox_Resources.ItemContainerGenerator.Status == GeneratorStatus.ContainersGenerated)

您应该第一次看到“c_listBox.ItemContainerGenerator.Status = GeneratingContainers”,然后当它再次命中时,它应该是 ContainersGenerate,然后我们可以设置 SelectedIndex。无论如何,这对我有用。

Try this

private void listBox_Resources_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
{
    EventHandler eventHandler = null;
    eventHandler = new EventHandler(delegate
    {
        if (listBox_Resources.ItemContainerGenerator.Status == GeneratorStatus.ContainersGenerated)
        {
            listBox_Resources.SelectedIndex = 0;
            listBox_Resources.ItemContainerGenerator.StatusChanged -= eventHandler;
        }
    });
    listBox_Resources.ItemContainerGenerator.StatusChanged += eventHandler;
}

If you put a breakpoint at the last line

listBox_Resources.ItemContainerGenerator.StatusChanged += eventHandler;

and watch the value of listBox_Resources.ItemContainerGenerator.Status in the debugger it will should read "ContainersGenerated". If you then add a breakpoint within the delegate EventHanler at

if (listBox_Resources.ItemContainerGenerator.Status == GeneratorStatus.ContainersGenerated)

you should see that "c_listBox.ItemContainerGenerator.Status = GeneratingContainers" a first time and then when it hits again it should be ContainersGenerated and then we can set SelectedIndex. Anyway, that's working for me.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文