简体   繁体   English

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

[英]Windows Runtime: How to set image in a ellipse shape button?

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

I have a button in ellipse shape, with color fill by default. 我有一个椭圆形的按钮,默认情况下填充颜色。 I want to change the fill to an image in code-behind in runtime. 我想在运行时将填充更改为代码隐藏中的图像。

How can I do this? 我怎样才能做到这一点?

More info: I tried to set the ellipse fill by using the "ellipsePicture" name but this name cannot be referenced in the code behind but I dont know the reason. 更多信息:我尝试使用“ellipsePicture”名称设置椭圆填充,但此名称不能在后面的代码中引用,但我不知道原因。

Try the following code.. 请尝试以下代码..

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

On your code behind add the following line.. 在您的代码后面添加以下行..

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

Vote me if this works! 如果有效,请投票给我!

Try this: 尝试这个:

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

Try it. 试试吧。 After this modify you can set image to content. 修改完成后,您可以将图像设置为内容。 This changes you can made at XAML and runtime. 您可以在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>

You can use Background property of the button to set image on the background.You can use it from code behind also like: 您可以使用按钮的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;
        }

in your button click event add the following line 在按钮单击事件中添加以下行

btnProfilePicture.Background=LoadBackgroundImage(yourfilename.jpg); btnProfilePicture.Background = LoadBackgroundImage(yourfilename.jpg); //you can use .jpg, .jpeg,*.png //你可以使用.jpg, .jpeg,*。png

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM