2013-06-12 125 views
3

我有一個網格控件,它有25個按鈕(網格只包含按鈕)。併爲每個按鈕我這樣設置相同的圖像:WPF按鈕背景問題

 ImageBrush brush = new ImageBrush(); 
    BitmapImage bitmap = new BitmapImage(); 
    bitmap.BeginInit(); 
    bitmap.UriSource = new Uri(@"pack://application:,,,/Images/notexplored.jpg", UriKind.RelativeOrAbsolute); 
    bitmap.EndInit(); 
    brush.ImageSource = bitmap; 

    foreach (UIElement uie in myGrid.Children) 
    { 
     var temp = uie as Button; 
     temp.Background = brush; 
    } 

我的問題是,現在當我將鼠標懸停在按鈕我得到這個(這個問題的短視頻):https://www.dropbox.com/s/jb8fb29lrpimue5/ButtonIssue.avi

哪有我改正這個?將圖像作爲背景應用到按鈕後,如果我將鼠標懸停在按鈕上,我不想看到默認的按鈕背景(看起來&感覺)而不是應用的圖像。

+0

順便說一句,你有足夠的代表現在upvote。 ;) – Athari

回答

3

爲了便於重新創建示例,我在此將'StackPanel'作爲容器。如果你不需要動態設置圖片,那麼我希望應用如下所示的樣式應該看起來像你期望的那樣看起來像&的按鈕,同時仍然允許使用事件等,與使用典型的WPF按鈕。

<StackPanel x:Name="sp"> 
     <StackPanel.Resources> 
      <Style TargetType="{x:Type Button}" x:Key="btnWithImageBgStyle"> 
       <Setter Property="Template"> 
        <Setter.Value> 
         <ControlTemplate TargetType="{x:Type Button}"> 
          <Border CornerRadius="5" BorderBrush="{TemplateBinding BorderBrush}" > 
           <Border.Background> 
            <ImageBrush ImageSource="pack://application:,,,/Tulip.jpg"/> 
           </Border.Background> 
           <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" /> 
          </Border> 
         </ControlTemplate> 
        </Setter.Value> 
       </Setter> 
      </Style> 
     </StackPanel.Resources> 
     <Button x:Name="btn1" Width="100" Height="50" Content="Button1!" Style="{StaticResource btnWithImageBgStyle}" Click="btn1_Click"/> 
     <Button x:Name="btn2" Width="100" Height="50" Content="Button2!" Style="{StaticResource btnWithImageBgStyle}"/> 
     <Button x:Name="btn3" Width="100" Height="50" Content="Button3!" Style="{StaticResource btnWithImageBgStyle}"/> 
+0

謝謝。這是我需要的。 – Rock3rRullz

1

而不是設置背景,只需添加一個新的圖像作爲按鈕的Content。一個小例子:

var b = new Button(); 

var bitmap = new BitmapImage(); 
bitmap.BeginInit(); 
bitmap.UriSource = new Uri(@"pack://application:,,,/floor.png", UriKind.RelativeOrAbsolute); 
bitmap.EndInit(); 

var img = new Image {Source = bitmap}; 

b.Content = img; 
+0

我已經將內容設置爲圖像,但現在我有另一個錯誤:指定的元素已經是另一個元素的邏輯子元素。先斷開它。 – Rock3rRullz

+0

我已經使用下面的代碼來設置圖像:Image image = new Image(); BitmapImage bi = new BitmapImage(); bi.BeginInit(); bi.UriSource = new Uri(@「pack:// application:,,,/Images/notexplored.jpg」,UriKind.RelativeOrAbsolute); bi.EndInit(); image.Source = bi; – Rock3rRullz

+0

我加了一個小例子! – dsfgsho

1

你必須改變按鈕的控件模板,以便它確實不再對MouseOver事件作出響應。最簡單的解決辦法是這樣的

<Window.Resources> 
    <Style TargetType="{x:Type Button}"> 
     <Setter Property="Template"> 
      <Setter.Value> 
       <ControlTemplate TargetType="{x:Type Button}"> 
        <ContentPresenter /> 
       </ControlTemplate> 
      </Setter.Value> 
     </Setter> 
    </Style> 
</Window.Resources> 

這是適用於每一個駐留在窗口內的按鈕隱式風格。在控件模板中,您可以更改按鈕的外觀以及它在狀態更改時的行爲方式(例如,當鼠標位於按鈕上方時)。

通常情況下,控制模板會有更多的代碼,但在這個例子中,你只是想要一個顯示某些東西的按鈕,但是當鼠標移出或者當它結束時可以看到所有其他行爲的東西被點擊。您可以在MSDN上查看WPF中按鈕的默認模板:http://msdn.microsoft.com/en-us/library/ms753328.aspx

我絕對建議您在WPF中瞭解更多關於樣式和模板的信息。這是一個很好的起點:http://msdn.microsoft.com/en-us/library/ms745683.aspx

如果您有任何問題,請隨時發表評論。

+0

'網格'是多餘的。另外我看不出將模板分開放置而不是直接放入'Setter.Value'中的理由,特別是在這個微不足道的情況下。 – Athari

+0

確實。午餐時間,我只是用Blend快速生成代碼,請原諒我。 – feO2x

1

如果您只是需要顯示圖像並響應點擊,按鈕會過度殺傷。您只能使用ContentControl

+0

如果他想對Click事件做出反應,我認爲按鈕非常好。 – feO2x

+0

我想使用click事件來了解女巫單元格中的單擊事件,這是我使用網格的原因,並且每個按鈕的定義如下所示:

+0

'MouseDown'或'MouseUp'事件就足夠了。如果沒有計劃的鍵盤訪問,行爲沒有那麼大的不同。 – Athari