Adding an Image inside a Button programmatically

依然范特西╮ 提交于 2019-12-23 08:18:22

问题


In WPF:

<Button Width="24" Height="24" >
    <Image Source="pack://application:,,,/res/x.png" VerticalAlignment="Center"/>
</Button>

How can I mimic this in C#? I can't find any method in the Button class that adds children.


回答1:


Button is a Content control so you just have to use the Buttons Content property

Example:

Button myButton = new Button
{
    Width = 24,
    Height = 24,
    Content = new Image
    {
        Source = new BitmapImage(new Uri("image source")),
        VerticalAlignment = VerticalAlignment.Center
    }
};



回答2:


Why don't you add the Image in XAML and bind Source to a property in your view model?



来源:https://stackoverflow.com/questions/15627123/adding-an-image-inside-a-button-programmatically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!