我有一个带有工具栏
的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"},
};
}
}
这是视觉结果:
最佳答案
工具栏中有一个错误,如果重新调整窗口大小,问题就会消失。
解决方案是使用另一个控件,例如:
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/