gpt4 book ai didi

c# - 如何绑定(bind)图像源?

转载 作者:太空狗 更新时间:2023-10-30 00:43:16 24 4
gpt4 key购买 nike

这是我目前所拥有的:

<Image Source="{Binding ImageSource"} />

<Button Content"Text" ImageSource="path/image.png" />

我知道这里有些不对劲。我想我看不到 ImageSource 的定义位置。

我有几个这样的按钮,只想为每个按钮设置一个独特的图像。我有一个正在使用的按钮模板,它非常适合文本。

<Label Content="TemplateBinding Content" />

感谢您的帮助!

最佳答案

在您的情况下,这可能非常简单!

将图像作为资源添加到项目中,然后在 XAML 中使用如下内容:

<Button HorizontalAlignment="Left" Margin="20,0,0,20" VerticalAlignment="Bottom" Width="50" Height="25">
<Image Source="image.png" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Margin="0">
</Image>
</Button>

或者,更复杂的方式:

如果你使用MVVM模式,你可以做以下事情

在您的 XAML 中:

<Button Focusable="False" Command="{Binding CmdClick}" Margin="0">
<Image Source="{Binding ButtonImage}" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Margin="0">
</Image>
</Button>

在您的 ViewModel 中:

private Image buttonImage;

public Image ButtonImage
{
get
{
return buttonImage;
}
}

在 ViewModel 的构造函数或它的初始化中的某处:

BitmapImage src = new BitmapImage();
src.BeginInit();
src.UriSource = new Uri("image.png", UriKind.Relative);
src.CacheOption = BitmapCacheOption.OnLoad;
src.EndInit();

buttonImage = new Image();
buttonImage.Source = src;

关于c# - 如何绑定(bind)图像源?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11745696/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com