从ViewModel属性绑定WPF中的ImageAwesome对象

时间:2017-08-24 11:33:11

标签: c# wpf mvvm data-binding font-awesome

我对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>

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:1)

试试这个:

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