我有一个包含 RibbonGroup 的 wpf RibbonTab。 我正在尝试将 RibbonButtons 动态添加到 RibbonGroup。
我可以让它显示我的 ObservableCollection 项目中的第一个项目。 我已经切换了集合中的第一个项目,它始终只显示第一个项目,无论它是什么。
我尝试将 RibbonGroup 的宽度设置为较宽的值,虽然它使 RibbonGroup 与我告诉的一样宽,但它仍然不会显示超过一个 RibbonButton。目前我将其设置为 Width="Auto"
任何帮助解决为什么我无法在 RibbonGroup 中显示多个按钮的问题将不胜感激。
这是我的xaml:
<RibbonTab KeyTip="0" IsSelected="{Binding IsTestTabSelected}" Header="Test Tab"
Visibility="{Binding ShowTestTab, Converter={StaticResource BoolToVisibilityConverter}}" Width="Auto">
<RibbonTab.DataContext>
<Binding Path="TestRibbonViewModel" />
</RibbonTab.DataContext>
<RibbonTab.HeaderTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" Width="Auto">
<TextBlock Text="Test Tab" Margin="5,0,0,0" VerticalAlignment="Center"/>
</StackPanel>
</DataTemplate>
</RibbonTab.HeaderTemplate>
<RibbonGroup x:Name="tgStep2" Header="Delete older files" Width="Auto">
<ItemsControl ItemsSource="{Binding GroupWeeksOldButtons}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<RibbonButton
LargeImageSource="{Binding ImageSource}"
Width="{Binding Width}"
Label="{Binding Content}"
ToolTip="{Binding ToolTip}"
Command="{Binding Command}" />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</RibbonGroup>
</RibbonTab>
这是我的 RibbonViewModel:
public class RibbonViewModel
{
public ObservableCollection<RibbonButtonModel> GroupWeeksOldButtons { get; private set; }
public ObservableCollection<RibbonButtonModel> GroupMonthsOldButtons { get; private set; }
public RibbonViewModel()
{
// Initialize the ObservableCollection
GroupWeeksOldButtons = new ObservableCollection<RibbonButtonModel>();
GroupMonthsOldButtons = new ObservableCollection<RibbonButtonModel>();
}
public void AddWeeksOldButton(RibbonButtonModel button)
{
GroupWeeksOldButtons.Add(button);
}
public void AddMonthsOldButton(RibbonButtonModel button)
{
GroupMonthsOldButtons.Add(button);
}
public void RemoveWeeksOldButton(RibbonButtonModel button)
{
GroupWeeksOldButtons.Remove(button);
}
public void RemoveMonthsOldButton(RibbonButtonModel button)
{
GroupMonthsOldButtons.Remove(button);
}
}
这是我的 RibbonButtonModel:
public class RibbonButtonModel
{
public string Content { get; set; } = string.Empty;
public string ToolTip { get; set; } = string.Empty;
public string ImageSource { get; set; } = string.Empty;
public ICommand Command { get; set; } = null;
public string CommandParameter { get; set; } = string.Empty;
public int Width { get; set; } = 75;
}
我找到了解决方案。这就是我所做的。
我替换了 RibbonGroup 下的 ItemsControl,并使用了 ListBox。
这是更新后的 RibbonTab:
<RibbonTab KeyTip="1" IsSelected="{Binding TestTabIsSelected}" Header="Test"
Visibility="{Binding ShowTestTab, Converter={StaticResource BoolToVisibilityConverter}}" Width="Auto">
<RibbonTab.DataContext>
<Binding Path="TestRibbonViewModel" />
</RibbonTab.DataContext>
<RibbonTab.HeaderTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" Width="Auto">
<TextBlock Text="Test" Margin="5,0,0,0" VerticalAlignment="Center"/>
</StackPanel>
</DataTemplate>
</RibbonTab.HeaderTemplate>
<RibbonGroup x:Name="rgStep1" Header="Delete older files" Width="Auto">
<ListBox ItemsSource="{Binding GroupWeeksOldButtons}" BorderThickness="0" Background="Transparent"
ScrollViewer.HorizontalScrollBarVisibility="Disabled"
ScrollViewer.VerticalScrollBarVisibility="Disabled">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal"/>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.ItemTemplate>
<DataTemplate>
<RibbonButton
Width="{Binding Width}"
Label="{Binding Content}"
ToolTip="{Binding ToolTip}"
Command="{Binding Command}"
LargeImageSource="{Binding ImageSource}"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Margin="0,0,0,0"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</RibbonGroup>
</RibbonTab>