Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9082689
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T20:37:04+00:00 2026-06-16T20:37:04+00:00

Early start to the new year on SO for me :) I’m trying to

  • 0

Early start to the new year on SO for me 🙂

I’m trying to help out a friends with what I thought would be a simple thing. Basically we just want to change a style at runtime in code and update the style for a TextBlock.

I had no problem making this work with any other type of element, except the TextBlock. I’m now very curious if I’ve missed something here, or if indeed there is a bug. What would be the nicest way to solve this?

The code here is just for demonstration, it works with TextBox but not TextBlock (when targettype etc is changed of course)

Style defined in a resourcedictionary called StandardStyles, under the Common folder

    <Style x:Key="textStyle" TargetType="TextBlock">
    <Setter Property="Foreground" Value="red"/>
    <Setter Property="FontFamily" Value="Segoe UI"/>
</Style>

The UI

    <StackPanel Orientation="Horizontal">
    <ListBox ItemsSource="{Binding Fonts}" Height="300" Width="300" SelectionChanged="ListBox_SelectionChanged_1"></ListBox>
    <Border BorderBrush="White" BorderThickness="5" Padding="20,0,0,0" Height="300" Width="300">
        <TextBlock Text="Hi here is some text" Style="{Binding FontStyleText}"/>
    </Border>
</StackPanel>

The code

    public sealed partial class MainPage : INotifyPropertyChanged 
{
    private Style _fontStyleText;
    public Style FontStyleText
    {
        get
        {
            return this._fontStyleText;
        }

        set
        {
            if (value == this._fontStyleText) return;
            this._fontStyleText = value;
            NotifyPropertyChanged();
        }
    }

    private List<string> _fonts;
    public List<string> Fonts
    {
        get
        {
            return this._fonts;
        }

        set
        {
            if (value == this._fonts) return;
            this._fonts = value;
            NotifyPropertyChanged();
        }
    }


    public MainPage()
    {
        this.InitializeComponent();
        DataContext = this;

        Fonts = new List<string> {"Segoe UI", "Showcard Gothic", "Arial"};

        FontStyleText = Application.Current.Resources["textStyle"] as Style;

    }

    private void ListBox_SelectionChanged_1(object sender, Windows.UI.Xaml.Controls.SelectionChangedEventArgs e)
    {
        var font = (sender as ListBox).SelectedItem as string;

        var res = new ResourceDictionary()
        {
            Source = new Uri("ms-appx:///Common/StandardStyles.xaml", UriKind.Absolute)
        };

        var style = res["textStyle"] as Style;

        style.Setters.RemoveAt(0); // if it is the first item otherwise for more accurat removal se below :D


        foreach (var item in style.Setters.Cast<Setter>().Where(item => item.Property == FontFamilyProperty))
        {
            style.Setters.Remove(item);
        }

        style.Setters.Add(new Setter(FontFamilyProperty, new FontFamily(font)));
        style.Setters.Add(new Setter(ForegroundProperty, new SolidColorBrush(Colors.Purple)));

        FontStyleText = style;

    }

    public event PropertyChangedEventHandler PropertyChanged;
    private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T20:37:05+00:00Added an answer on June 16, 2026 at 8:37 pm

    Oki, so I figured out what was wrong.

    A big thank you to mlorbetske for that (and Matt on skype), I noticed something in the piece of code that I was missing. And yes, happy to say it was not a bug, but (as most of the time) something missing. Not getting an error message is a bug I would say.

    I was lacking the right dependecyproperty – the target property was set wrong. So instead of:

    style.Setters.Add(new Setter(FontFamilyProperty, new FontFamily(font)));
    

    I had to

    style.Setters.Add(new Setter(TextBlock.FontFamilyProperty, new FontFamily(font)));
    

    That’s it 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working on early designs for an application that needs to start out small
I'm fairly new to backbone and I'm trying to build a simple app. This
I'm trying to get the paragraph text to start on a new line after
This is just an early stage test of creating and writing a file to
In the early days of the iPhone development apps are often kicked out of
I am looking to start work on a brand-new project, something I've been thinking
I am going to start on with a new project. I need to deal
been trying to figure this problem out for about 5 hours but cant seem
I'm going to start a new WPF project and I'm considering to implement it
I have the code: var timer:Timer = new Timer(milliseconds, repititions); timer.addEventListener(TimerEvent.TIMER, callback); timer.start(); and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.