重新排序后,ToolBar溢出行为有时无法正常工作

时间:2014-09-23 09:49:54

标签: wpf xaml

当我拖动工具栏并更改窗口工具栏的大小时,溢出行为不起作用。这很简单。拖动后我需要溢出行为。 屏幕截图:

1

初始状态。最后一个工具栏没有足够的空间 - 应用了溢出策略。 http://prntscr.com/4pi3yh

2

工具栏重新排序并更改窗口大小(随机)后。出于某种原因,当没有足够的空间时,可以简单地切割一些工具栏。没有应用溢出策略。 http://prntscr.com/4pi40t

这是xaml,没有代码隐藏:

 <Window x:Class="WpfApplication9.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="MainWindow"
            Width="525"
            Height="350">
        <ToolBarTray>
            <ToolBar>
                <Button>Text 1</Button>
                <Button>Text 1</Button>
                <Button>Text 1</Button>
                <Button>Text 1</Button>
                <Button>Text 1</Button>
            </ToolBar>
            <ToolBar>
                <Button>Text 2</Button>
                <Button>Text 2</Button>
                <Button>Text 2</Button>
                <Button>Text 2</Button>
                <Button>Text 2</Button>
            </ToolBar>
            <ToolBar>
                <Button>Text 3</Button>
                <Button>Text 3</Button>
                <Button>Text 3</Button>
                <Button>Text 3</Button>
                <Button>Text 3</Button>
                <Button>Text 3</Button>
            </ToolBar>
        </ToolBarTray>
    </Window>

我注意到拖动后工具栏的Width属性从Auto(NaN)更改为某个特定宽度。并且具有宽度!=自动的工具栏被切断。我认为ToolBarTray在拖动时会改变Width属性。 任何解决方案或解决方法将不胜感激。

1 个答案:

答案 0 :(得分:0)

我对此问题的解决方法。如果你知道一些更好的解决方案 - 请告诉我。 正如我写的宽度是由ToolBarTray改变所以我们需要将它改回Auto。这段代码可以解决问题:

        var dpd = DependencyPropertyDescriptor.FromProperty(ToolBar.WidthProperty, typeof(ToolBar));
        dpd.AddValueChanged(AssociatedObject,
            delegate
            {
                if(!double.IsNaN(AssociatedObject.Width))
                    AssociatedObject.Width = double.NaN;
            });