logo
down
shadow

Apply a property for a part of text of textblock in WPF


Apply a property for a part of text of textblock in WPF

By : user2954775
Date : November 22 2020, 10:31 AM
may help you . i want , when a part of text of textblock is 'Thomas' , around the text be blue. , You need a converter:
code :
public class StringPropertyContainsThomasConverter : IValueConverter {
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture) {
        if(value != null) {
            if(value.ToString().Contains("Thomas")) return Brushes.Blue; //replace with whatever color you want
        }
        return Brushes.White;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) {
        throw new NotImplementedException();
    }
}
<Window.Resources>
    <local:StringPropertyContainsThomasConverter x:Key="StringPropertyContainsThomasConverter"/>
</Window.Resources>
<TextBlock Background="{Binding RelativeSource={RelativeSource self},
                                Path=Text,
                                Converter={StaticResources StringPropertyContainsThomasConverter}}"/>


Share : facebook icon twitter icon
How to data bind the text property of a TextBlock to the text property of a TextBox

How to data bind the text property of a TextBlock to the text property of a TextBox


By : Dich Dinh
Date : March 29 2020, 07:55 AM
With these it helps For that XAML to work, your "page one" will need to be set as the data context of page two, with the boxSource variable defined as a property, so that in the setter, you can raise the PropertyChanged event.
Matthias is right, though, this is a pretty brittle way to implement this, and one of the places where an MVVM approach will be more robust in the long run.
How to bind a textblock visibility to another textblock text property

How to bind a textblock visibility to another textblock text property


By : Jenoth
Date : March 29 2020, 07:55 AM
will be helpful for those in need , Using Converters You can achieve this
In xaml
code :
 <TextBlock x:Name="TB" Text="Text"/>
 <TextBox Visibility="{Binding ElementName=TB,Path=Text,Converter={StaticResource StringToVisibilityConverter}}"/>
 public class StringToVisibilityConverter: IValueConverter
{
    #region IValueConverter Members

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return string.IsNullOrEmpty((string)value)?Visibility.Collapsed:Visibility.Visible;

    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return (Visibility)value == Visibility.Visible;
    }

    #endregion
}
Apply italic effect in the part of Text property of Label in Xamarin.forms

Apply italic effect in the part of Text property of Label in Xamarin.forms


By : Markus
Date : March 29 2020, 07:55 AM
wish of those help This is my snippet in Xamarin.forms. , I solved it.
code :
var fs = new FormattedString ();
fs.Spans.Add (new Span { Text="Italic", ForegroundColor = Color.Gray, FontSize = 20, FontAttributes = FontAttributes.Italic });
fs.Spans.Add (new Span { Text=", Bold", ForegroundColor = Color.Gray, FontSize = 20, FontAttributes = FontAttributes.Bold });
labelFormatted.FormattedText = fs;
Binding string property to textblock and apply custom date format

Binding string property to textblock and apply custom date format


By : Harsh Sheth
Date : March 29 2020, 07:55 AM
wish helps you You need to add an IValueConverter to change your string object to DateTime. Something like this.
code :
public class StringToDateValueConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return DateTime.Parse(value.ToString());
        }
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
<Window x:Class="WpfApplication4.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApplication4"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Window.DataContext>
        <local:ViewModel />
    </Window.DataContext>
    <Window.Resources>
        <local:StringToDateValueConverter x:Key="converter" />
    </Window.Resources>
    <Grid>
        <TextBlock Grid.Row="0" Grid.Column="2" HorizontalAlignment="Right" Margin="25,5,0,0" Text="{Binding alertTimeStamp, Converter={StaticResource converter}, StringFormat=dd MMM yyyy hh:mm:ss tt}"></TextBlock>
    </Grid>
</Window>
How do I set the Foreground property of a TextBlock by TextBlock text value?

How do I set the Foreground property of a TextBlock by TextBlock text value?


By : 陳偉佳
Date : March 29 2020, 07:55 AM
fixed the issue. Will look into that further If you want the flexibility to do something smart, such as dynamically map texts to colors and so on, you could use a Converter class. I am assuming the text is set to bind to something, you could bind to the same something in Foreground but through a custom converter:
shadow
Privacy Policy - Terms - Contact Us © ourworld-yourmove.org