2012-01-09 19 views
1

我需要給背景圖片的大小,因爲我將圖片圖片(32x32)作爲背景圖片添加到我的按鈕中,對於按鈕來說很大,而且看起來很糟糕,所以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"> 
     <Grid> 
     <Image Source="C:\Users\Fernando\Desktop\Eye-Mouse\apple\apple\img0.jpg" Stretch="Fill"/> 
      <UniformGrid Name="gridx" Rows="6"> 
      </UniformGrid> 
    </Grid> 
</Window> 

而到了uniformGrid我添加的按鈕,像這樣:

ImageBrush ib = new ImageBrush(); 
System.Windows.Controls.Button newBtn = new Button(); 
FileToImageIconConverter some = new FileToImageIconConverter(link[n]); 
ImageSource imgSource = some.Icon; 
ib.ImageSource = imgSource; 
newBtn.Background = ib; 
gridx.Children.Add(newBtn); 

感謝您的任何幫助。

+1

你能澄清一下「看起來不好」是什麼意思嗎?另外,您是否可以爲您的問題添加修剪後的屏幕截圖,並且更準確地說明您希望它看起來像什麼樣子? – 2012-01-09 09:17:22

回答

2

您可能想使用Xaml &數據綁定來填充統一網格。這樣,您可以在Expression Blend中設置按鈕的樣式,只要您喜歡。

<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"> 
     <Grid> 
     <Image Source="C:\Users\Fernando\Desktop\Eye-Mouse\apple\apple\img0.jpg" Stretch="Fill"/> 
     <!-- YourLinkList is the list that provides the link[n] in your example --> 
     <ItemsControl ItemsSource="{Binding Path=YourLinkList}"> 
      <ItemsControl.ItemsPanel> 
       <ItemsPanelTemplate> 
        <UniformGrid Rows="6"/> 
       </ItemsPanelTemplate> 
      </ItemsControl.ItemsPanel> 
      <ItemsControl.ItemTemplate> 
       <DataTemplate> 
        <Button> 
         <Button.Background> 
          <ImageBrush> 
           <ImageBrush.ImageSource> 
            <!-- {Binding .} means that the UriSource is bound to the item in the link list --> 
            <BitmapImage UriSource="{Binding .}" DecodePixelWidth="32" DecodePixelHeight="32"/> 
           </ImageBrush.ImageSource> 
          </ImageBrush> 
         </Button.Background> 
        </Button> 
       </DataTemplate> 
      </ItemsControl.ItemTemplate> 
    </ItemsControl> 
    </Grid> 
</Window> 

您還可以嘗試將圖像設置爲按鈕的內容而不是將其用作背景。也許這會更好看。

+0

謝謝你的幫助,但我正在尋找如何做鏈接[n]綁定路徑,但我沒有得到它,你能教我嗎? – 2012-01-09 17:06:49

+0

@FernandoSantiago好吧,我想你將鏈接存儲在數組或列表中(實現'IEnumerable <>'的那個)。您只需要將ItemsControl的ItemsSource設置爲該列表。 ItemsControl的第n項的'DataContext'然後是列表中的第n項。 – kev 2012-01-09 18:09:26