简体   繁体   中英

Bind ImageAwesome Object in WPF from a ViewModel Property

I'm pretty new to WPF and could really do with some help. Is there anyway to bind an ImageAwesome object ( Font-Awesome ) from a ViewModel property? As it stands, my ViewModel on instantiation creates an ImageAwesome object which is then accessible using the property SpinIcon .

ViewModel

 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");
            }
        }

    }

}

I can bind the individual properties of SpinIcon as shown below but this would cause of a lot of duplication of code which I'm trying to avoid.

UserControl

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

Any help would be much appreciated.

尝试这个:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM