Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/296.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在WPF中的多个按钮中显示不同的背景_C#_Wpf_Xaml_Button - Fatal编程技术网

C# 如何在WPF中的多个按钮中显示不同的背景

C# 如何在WPF中的多个按钮中显示不同的背景,c#,wpf,xaml,button,C#,Wpf,Xaml,Button,我需要显示多个按钮,但每个按钮必须有不同的背景比其他按钮,我一直在努力,但我只需要显示多个按钮,但具有相同的背景。 以下是XAML: <Window x:Class="apple.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

我需要显示多个按钮,但每个按钮必须有不同的背景比其他按钮,我一直在努力,但我只需要显示多个按钮,但具有相同的背景。 以下是XAML:

<Window x:Class="apple.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="370" Width="525">
    <Grid>
        <Image Source="C:\Users\Public\Pictures\Sample Pictures\Koala.jpg" Stretch="Fill"/>
        <DockPanel Name="dock">
            <UniformGrid Name="gridx" DockPanel.Dock="Top" Rows="3" Columns="3" Height="334"> 
            </UniformGrid>
        </DockPanel>
    </Grid>
</Window>

此外,以下是C代码:

苹果名称空间 { 公共部分类主窗口:窗口 { 公共主窗口() { 初始化组件(); masterGUI(); } public-void-masterGUI() { ImageBrush ib=新的ImageBrush(); IconImage[]ico=null; 位图[]img=null; string[]list=null; string[]link=Directory.GetFiles(@“C:\ProgramData\Microsoft\Windows\Start Menu\Programs”、“*.lnk”、SearchOption.AllDirectories); 列表=新字符串[link.Length]; ico=新图标[link.Length]; img=新位图[link.Length]; for(int n=0;n }


有什么想法吗?多谢各位

需要在for循环中为每个项目分别创建
ImageBrush
。否则,每个项目的背景都将相同


此外,您的做法是“错误的”,在WPF中,您应该使用and来完成这类操作,而不是强制循环。

非常感谢!有没有办法让每个按钮执行不同的操作?@FernandoSantiago:是的,可以创建一个类来承载映像和并分别绑定它们(这比较干净),也可以根据任何条件连接映像。@FernandoSantiago:不客气。如果这回答了你的问题,你会发现它。如果你对我提到的那些东西有疑问,请先浏览链接的文章并尝试一下,然后再问一个单独的问题,但如果你仔细阅读它们,应该不会太难(即使需要一些时间才能习惯)。
namespace apple
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            masterGUI();
        }

    public void masterGUI()
    {
        ImageBrush ib = new ImageBrush();
        IconImage[] ico = null;
        Bitmap[] img = null;
        string[] list = null;
        string[] link = Directory.GetFiles(@"C:\ProgramData\Microsoft\Windows\Start Menu\Programs", "*.lnk", SearchOption.AllDirectories);
        list = new string[link.Length];
        ico = new Icon[link.Length];
        img = new Bitmap[link.Length];
        for (int n = 0; n < link.Length; n++)
        {
            System.Windows.Controls.Button newBtn = new Button();
            list[n] = System.IO.Path.GetFileNameWithoutExtension(link[n]);
            FileToImageIconConverter some = new FileToImageIconConverter(link[n]);
            ImageSource imgSource = some.Icon;
            ib.ImageSource = imgSource;
            newBtn.Background = ib;
            newBtn.Content = list[n];
            gridx.Children.Add(newBtn);
        }  
    }
}