2017-08-24 94 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

相關問題