Underline text in WPF

I have a WPF user control that has a TextBox. I set the text Underlineor not Strikethroughor not.

I think that something is wrong with my code, especially with the insecure part, because it does not work properly every time ... maybe some Underline elements are deleted using Strikethrough? ...

Especially if you set the same property several times to the same value, it adds and adds ....

private void UpdateUnderline()
{
    if (this.IsUnderline)
    {
        this.MyTextBlock.TextDecorations.Add(TextDecorations.Underline);
    }
    else
    {
        foreach (var item in TextDecorations.Underline)
        {
            this.MyTextBlock.TextDecorations.Remove(item);
        }
    }
}

private void UpdateStrikethrough()
{
    if (this.IsStrikethrough)
    {
        this.MyTextBlock.TextDecorations.Add(TextDecorations.Strikethrough);
    }
    else
    {
        foreach (var item in TextDecorations.Strikethrough)
        {
            this.MyTextBlock.TextDecorations.Remove(item);
        }
    }
}

as i can't do

this.MyTextBlock.TextDecorations.Contains(TextDecorations.Strikethrough);

he adds and adds, and adds again, so when he removes ... he stays :)

Example:

<Window x:Class="WpfApplication10.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <StackPanel>
            <CheckBox Content="Underline"
                      IsChecked="{Binding IsUnderline}" />
            <CheckBox Content="Strikethrough"
                      IsChecked="{Binding IsStrikethrough}" />
            <TextBlock Name="MyTextBlock"
                       Text="Quick Brown Fox Jumps Over The Lazy Dog." />
            <Button Content="Underline" Name="Underline" Click="Underline_Click"/>
            <Button Content="Strikethrough" Name="Strikethrough" Click="Strikethrough_Click"/>
            <Button Content="NO Underline" Name="NoUnderline" Click="NoUnderline_Click"/>
            <Button Content="NO Strikethrough" Name="NoStrikethrough" Click="NoStrikethrough_Click" />
        </StackPanel>
    </Grid>
</Window>

CS:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        DataContext = this;
    }

    private Boolean _IsUnderline;
    public Boolean IsUnderline
    {
        get
        {
            return _IsUnderline;
        }
        set
        {
            _IsUnderline = value;
            UpdateUnderline();
        }
    }
    private Boolean _IsStrikethrough;
    public Boolean IsStrikethrough
    {
        get
        {
            return _IsStrikethrough;
        }
        set
        {
            _IsStrikethrough = value;
            UpdateStrikethrough();
        }
    }

    private void UpdateUnderline()
    {
        if (this.IsUnderline)
        {
            this.MyTextBlock.TextDecorations.Add(TextDecorations.Underline);
        }
        else
        {
            foreach (var item in TextDecorations.Underline)
            {
                this.MyTextBlock.TextDecorations.Remove(item);
            }
        }
    }

    private void UpdateStrikethrough()
    {
        if (this.IsStrikethrough)
        {
            this.MyTextBlock.TextDecorations.Add(TextDecorations.Strikethrough);
        }
        else
        {
            foreach (var item in TextDecorations.Strikethrough)
            {
                this.MyTextBlock.TextDecorations.Remove(item);
            }
        }
    }

    private void Underline_Click(object sender, RoutedEventArgs e)
    {
        IsUnderline = true;
    }

    private void Strikethrough_Click(object sender, RoutedEventArgs e)
    {
        IsStrikethrough = true;
    }

    private void NoUnderline_Click(object sender, RoutedEventArgs e)
    {
        IsUnderline = false;
    }

    private void NoStrikethrough_Click(object sender, RoutedEventArgs e)
    {
        IsStrikethrough = false;
    }
}
+3
source share
2 answers

dirty solution:

    private void UpdateUnderline()
    {
        if (this.IsUnderline)
        {
            foreach (var item in TextDecorations.Underline)
            {
                this.MyTextBlock.TextDecorations.Remove(item);
            }
            this.MyTextBlock.TextDecorations.Add(TextDecorations.Underline);
        }
        else
        {
            foreach (var item in TextDecorations.Underline)
            {
                this.MyTextBlock.TextDecorations.Remove(item);
            }
        }
    }
+2

:

:

XAML:

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow"
        Height="350"
        Width="525">
    <Grid>
        <StackPanel>
            <CheckBox Content="Underline"
                      IsChecked="{Binding IsUnderline}" />
            <CheckBox Content="Strikethrough"
                      IsChecked="{Binding IsStrikethrough}" />
            <TextBlock Name="MyTextBlock"
                       Text="Quick Brown Fox Jumps Over The Lazy Dog." />
        </StackPanel>
    </Grid>
</Window>

:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        DataContext = this;
    }

    private Boolean _IsUnderline;
    public Boolean IsUnderline
    {
        get
        {
            return _IsUnderline;
        }
        set
        {
            _IsUnderline = value;
            UpdateUnderline();
        }
    }
    private Boolean _IsStrikethrough;
    public Boolean IsStrikethrough
    {
        get
        {
            return _IsStrikethrough;
        }
        set
        {
            _IsStrikethrough = value;
            UpdateStrikethrough();
        }
    }

    private void UpdateUnderline()
    {
        if (this.IsUnderline)
        {
            this.MyTextBlock.TextDecorations.Add(TextDecorations.Underline);
        }
        else
        {
            foreach (var item in TextDecorations.Underline)
            {
                this.MyTextBlock.TextDecorations.Remove(item);
            }
        }
    }

    private void UpdateStrikethrough()
    {
        if (this.IsStrikethrough)
        {
            this.MyTextBlock.TextDecorations.Add(TextDecorations.Strikethrough);
        }
        else
        {
            foreach (var item in TextDecorations.Strikethrough)
            {
                this.MyTextBlock.TextDecorations.Remove(item);
            }
        }
    }
}
0

Source: https://habr.com/ru/post/1780931/


All Articles