【问题标题】:How to get a WPF Toolbar to bind to a collection in my VM without using expander如何在不使用扩展器的情况下让 WPF 工具栏绑定到我的 VM 中的集合
【发布时间】:2009-10-19 14:36:26
【问题描述】:

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

我有以下 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"},
                       };

        }
    }

这是视觉结果:

【问题讨论】:

  • 如果我将工具栏的静态宽度设为 500,则会发生相同的视觉结果
  • 你能说得更具体些吗
  • @Shimmy,我认为我不能更具体,但我会尝试。我希望工具栏中的那些按钮显示在工具栏的标准(非溢出)部分

标签: wpf data-binding mvvm toolbar


【解决方案1】:

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

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

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

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

查看this thread了解更多信息

【讨论】:

  • 不幸的是,这不起作用。不过你是对的,当我调整屏幕大小时,按钮会按预期显示。
  • 在我的机器上工作,试试我制作的这个准系统示例bizcacha.com/public/WpfApplication3.rar
  • 这个解决方案也不能直接为我工作。不过,我还没有检查您提供的样品。
  • 对不起爱德华多,我的错。此解决方案有效。我也忘了用&lt;WorkaroundToolBar.ItemTemp... 替换&lt;ToolBar.ItemTemplate...
  • 不幸的是,这并不完全有效。如果按钮集合最初为空,则启动应用程序并通过代码动态添加一些按钮,然后(解决方法)工具栏在视觉上仍然无效。它看起来就像上面的屏幕截图,并在调整窗口大小后立即正确重绘。还有更多提示吗?
【解决方案2】:

您还可以将 xaml 中工具栏的高度设置为对我有用的合理值。

【讨论】:

    【解决方案3】:

    要添加到 Eduardo's,我不得不稍微调整一下,因为我的项目源是在显示初始 UI 后的一段时间内异步填充的:

    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);
        }
    }
    

    这足以捕获所有边缘情况并在填充项目后根据需要正确发生溢出。

    【讨论】:

      【解决方案4】:

      设置 'ToolBar.OverflowMode="Never"'。

      更多关于工具栏溢出的信息可以在here找到。

      【讨论】:

      • 您应该对自己的回答更有信心。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-19
      • 2012-02-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多