Windows运行时:如何在椭圆形状按钮中设置图像?

时间:2015-06-29 10:41:19

标签: c# windows-runtime winrt-xaml

<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 >

我有一个椭圆形的按钮,默认为彩色填充。 我想在运行时将填充更改为代码隐藏中的图像。

我该怎么做?

更多信息: 我尝试使用&#34; ellipsePicture&#34;设置椭圆填充。名称,但这个名称不能在后面的代码中引用,但我不知道原因。

4 个答案:

答案 0 :(得分: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)};

如果有效,请投票给我!

答案 1 :(得分: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>

答案 2 :(得分: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>

答案 3 :(得分:0)

您可以使用按钮的Background属性在背景上设置图像。您也可以从后面的代码中使用它,如:

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

相关问题