WPF ToolbarControl 绑定在溢出模式下中断

WPF ToolbarControl binding breaks in overflow mode

我正在编写一个在工具栏中有一系列按钮的应用程序。我遇到的问题是 Command={Binding ...} 中断了延伸到工具栏控件溢出弹出窗口的按钮。

这是XAML:

<ToolBarTray>
    <ToolBar
        ItemsSource="{Binding Path=AppConfigs, NotifyOnSourceUpdated=True}"
        MaxHeight="100"
        MaxWidth="100"
        OverflowMode="AsNeeded">
        <ToolBar.ItemTemplate>
            <DataTemplate>
                <Button
                    Name="btn"
                    Command="{Binding DataContext.IconClickCommand, RelativeSource={RelativeSource AncestorType=UserControl}}"
                    CommandParameter="{Binding Loc}">
                    <Button.Content>
                        <Image Source="{Binding IconLoc}" />
                    </Button.Content>
                </Button>
            </DataTemplate>
        </ToolBar.ItemTemplate>
    </ToolBar>
</ToolBarTray>

理想情况下,IconClickCommand 将成为 ItemTemplate 中项目的一部分,但由于代码的其他方面,我必须将其保留在外部视图的 ViewModel 中。

我尝试了一些使用 RelativeSource 绑定的方法,但无济于事。这些按钮上的所有其他绑定(工具提示、内容图像等)仍然有效。

您可以设置 UserControlName 属性 并使用 x:Reference:

<UserControl ... Name="uc">
...
<ToolBar.ItemTemplate>
    <DataTemplate>
        <Button
            Name="btn"
            Command="{Binding DataContext.IconClickCommand, Source={x:Reference uc}}"
            CommandParameter="{Binding Loc}">