gpt4 book ai didi

.net - WPF 中的下划线文本

转载 作者:行者123 更新时间:2023-12-02 02:29:09 24 4
gpt4 key购买 nike

我有一个带有文本框的 WPF 用户控件。我设置文本 Underline 与否,Strikethrough 与否。

我认为我的代码有问题,尤其是 unset 部分,因为每次都不能正常工作...可能是下划线的某些元素被删除线删除了?...

Especiallly 当多次设置相同的属性为相同的值时,它添加并添加....

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);
}
}
}

我做不到

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

它添加并添加,然后再次添加,所以当它删除时...它仍然存在 :)

示例:

<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;
}
}

最佳答案

“肮脏”的解决方案:

    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);
}
}
}

关于.net - WPF 中的下划线文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4474123/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com