wpf - 如何在不使用扩展器的情况下将 WPF 工具栏绑定(bind)到我的 VM 中的集合

标签 wpf data-binding mvvm toolbar

我有一个带有工具栏的WPF窗口。我的虚拟机中有一组要绑定(bind)的对象。它们显示为按钮,但总是被推到工具栏的展开下拉部分。如何使这些按钮出现在工具栏的标准部分?

我有以下 XAML:

<ToolBarTray Grid.Row="1">
    <ToolBar ItemsSource="{Binding Path=MyList}" >
        <ToolBar.ItemTemplate>
            <DataTemplate  >
                <Button ToolTip="{Binding ButtonName}" 
                        Command="{Binding Path=ButtonCommand}" >
                    <Button.Content>
                        <Image Width="32" Height="32" Source="{Binding ImageSource}"/>
                    </Button.Content>
                </Button>
            </DataTemplate>
        </ToolBar.ItemTemplate>
    </ToolBar>
</ToolBarTray>

我有以下 C#:

public List<MyClass> MyList
    {
        get
        {
            return new List<MyClass>
                       {
                           new MyClass{ButtonName="Button1",ImageSource=@"C:\Projects\WpfApplication2\WpfApplication2\Employee.png"},
                           new MyClass{ButtonName="Button2",ImageSource=@"C:\Projects\WpfApplication2\WpfApplication2\Employee.png"},
                           new MyClass{ButtonName="Button3",ImageSource=@"C:\Projects\WpfApplication2\WpfApplication2\Employee.png"},
                           new MyClass{ButtonName="Button4",ImageSource=@"C:\Projects\WpfApplication2\WpfApplication2\Employee.png"},
                       };

        }
    }

这是视觉结果:

alt text

最佳答案

工具栏中有一个错误,如果重新调整窗口大小,问题就会消失。

解决方案是使用另一个控件,例如:

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

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

查看this thread了解更多信息

关于wpf - 如何在不使用扩展器的情况下将 WPF 工具栏绑定(bind)到我的 VM 中的集合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1589034/

相关文章:

c# - 计算时间长

c# - 使用后台线程生成的 RenderTargetBitmap 设置图像源

c# - Animation Completed 稍微到很快就会触发

c# - 通过 XML 属性扩展 ASP.Net TreeView 中的节点

wpf - 通过两个级别绑定(bind)到 TemplatedParent

wpf - 过多的数据绑定(bind)是否会使 WPF 应用程序变慢?有哪些可用的优化技术?

wpf 数据绑定(bind)网格行可见性

wpf - 如何使用 TDD 测试 BusyIndi​​cator IsBusy 属性

wpf - Caliburn - 异常处理和救援

c# - MVVM 和控件的动态生成