C# 从ViewModel属性绑定WPF中的ImageAwesome对象
我是WPF的新手,需要一些帮助。 是否仍然可以从ViewModel属性绑定C# 从ViewModel属性绑定WPF中的ImageAwesome对象,c#,wpf,mvvm,data-binding,font-awesome,C#,Wpf,Mvvm,Data Binding,Font Awesome,我是WPF的新手,需要一些帮助。 是否仍然可以从ViewModel属性绑定ImageAwesome对象()?目前,我的ViewModel在实例化时创建了一个ImageAwesome对象,然后可以使用属性SpinIcon访问该对象 视图模型 public class DefaultPageViewModel : BaseViewModel { private ImageAwesome _spinIcon; public DefaultPageViewModel()
ImageAwesome
对象()?目前,我的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");
}
}
}
}
<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>
我可以绑定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");
}
}
}
}
<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>
任何帮助都将不胜感激。试试以下方法:
<ContentControl Content="{Binding SpinIcon, Source={StaticResource DefaultVM}}" />
我不知道你能做到!非常好,谢谢!