所附的屏幕截图应该能让您清楚地了解我在说什么。我编写了一个小型 wpf 应用程序,可以使用我选择的任何图像创建“明信片”。然后,它可以将带有或不带有文本的图像保存为 jpg 格式,然后通过电子邮件发送。我采取以下步骤:
当文本框应该是唯一受影响的控件时,标签中的字体粗细和所有 3 个按钮都会变为粗体。只有粗细发生了变化——字体名称、大小和颜色保持不变。 这让我疯狂。想不通。我已经包含了我认为相关的代码。
来自表单的 xaml:
<UserControl x:Class="PostcardMVVM.Views.EditView" Style="{StaticResource ControlStyle}"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:PostcardMVVM.ViewModel"
mc:Ignorable="d" Width="800" Height="640" Loaded="UserControl_Loaded">
<!--<UserControl.DataContext>
<editModel:EditViewModel/>
</UserControl.DataContext>-->
<!--<Grid>
<Border Margin="68,67,60,67" BorderBrush="Black" BorderThickness="1" CornerRadius="10">
<Border.Effect>
<DropShadowEffect/>
</Border.Effect>
<Rectangle Fill="White" Stroke="Black" Margin="37,89,118,98" />
</Border>
</Grid>-->
<Grid>
<Grid.Effect>
<DropShadowEffect Color="SlateGray" BlurRadius="10"/>
</Grid.Effect>
<Canvas x:Name="mainGrid" Margin="0,0,0,200" Background="White">
<Image x:Name="imgvImage" Height="400" Width="400" Canvas.Left="255" Canvas.Top="20"/>
<TextBlock x:Name="tblkText" IsEnabled="False" HorizontalAlignment="Center" TextWrapping="Wrap" Height="210" Canvas.Left="20" Canvas.Top="20"
VerticalAlignment="Top" Foreground="{Binding ElementName=txtText, Path=Foreground}" Width="400" Text="{Binding ElementName=txtText, Path=Text}" Padding="10, 3, 10, 3"
FontFamily="{Binding ElementName=txtText, Path=FontFamily}" FontSize="{Binding ElementName=txtText, Path=FontSize}" FontWeight="{Binding ElementName=txtText, Path=FontWeight}"
RenderTransformOrigin="0.238,-2.2" Background="Transparent">
<TextBlock.Effect>
<DropShadowEffect ShadowDepth="2" Color="Black" BlurRadius="3" ></DropShadowEffect>
</TextBlock.Effect>
<TextBlock.LayoutTransform >
<RotateTransform Angle = "-90" ></RotateTransform >
</TextBlock.LayoutTransform >
</TextBlock>
</Canvas>
<Canvas>
<!--<Canvas.Resources>
</Canvas.Resources>-->
<StackPanel Orientation="Horizontal" HorizontalAlignment="Center" Height="115" Canvas.Top="440" VerticalAlignment="Top" >
<Label Margin="10,30,0,30" TabIndex="20" x:Name="Label1" VerticalContentAlignment="Center" Content="Enter Text:"
Style="{Binding Source={StaticResource LabelStyle}}" RenderTransformOrigin="0.511,-2.5"/>
<TextBox x:Name="txtText" TabIndex="0" Width="200" VerticalContentAlignment="Top" MinLines="1"
MaxLines="3" AcceptsReturn="True" TextWrapping="Wrap" Margin="40,10,0,10"
BorderBrush="Black" BorderThickness="1" ToolTip="Type in message and hit enter to save postcard" KeyUp="txtText_KeyUp"/>
<Button x:Name="btnFont" Content="Font" Width="75" Margin="45,35" Click="btnFont_Click"
Style="{Binding Source={StaticResource ButtonStyle}}" IsDefault="True" Height="40" />
</StackPanel>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Center" Height="50" Canvas.Top="570" VerticalAlignment="Top">
<Button Margin="10,0,0,0" Height="40" x:Name="btnEmail" Width="75" Content="Email" IsEnabled="false" Style="{Binding Source={StaticResource ButtonStyle}}" />
<Button Margin="10,0,0,0" Height="40" x:Name="btnCancel" Width="75" Content="Cancel" Click="btnCancel_Click" Style="{Binding Source={StaticResource ButtonStyle}}"/>
</StackPanel>
</Canvas>
</Grid>
背后代码:
private void btnFont_Click(object sender, RoutedEventArgs e)
{
FontDialog fontDialog1 = new FontDialog(); // try setting initial color here
fontDialog1.ShowColor = true;
var result = fontDialog1.ShowDialog();
if (result == System.Windows.Forms.DialogResult.OK)
{
System.Drawing.Color fc = fontDialog1.Color;
txtText.FontFamily = new swm.FontFamily(fontDialog1.Font.Name);
txtText.FontSize = fontDialog1.Font.Size;
txtText.FontWeight = FontWeight = fontDialog1.Font.Bold ? FontWeights.Bold : FontWeights.Regular;
txtText.Foreground = new SolidColorBrush(Color.FromArgb(fc.A, fc.R, fc.G, fc.B));
//tblkText.Foreground = new SolidColorBrush(Color.FromArgb(fc.A, fc.R, fc.G, fc.B));
var bindingExpression = tblkText.GetBindingExpression(TextBlock.TextProperty);
bindingExpression.UpdateSource();
bindingExpression = tblkText.GetBindingExpression(TextBlock.FontFamilyProperty);
bindingExpression.UpdateSource();
bindingExpression = tblkText.GetBindingExpression(TextBlock.FontSizeProperty);
bindingExpression.UpdateSource();
bindingExpression = tblkText.GetBindingExpression(TextBlock.FontWeightProperty);
bindingExpression.UpdateSource();
bindingExpression = tblkText.GetBindingExpression(TextBlock.ForegroundProperty);
bindingExpression.UpdateSource();
//(FindResource("LabelStyle") as Style).Resources.;
//(FindResource("LabelStyle") as Style).; // as Label).ref ();
bindingExpression = Label1.GetBindingExpression(Label.StyleProperty);
bindingExpression.UpdateSource();
bindingExpression = btnFont.GetBindingExpression(Button.StyleProperty);
bindingExpression.UpdateSource();
bindingExpression = btnEmail.GetBindingExpression(Button.StyleProperty);
bindingExpression.UpdateSource();
bindingExpression = btnCancel.GetBindingExpression(Button.StyleProperty);
bindingExpression.UpdateSource();
}
}
app.xaml:
<Application x:Class="PostcardMVVM.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:PostcardMVVM"
StartupUri="MainWindow.xaml">
<Application.Resources>
<Style x:Key="ControlStyle" TargetType="{x:Type UserControl}">
<Setter Property="FontSize" Value="18" />
<Setter Property="FontFamily" Value="Calibri" />
<Setter Property="FontStyle" Value="Normal" />
</Style>
<Style x:Key="ButtonStyle" TargetType="{x:Type Button}">
<Setter Property="FontSize" Value="18" />
<Setter Property="FontFamily" Value="Calibri" />
<Setter Property="FontStyle" Value="Normal" />
</Style>
<Style x:Key="LabelStyle" TargetType="{x:Type Label}">
<Setter Property="FontSize" Value="18" />
<Setter Property="FontFamily" Value="Calibri" />
<Setter Property="FontStyle" Value="Normal" />
</Style>
</Application.Resources>
正确状态下明信片表格的屏幕截图
这一行:
txtText.FontWeight = FontWeight = fontDialog1.Font.Bold ? FontWeights.Bold : FontWeights.Regular;
有一个额外的
FontWeight=
,它将把父窗体设置为粗体。它的子控件,包括您的按钮,将继承它的粗体。
摆脱无关的设置器应该可以解决这个问题。