本文介绍了按钮样式与TextBlock样式冲突?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我的'默认'按钮有自定义样式,还为TextBlocks创建了自定义样式。如果我完全删除TextBlock样式,Button样式和鼠标悬停触发器工作正常,但是一旦按钮的样式添加了TextBlock样式,则的长度不再有效。 我正在使用Expression Blend 4--另一个奇怪的事情是Blend中的预览看起来很好,但是当我运行应用程序时,按钮样式在默认状态下是不正确的。 (如果我在TextBlock上没有设置任何样式的情况下运行应用程序,那么 Button样式工作正常。) 我有一个'样本'项目在这里展示这个(在Blend 4中制作): http://www.megaupload.com/?d=NG7CL1Q5 此外,我在其他网站上发布了一些屏幕截图和示例代码: http://stackoverflow.com/q/4484313/313366 < 应用程序 xmlns = " http://schemas.microsoft.com/winfx/2006/xaml/presentation " xmlns:x = " http://schemas.microsoft.com/winfx/2006/xaml " x:Class = " ButtonTest.App " StartupUri = " MainWindow.xaml " > < Application.Resources > <! - 应在此处定义应用程序级别的资源。 - > < ResourceDictionary > < 样式 TargetType = " {x:Type Button} " > < Style.Triggers > < 触发器 属性 = " IsMouseOver " 值 = " True " > < Setter 属性 = " 前景 " > < Setter.Value > < LinearGradientBrush StartPoint = " 0.5,0 " EndPoint = " 0.5,1 " > < GradientStop 颜色 = " 白色 " Offset = " 0 " /> < GradientStop 颜色 = " #FFFDFF00 " Offset = " 1 " /> < / LinearGradientBrush > < / Setter.Value > < / Setter > < Setter 属性 = " RenderTransformOrigin " 值 = " 0.5,0.5 " /> < Setter 属性 = " RenderTransform " > < Setter.Value > < TransformGroup > < ScaleTransform ScaleY = " 1.20 " ScaleX = " 1.20 " /> < SkewTransform /> < RotateTransform /> < TranslateTransform /> < / TransformGroup > < / Setter.Value > < / Setter > < / 触发 > < / Style.Triggers > < Setter 属性 = " 模板 " > < Setter.Value > < ControlTemplate TargetType = " {x :类型按钮} " > < 网格 > < ContentPresenter RecognizesAccessKey = " True " SnapsToDevicePixels = " {TemplateBinding SnapsToDevicePixels} " > < ContentPresenter.Effect > < DropShadowEffect BlurRadius = " 3 " ShadowDepth = " 4 " /> < / ContentPresenter.Effect > < / ContentPresenter > < / 网格 > < ControlTemplate.Triggers > < 触发器 属性 = " IsFocused " Value = " True " /> < 触发器 属性 = " IsDefaulted " 值 = " ; True " /> < 触发器 属性 = " IsMouseOver " 值 = " True " /> < 触发器 属性 = " IsPressed " 值 = " True " /> < 触发器 属性 = " IsEnabled " 值 = " False " /> < / ControlTemplate.Triggers > < / ControlTemplate > < / Setter.Value > < / Setter > < Setter 属性 = " FontFamily " 值 = " / ButtonTest;组件/字体/ #Segoe打印 " /> < Setter 属性 = " FontWeight " 值 = " Bold " /> < Setter 属性 = " FontSize " 值 = " 18 " /> < Setter 属性 = " 前景 " 值 = " 白色 " /> < Setter 属性 = " HorizontalAlignment " Value = " 中心 " /> < Setter 属性 = " VerticalAlignment " Value = " 中心 " /> < Setter 属性 = " 背景 " 值 = " {x:Null} " /> < / 样式 > <! - 如果我删除此S tyle块下面,它工作正常。 - > < 样式 TargetType = " {x:键入TextBlock} " > < Setter = " TextWrapping " 值 = " NoWrap " /> < Setter 属性 = " TextTrimming " 值 = " 无 " /> < Setter 属性 = " 前景 " 值 = " White " /> < ; Setter 属性 = " FontSize " Value = " 16 " /> < Setter 属性 = " 效果 " > < Setter.Value > < DropShadowEffect BlurRadius = " 3 " ShadowDepth = " 4 " /> < / Setter。值 > < / Setter > < Setter 属性 = " FontFamily " 值 = " / ButtonTest; component / Fonts /#Segoe Print " ; /> < / 样式 > < / ResourceDictionary > < / Application.Resources > < / 应用程序 > 解决方案 该按钮正在处理您的内容并将其放入一个文本块中来自app.xaml的文本块的样式。如果将文本块中的前景属性更改为红色和更大的文本,则很容易看到。这些值也会对按钮的文本块应用。 一些解决方法: 1。在您的按钮中放置一个明确的文本块,并且不应用任何样式: < 按钮 边距 = " 272,192,277,0 " VerticalAlignment = " 顶部 " > < TextBlock 文字 = " 按钮 " 样式 = " {x:Null} " /> < / 按钮 > I have a custom style for my 'default' Buttons, and also created a custom style for TextBlocks. If I remove the TextBlock style entirely, the Button style and mouseover trigger works fine, but once the TextBlock styling is added in the Button's style no longer works. I'm using Expression Blend 4-- and also another odd thing is that the preview in Blend looks fine, but when I RUN the application, the button styles are incorrect in their default state. (If I run the application without any style set on TextBlock at all however, the Button style works fine.)I have a 'sample' project demonstrating this here (made in Blend 4): http://www.megaupload.com/?d=NG7CL1Q5Also I posted some screenshots and sample code on another site here: http://stackoverflow.com/q/4484313/313366 <Applicationxmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"x:Class="ButtonTest.App"StartupUri="MainWindow.xaml"><Application.Resources><!-- Resources scoped at the Application level should be defined here. --><ResourceDictionary> <Style TargetType="{x:Type Button}"> <Style.Triggers> <Trigger Property="IsMouseOver" Value="True"> <Setter Property="Foreground"> <Setter.Value> <LinearGradientBrush StartPoint="0.5,0" EndPoint="0.5,1"> <GradientStop Color="White" Offset="0"/> <GradientStop Color="#FFFDFF00" Offset="1"/> </LinearGradientBrush> </Setter.Value> </Setter> <Setter Property="RenderTransformOrigin" Value="0.5,0.5"/> <Setter Property="RenderTransform"> <Setter.Value> <TransformGroup> <ScaleTransform ScaleY="1.20" ScaleX="1.20"/> <SkewTransform/> <RotateTransform/> <TranslateTransform/> </TransformGroup> </Setter.Value> </Setter> </Trigger> </Style.Triggers> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type Button}"> <Grid> <ContentPresenter RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"> <ContentPresenter.Effect> <DropShadowEffect BlurRadius="3" ShadowDepth="4"/> </ContentPresenter.Effect> </ContentPresenter> </Grid> <ControlTemplate.Triggers> <Trigger Property="IsFocused" Value="True"/> <Trigger Property="IsDefaulted" Value="True"/> <Trigger Property="IsMouseOver" Value="True"/> <Trigger Property="IsPressed" Value="True"/> <Trigger Property="IsEnabled" Value="False"/> </ControlTemplate.Triggers> </ControlTemplate> </Setter.Value> </Setter> <Setter Property="FontFamily" Value="/ButtonTest;component/Fonts/#Segoe Print"/> <Setter Property="FontWeight" Value="Bold"/> <Setter Property="FontSize" Value="18"/> <Setter Property="Foreground" Value="White"/> <Setter Property="HorizontalAlignment" Value="Center"/> <Setter Property="VerticalAlignment" Value="Center"/> <Setter Property="Background" Value="{x:Null}"/> </Style><!-- If I remove this Style block below, it works fine. --> <Style TargetType="{x:Type TextBlock}"> <Setter Property="TextWrapping" Value="NoWrap"/> <Setter Property="TextTrimming" Value="None"/> <Setter Property="Foreground" Value="White"/> <Setter Property="FontSize" Value="16"/> <Setter Property="Effect"> <Setter.Value> <DropShadowEffect BlurRadius="3" ShadowDepth="4"/> </Setter.Value> </Setter> <Setter Property="FontFamily" Value="/ButtonTest;component/Fonts/#Segoe Print"/> </Style> </ResourceDictionary></Application.Resources></Application> 解决方案 The button is taking your content and putting it into a textblock, which picks up the style from app.xaml for textblocks. This is easy to see if you change the foreground property in the textblock to something like red, and bigger text. The values will apply to the button's textblock as well.A couple of workarounds:1. Put an explicit textblock into your button, and apply no style:<Button Margin="272,192,277,0" VerticalAlignment="Top"><TextBlock Text="Button" Style="{x:Null}"/></Button> 这篇关于按钮样式与TextBlock样式冲突?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 09-06 00:50