我对WPF很陌生,可以提供一些帮助。
无论如何都要从ViewModel属性绑定ImageAwesome
对象(Font-Awesome)?就目前而言,我的实例化ViewModel创建了一个ImageAwesome
对象,然后可以使用属性SpinIcon
访问该对象。
视图模型
public class DefaultPageViewModel : BaseViewModel
{
private ImageAwesome _spinIcon;
public DefaultPageViewModel()
{
_spinIcon = new ImageAwesome();
_spinIcon.Icon = FontAwesomeIcon.Spinner;
_spinIcon.Height = 10;
}
public ImageAwesome SpinIcon {
get
{
return _spinIcon;
}
set
{
if(value != _spinIcon)
{
_spinIcon = value;
OnPropertyChanged("SpinIcon");
}
}
}
}
我可以绑定SpinIcon
的各个属性,如下所示,但这会导致很多重复的代码,我试图避免。
用户控件
<UserControl.Resources>
<default:DefaultPageViewModel x:Key="DefaultVM" />
<SolidColorBrush x:Key="ImageBrush" Color="LightBlue" />
</UserControl.Resources>
<Grid>
<fa:ImageAwesome Icon="{Binding SpinIcon.Icon, Source={StaticResource DefaultVM}}" />
</Grid>
</UserControl>
非常感谢任何帮助。
答案 0 :(得分:1)
试试这个:
<ContentControl Content="{Binding SpinIcon, Source={StaticResource DefaultVM}}" />