Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. WPF
  4. (beginner) There must be a better way (TextBlock properties)

(beginner) There must be a better way (TextBlock properties)

Scheduled Pinned Locked Moved WPF
wpfcsharpwcfdevopsquestion
3 Posts 2 Posters 10 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Maximilien
    wrote on last edited by
    #1

    I have a TextBlock in a WPF page that will be displayed after an action, it can be a "green" message or a "red" message if an action is successful or not. Currently, I have binding for the color (I also have a property for the text content and the visibility). : (simplified) public string MessageColor { get { if ( someBooleanValue ) return "green"; else return "red"; } } public string Message { get { if ( someBooleanValue ) return "Success Message"; else return "Failure Message"; } } This seems awkward or clumsy. Would it be better to have 2 TextBlock in the XAML with their specific styles and toggle the visibility ?

    CI/CD = Continuous Impediment/Continuous Despair

    Richard DeemingR 1 Reply Last reply
    0
    • M Maximilien

      I have a TextBlock in a WPF page that will be displayed after an action, it can be a "green" message or a "red" message if an action is successful or not. Currently, I have binding for the color (I also have a property for the text content and the visibility). : (simplified) public string MessageColor { get { if ( someBooleanValue ) return "green"; else return "red"; } } public string Message { get { if ( someBooleanValue ) return "Success Message"; else return "Failure Message"; } } This seems awkward or clumsy. Would it be better to have 2 TextBlock in the XAML with their specific styles and toggle the visibility ?

      CI/CD = Continuous Impediment/Continuous Despair

      Richard DeemingR Offline
      Richard DeemingR Offline
      Richard Deeming
      wrote on last edited by
      #2

      You could use a style with triggers to change the TextBlock properties when the view-model property changes:

      // Make sure you raise the "PropertyChanged" event when the value changes:
      public bool SomeBooleanValue
      {
      get { return _someBooleanValue; }
      private set { SetProperty(ref _someBooleanValue, value); }
      }

      <TextBlock>
      <TextBlock.Style>
      <Style TargetType="TextBlock">
      <Style.Triggers>
      <DataTrigger Binding="{Binding Path=SomeBooleanValue}" Value="True">
      <Setter Property="Foreground" Value="Green" />
      <Setter Property="Text" Value="Success Message" />
      </DataTrigger>
      <DataTrigger Binding="{Binding Path=SomeBooleanValue}" Value="False">
      <Setter Property="Foreground" Value="Red" />
      <Setter Property="Text" Value="Failure Message" />
      </DataTrigger>
      </Style.Triggers>
      </Style>
      </TextBlock.Style>
      </TextBlock>

      Trigger, DataTrigger & EventTrigger - The complete WPF tutorial[^]


      "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

      "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

      M 1 Reply Last reply
      0
      • Richard DeemingR Richard Deeming

        You could use a style with triggers to change the TextBlock properties when the view-model property changes:

        // Make sure you raise the "PropertyChanged" event when the value changes:
        public bool SomeBooleanValue
        {
        get { return _someBooleanValue; }
        private set { SetProperty(ref _someBooleanValue, value); }
        }

        <TextBlock>
        <TextBlock.Style>
        <Style TargetType="TextBlock">
        <Style.Triggers>
        <DataTrigger Binding="{Binding Path=SomeBooleanValue}" Value="True">
        <Setter Property="Foreground" Value="Green" />
        <Setter Property="Text" Value="Success Message" />
        </DataTrigger>
        <DataTrigger Binding="{Binding Path=SomeBooleanValue}" Value="False">
        <Setter Property="Foreground" Value="Red" />
        <Setter Property="Text" Value="Failure Message" />
        </DataTrigger>
        </Style.Triggers>
        </Style>
        </TextBlock.Style>
        </TextBlock>

        Trigger, DataTrigger & EventTrigger - The complete WPF tutorial[^]


        "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

        M Offline
        M Offline
        Maximilien
        wrote on last edited by
        #3

        Thanks, looking at it. What is the "SetProperty" ? is this something I have to implement ? Scratch that, it's working. Thanks. :rose:

        CI/CD = Continuous Impediment/Continuous Despair

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups