以编程方式显示功能区按钮图像



我正在尝试将图像加载到功能区按钮上。这是 System.Windows.Controls.RibbonBar 类型。

这是我正在使用的代码

public RibbonGroup CreateButtons()
{
    RibbonGroup GroupControlComputers = new RibbonGroup();
    GroupControlComputers.Header = "Computer Control";
    GroupControlComputers.Items.Add(DropdownButton("DropDown Stuffs"));
    return GroupControlComputers;     
}
public RibbonButton DropdownButton(String Caption)
{
    RibbonButton NewRibbonButton = new RibbonButton();
    NewRibbonButton.Label = Caption;

    NewRibbonButton.AllowDrop = true;
    return NewRibbonButton;
}

我不知道如何添加图标。 我可以毫无问题地添加没有图像的按钮

类文件创建按钮的路径是 MyProject\Functions\Ribbonbar.cs
图标文件的路径是 MyProject\Images\Test\smallicon.ico

我试图弄清楚LargeImageSource,只是无法理解我需要做什么。

您需要指定两个图像: LargeImageSourceSmallImageSource ,它们实际上需要不同(一个 - 更大,一个 - 更小),但为了测试,请尝试以下操作:

public RibbonButton DropdownButton(String Caption)
{
    RibbonButton NewRibbonButton = new RibbonButton();
    NewRibbonButton.Label = Caption;
    BitmapImage image = new BitmapImage();
    image.BeginInit();
    // your path to image might be different
    image.UriSource = new Uri("pack://application:,,,/Images/Test/smallicon.ico"); 
    image.EndInit();
    NewRibbonButton.SmallImageSource = image;
    NewRibbonButton.LargeImageSource = image;
    NewRibbonButton.AllowDrop = true;
    return NewRibbonButton;
}

最新更新