2013-04-09 89 views
4

我想改變我的按鈕的背景圖像其他一些圖像,我遇到了一些錯誤。 這是我對我的XAML代碼:如何在C#WPF代碼中更改設置按鈕的背景圖像?

<Button x:Name="Button1" Width="200" Height="200" Content="Button1" Margin="0,0,0,400"> 
     <Button.Background> 
      <ImageBrush **ImageSource ="Images/AERO.png"** ></ImageBrush> 
     </Button.Background> 
    </Button> 

和我的CS:

private void Button1_Click_1(object sender, RoutedEventArgs e) 
    { 
     var brush = new ImageBrush(); 
     brush.ImageSource = new BitmapImage(new Uri("Images/AERO.png")); 
     Button1.Background = brush; 
    } 

我有我的XAML中的錯誤是「文件‘圖片\ logo.png’不是一部分 任何人都可以幫我解釋一下,謝謝

+1

你能否證實在Visual Studio項目文件「圖像\ logo.png」是否被incuded。如果是,您是否也可以在其屬性選項卡上確認其生成操作已設置爲資源? – Kohanz 2013-04-09 02:36:10

+0

我該如何設置Build Action屬性?我似乎無法找到它。該文件 – Jaz 2013-04-09 02:41:43

+0

單擊鼠標右鍵,選擇屬性 – Kohanz 2013-04-09 02:43:17

回答

13

在構建動作,你可以標記的圖像文件的內容或資源。在ImageBrush中使用圖像的語法根據您選擇的語法而有所不同。

這裏是標記爲內容一個圖像文件。

Image, marked as content

要設置按鈕背景該圖像使用下面的代碼。

var brush = new ImageBrush(); 
brush.ImageSource = new BitmapImage(new Uri("Images/ContentImage.png",UriKind.Relative)); 
button1.Background = brush; 

這裏是標記爲資源的圖像文件。

Image, marked as resource

要設置按鈕背景資源圖像使用下面的代碼。

Uri resourceUri = new Uri("Images/ResourceImage.png", UriKind.Relative); 
    StreamResourceInfo streamInfo = Application.GetResourceStream(resourceUri); 

    BitmapFrame temp = BitmapFrame.Create(streamInfo.Stream); 
    var brush = new ImageBrush(); 
    brush.ImageSource = temp; 

    button1.Background = brush; 
+0

我只有一個問題。無法找到類型或名稱空間名稱「StreamResourceInfo」。(你是否缺少使用指令或程序集引用) – Jaz 2013-04-09 06:34:20

+0

這是一個常見的C#編譯器錯誤。你需要添加一個使用System.Windows.Resources;到您的代碼窗口的頂部。 – 2013-04-09 07:37:54

1

如果沒有包含,請在項目中包含文件「Image \ Logo.png」。然後通過訪問該文件的屬性選項卡將其構建操作設置爲「資源」 (右鍵點擊)。

Setting action to build resource

而且,我不知道你想在按鈕的Click處理程序做什麼。您已經在XAML中設置了背景圖像。除非您將它設置爲Click處理程序中的另一個圖像,否則不需要該代碼。

+0

是的,它的資源下已經..但現在沒有錯誤,但我不能再看到主窗口上的按鈕。 – Jaz 2013-04-09 02:50:11

2

我給一個代碼段下面,assing在代碼片段中提到的按鈕或切換按鈕的風格,那麼你就已經改​​變的背景下通過XAML完全控制...沒有其他的編碼需要。我不是得到安寧的所有代碼只是試圖瞭解背後的邏輯;)

<Style x:Key="KeyStopButtonStyle" TargetType="{x:Type Button}"> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="Button"> 
       <Border TextBlock.Foreground="{TemplateBinding Foreground}" 
            x:Name="Border" 
            CornerRadius="1" 
            BorderThickness="1"> 
        <Border.Background>        
         <ImageBrush ImageSource= "..\Images\ButtonImages\StopButton.png" Stretch="Uniform"/> 
        </Border.Background>       
        <Border.Effect>        
         <DropShadowEffect/>         
        </Border.Effect> 
       </Border> 
       <ControlTemplate.Triggers>       
        <Trigger Property="IsPressed" Value="true"> 
         <Setter TargetName="Border" Property="Border.Effect" Value="{x:Null}"/>                
        </Trigger> 
       </ControlTemplate.Triggers> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
</Style> 

<Style x:Key="KeyPlayPauseButtonStyle" TargetType="{x:Type ToggleButton}"> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="ToggleButton"> 
       <Border TextBlock.Foreground="{TemplateBinding Foreground}" 
            x:Name="Border" 
            CornerRadius="1" 
            BorderThickness="1"> 
        <Border.Background> 
         <ImageBrush ImageSource= "..\Images\ButtonImages\PlayButton.png" Stretch="Uniform"/> 
        </Border.Background> 
        <Border.Effect> 
         <DropShadowEffect/> 
        </Border.Effect> 
       </Border> 
       <ControlTemplate.Triggers> 
        <Trigger Property="IsChecked" Value="true"> 
         <Setter TargetName="Border" Property="Border.Background"> 
          <Setter.Value> 
           <ImageBrush ImageSource= "..\Images\ButtonImages\PauseButton.png" Stretch="Uniform"/> 
          </Setter.Value> 
         </Setter> 
        </Trigger> 
       </ControlTemplate.Triggers> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
</Style>