2014-02-14 131 views
3

啓發this question我會使這個問題複雜化。在WPF中點擊更改圈子「啓動 - 停止」按鈕背景圖片

我的目標是一個圓按鈕(與像enter image description here背景圖像)whick執行像「啓動 - 停止」按鈕:

1)第一次點擊改變按鈕圖象「停止」圖像並啓動一些計算功能(進程,任務等)

2)enother click button button image to the original「start」image and stopping the function。

按鈕的代碼是這樣的:

<Button x:Name="btnStartStop" Width="72" Height="72" Content="" Margin="180,0,372,94" VerticalAlignment="Bottom" d:LayoutOverrides="VerticalAlignment"> 
     <Button.Template> 
      <ControlTemplate> 
       <Grid> 
        <Ellipse> 
         <Ellipse.Fill> 
          <ImageBrush ImageSource="Images/Start.jpg"/> 
         </Ellipse.Fill> 
        </Ellipse> 
       </Grid> 
      </ControlTemplate> 
     </Button.Template> 
</Button> 

謝謝。

P.S. @Sheridan要求提及他的名字,也許他可以幫助我。

UPDATE我的按鈕看起來非常像我的背景圖像 - 它是無邊框和可點擊區域相匹配的圖像(它是圓的)。該圓形圖像之外的所有內容都是透明的,並且不會觸發點擊事件這就是爲什麼我必須使用Ellipse。

回答

1

您可以使用DataTrigger更改按鈕的背景圖像。下面是鏈接可能會有所幫助。

WPF Change button background image when clicked

和下面是對於相同的溶液。根據您的需要正確工作。

enter image description here

enter image description here

+0

從您的[**第一鏈接**]以編程方式設置它(無DataTriggers)的方法(http://stackoverflow.com/questions/5971300/programmatically-changing-button-icon-in-wpf/5973592#5973592 )最適合我。但是我不能使用'btnStartStop.Content = FindResource(「Stop」)',因爲你可以看到我需要在Button裏面的Ellipse裏設置ImageBrush的圖像源。我怎樣才能做到這一點? – bairog

+0

請檢查更新我的答案,讓我知道任何幫助.. –

+0

Thx尋求幫助,但你不明白我的按鈕看起來像(可點擊區域應該是EXACT的圖片 - 一個圓圈)。我已更新我的原始帖子。 – bairog

0

這實際上聽起來像一個完美的用例爲ToggleButton或者甚至是一個自定義的複選框。

您可以提供自己的控件模板,使用兩種不同的圖像/圖形來顯示兩種不同的切換狀態。使用VisualStateManager您可以聲明在轉換過程中要執行的操作。