locked
Unsetting a Property Value to a Value From a Style

    Question

  • I have a scenario in which I have a collection of TextBlocks, all of which I initialize with a Style from my app's resources. During the life of the app, individual TextBlocks will have their Foreground property changed. Assigning a new value to the property is easy, but how do I "unassign" it? In other words, I want to be able to undo the assignment at some point so that the TextBox uses the value from the Style again. I could, of course, just assign to it the value that is in the Style, but that would mean changing the code every time I decided to use a different color. Any ideas? Thanks.

    Nathan Sokalski njsokalski@hotmail.com http://www.nathansokalski.com/

    Tuesday, May 06, 2014 10:31 PM

Answers

  • Hi, if you use storyboard to set property, the property value will not override Style. and when you stop storyboard, the property will be back to Styles automatically. The key point is using storyboard to change it.

    I just noticed that you use VB.NET, so you can try this:

    Me.txtMessage.ClearValue(TextBlock.ForegroundProperty)
    it will also clear the local value and change back to Styles.




    Wednesday, May 07, 2014 4:06 PM

All replies

  • Hi, you can use Storyboard to change color, and when stop the Storyboard, the color will be changed back ("unassign") automatically. below is the example:

    <Page
        x:Class="App1.MainPage"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="using:App1"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d">
    
        <StackPanel Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
            <TextBlock Name="textblock" Text="textblock">
                <TextBlock.Style>
                    <Style TargetType="TextBlock">
                        <Setter Property="Foreground" Value="Red"/>
                    </Style>
                </TextBlock.Style>
            </TextBlock>
            <Button Content="change color" Click="Button_Click_1"/>
            <Button Content="change color back" Click="Button_Click_2"/>
            
        </StackPanel>
    </Page>
        public sealed partial class MainPage : Page
        {
            public MainPage()
            {
                this.InitializeComponent();          
            }
    
            private void ChangeColor(TextBlock txt, Color color)
            {
                Storyboard board = new Storyboard();
                ColorAnimation ani = new ColorAnimation();
                ani.To = color;
                ani.Duration = new Duration(new TimeSpan(0));
                Storyboard.SetTarget(ani, txt);
                Storyboard.SetTargetProperty(ani, "(TextBlock.Foreground).Color");
                board.Children.Add(ani);
                board.Begin();
                txt.Tag = board;
            }
    
            private void ChangeColorBack(TextBlock txt)
            {
                ((Storyboard)txt.Tag).Stop();
            }
    
            private void Button_Click_1(object sender, RoutedEventArgs e)
            {
                ChangeColor(textblock, Colors.Green);
            }
    
            private void Button_Click_2(object sender, RoutedEventArgs e)
            {
                ChangeColorBack(textblock);
            }
        }



    • Edited by lapheal Wednesday, May 07, 2014 4:27 AM
    Wednesday, May 07, 2014 4:26 AM
  • I think you have misunderstood my question. I am not asking how to change the color back to the original value, I am asking how to remove it so that it is as if I had never set it. Take a look at the following two XAML TextBoxes:
    <TextBlock Name="txtMessage" Style="{StaticResource MyTextBlockStyle}"/>
    <TextBlock Name="txtMessage" Style="{StaticResource MyTextBlockStyle}" Foreground="Blue"/>
    The first one is what my XAML looks like, and notice that it will use the Foreground value that I have set in the Style MyTextBlockStyle. The second one is what it would look like after the following line of code is executed:
    Me.txtMessage.Foreground = New SolidColorBrush(Colors.Blue)
    Notice that it will now use a Foreground value of Blue, since explicitly set properties override Styles. What I want to be able to do is change it back to it's original form, basically saying "Start using the Foreground value in MyTextBlockStyle again" by undoing the assignment I made in the code.

    Nathan Sokalski njsokalski@hotmail.com http://www.nathansokalski.com/

    Wednesday, May 07, 2014 3:54 PM
  • Hi, if you use storyboard to set property, the property value will not override Style. and when you stop storyboard, the property will be back to Styles automatically. The key point is using storyboard to change it.

    I just noticed that you use VB.NET, so you can try this:

    Me.txtMessage.ClearValue(TextBlock.ForegroundProperty)
    it will also clear the local value and change back to Styles.




    Wednesday, May 07, 2014 4:06 PM