我有一个带有TextBox的WPF用户控件。我是否设置了文本Underline
,不设置Strikethrough
。
我认为我的代码有问题,特别是未设置部分,因为每次都无法正常工作...可能会删除下划线的某些元素并使用删除线?...
特别是当将相同的属性多次设置为相同的值时,它会添加并添加....
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;
}
}
答案 0 :(得分:2)
“脏”解决方案:
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);
}
}
}
答案 1 :(得分:0)
我重新创建它并且工作正常:
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);
}
}
}
}