How to get a WPF Toolbar to bind to a collection in my VM without using expander

与世无争的帅哥 提交于 2019-12-03 15:36:20

There is a bug in the toolbar, if you re-size the window, the problem goes away.

The solution is using another control, like:

public class WorkaroundToolBar : ToolBar
{
    private delegate void IvalidateMeasureJob();

    public override void OnApplyTemplate()
    {
        Dispatcher.BeginInvoke(new IvalidateMeasureJob(InvalidateMeasure), 
                                     DispatcherPriority.Background, null);
        base.OnApplyTemplate();
    }
}

Check out this thread for more info

You can also set the height of the toolbar in the xaml to a reasonable value which has worked for me.

To add to Eduardo's, I had to slightly tweak this since my item source was getting populated asynchronously, some time after the initial UI is displayed:

public class ToolBar : System.Windows.Controls.ToolBar
{
    public override void OnApplyTemplate ()
    {
        Dispatcher.BeginInvoke ((Action)(InvalidateMeasure), DispatcherPriority.Background, null);
        base.OnApplyTemplate ();
    }
    protected override void OnItemsChanged (NotifyCollectionChangedEventArgs e)
    {
        base.OnItemsChanged (e);
        Dispatcher.BeginInvoke ((Action)(InvalidateMeasure), DispatcherPriority.Background, null);
    }
}

This was enough to catch all edge cases and have the overflow properly happen as needed after population of the items.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!