2017-08-24 92 views
0

我对WPF很新,可以真正做一些帮助。 反正有绑定ImageAwesome对象(Font-Awesome)从ViewModel属性吗?现在,我的ViewModel在实例化时会创建一个ImageAwesome对象,然后使用属性SpinIcon访问该对象。从ViewModel属性中绑定WPF中的ImageAwesome对象

视图模型

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> 

任何帮助将非常感激。

回答

1

试试这个:

<ContentControl Content="{Binding SpinIcon, Source={StaticResource DefaultVM}}" /> 
+0

我不知道你能做到这一点!非常感谢你! – Kitson88

相关问题