2015-06-29 42 views
1
<Button x:Name="btnProfilePicture" HorizontalAlignment="Center" Click="btnProfilePicture_Click"> 
    <Button.Template> 
     <ControlTemplate> 
      <Ellipse x:Name="ellipsePicture" Fill="Turquoise" Width="150" Height="150" Stroke="White" StrokeThickness="10"> 
      </Ellipse> 
     </ControlTemplate> 
    </Button.Template> 
</Button > 

我有一個橢圓形狀的按鈕,默認情況下顏色填充。 我想在運行時在代碼隱藏中將填充更改爲圖像。Windows運行時:如何在橢圓形狀按鈕中設置圖像?

我該怎麼做?

更多信息: 我試圖通過使用「ellipsePicture」名稱設置橢圓填充,但這個名稱不能在代碼後面引用,但我不知道原因。

+0

Winforms或wpf? – progpow

+0

這是一個Windows運行時/地鐵應用程序。 –

+0

請查看這篇文章https://msdn.microsoft.com/en-us/library/windows/apps/xaml/hh868203.aspx – Jerin

回答

3

試試下面的代碼..

<Button x:Name="BtnProfilePicture" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="10"> 
      <Button.Template> 
       <ControlTemplate> 
        <Grid Height="100"> 
         <Ellipse x:Name="ellipsePicture" Fill="{TemplateBinding Background}"/> 
        </Grid> 
       </ControlTemplate> 
      </Button.Template> 
     </Button > 

在後面的代碼中添加以下行..

BtnProfilePicture.BackgroundImage = 
      new ImageBrush { ImageSource = LoadBackgroundImage(yourfilename.jpg)}; 

如果這個工程投了我!

+0

非常感謝。它工作正常!確切的代碼如下:btnProfilePicture.Background = new ImageBrush { ImageSource = new BitmapImage(new Uri(fullpath)) }; –

+0

謝謝Raymond! :-) –

0

試試這個:

<Button x:Name="btnProfilePicture" HorizontalAlignment="Center" Click="btnProfilePicture_Click"> 
<Button.Template> 
    <ControlTemplate> 
     <Ellipse x:Name="ellipsePicture" Width="150" Height="150" Stroke="White" StrokeThickness="10"> 
      <Ellipse.Fill> 
       <ImageBrush ImageSource="/DessCol;component/Images/Recommencer.ico"/> 
      </Ellipse.Fill> 
     </Ellipse> 
    </ControlTemplate> 
</Button.Template> 

+0

我想在運行時代碼中執行此操作,而不是在xaml中對其進行硬編碼。 –

0

試試吧。在此修改後,您可以將圖像設置爲內容。您可以在XAML和運行時進行此更改。

 <Button Content="C:\Round.JPG"> 
      <Button.Template> 
       <ControlTemplate> 
        <Ellipse x:Name="ellipsePicture" Width="150" Height="150" Stroke="White" StrokeThickness="10"> 
         <Ellipse.Fill> 
          <ImageBrush ImageSource="{Binding RelativeSource={RelativeSource AncestorType={x:Type Button}}, 
                  Path=Content}"/> 
         </Ellipse.Fill> 
        </Ellipse> 
       </ControlTemplate> 
      </Button.Template> 
     </Button> 
+0

感謝您的評論,但我想在運行時的代碼中完成此操作。 –

0

您可以使用該按鈕的背景屬性設置在background.You圖像可以用它從後面的代碼也喜歡:

public BitmapImage LoadBackgroundImage(string fileName) 
{ 
      var image = new BitmapImage(); 
      try 
      { 
       image.BeginInit(); 
       if (!string.IsNullOrEmpty(fileName) && File.Exists(fileName)) 
       { 
        var bytes = File.ReadAllBytes(fileName); 
        image.StreamSource = new MemoryStream(bytes); 
       } 
       else 
       { 
        var bytes = File.ReadAllBytes(Path.GetFullPath(Properties.Resources.DefaultBackgroundImage)); 
        image.StreamSource = new MemoryStream(bytes); 
       } 
       image.CacheOption = BitmapCacheOption.OnLoad; 
       image.EndInit(); 
       image.Freeze(); 
      } 
      catch (FileNotFoundException ex) 
      { 
        throw ex; 
      } 

      return image; 
     } 

在按鈕單擊事件中添加以下行

btnProfilePicture.Background = LoadBackgroundImage(yourfilename.jpg); //你可以使用.JPG, .JPEG,*。PNG

+0

但這只是將圖像加載到按鈕中,而不是橢圓形狀。 –

+0

使用上面的代碼來填充橢圓形圖像,如「ellipsePicture.Fill = LoadBackgroundImage(yourfilename.jpg);」 –

+0

我試過這個,但「ellipsePicture」似乎無法在後面的代碼中引用。我不知道這是因爲它在控制模板中,還是嵌套在按鈕控件中或其他內容中。 –