Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从ViewModel属性绑定WPF中的ImageAwesome对象_C#_Wpf_Mvvm_Data Binding_Font Awesome - Fatal编程技术网

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

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()

我是WPF的新手,需要一些帮助。 是否仍然可以从ViewModel属性绑定
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}}" />

我不知道你能做到!非常好,谢谢!