Igor Meszaros
Igor Meszaros

Reputation: 2127

Textblock style binding

I want to change a textblock color or style in code with a Viewmodel binding. This is what I got:

<TextBlock 
     Visibility="{Binding SubTitleVisibility}" 
     Text="{Binding SubTitle1}" 
     TextWrapping="Wrap" 
     Margin="12,-6,12,0" 
     Style="{Binding SubColor}"/>

Every binding works here except for the style. Though it applies the font size but without a color. (As Queen would say it's a kind of magic)

View model:

    internal DisplayMode Mode
    {
        get
        {
            return _mode;
        }
        set
        {
            //if (value != _type)
            //{
            _mode = value;
            if (value.Equals(DisplayMode.EditMode) && _type.Equals(ItemType.WithIcon))
            {
                _iconVisibility = Visibility.Visible;
                SubTitleVisibility = Visibility.Collapsed;
            }
            else if (_type.Equals(ItemType.WithSubTitle))
            {
                _iconVisibility = Visibility.Collapsed;
                SubTitleVisibility = Visibility.Visible;
            }

            NotifyPropertyChanged("Mode");
            NotifyPropertyChanged("SubColor");
        }
    }

    internal Style SubColor
    {
        get
        {
            Style temp; 
            if (_group != "phone" && _group != "email" && _mode == DisplayMode.ViewMode)
                temp = (Style)App.Current.Resources["PhoneTextNormalStyle"];
            else
                temp = (Style)App.Current.Resources["PhoneTextAccentStyle"];

            return temp;
        }
    }

I found this codesnippet here, but it won't even go into the get method of the SubColor.

Thanks in advance for your help.

Upvotes: 0

Views: 712

Answers (1)

Claus J&#248;rgensen
Claus J&#248;rgensen

Reputation: 26344

Your approach is wrong. You shouldn't attempt to bind a style to your ViewModel, but instead use a Data Template Selector.

Upvotes: 2

Related Questions