0
我需要顯示多個按鈕,但每個按鈕必須有不同於其他按鈕的背景,我一直在努力,但我只能顯示多個按鈕,但與相同的背景。 這裏是XAML:我怎樣才能在WPF中的多個按鈕顯示不同的背景
<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#代碼:
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);
}
}
}
}
任何想法?謝謝。
非常感謝!有沒有什麼辦法讓每個按鈕執行不同的動作? – 2012-01-07 20:57:29
@FernandoSantiago:是的,要麼創建一個宿主圖像的類和一個[命令](http://msdn.microsoft.com/en-us/library/ms752308.aspx)並分別綁定它們(這是更清潔的)或取決於任何條件,勾選['Click'事件](http://msdn.microsoft.com/en-us/library/system.windows.controls.button.aspx)。 – 2012-01-07 21:00:06
再次感謝你,我會努力! – 2012-01-07 21:02:22