(beginner) There must be a better way (TextBlock properties)
-
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
-
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
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
-
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
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