问题描述
高手们,用expression制作个性化按钮时,怎么才能让按钮上显示的图片是由用户指定的图片?
解决方案
解决方案二:
大神们现身哇
解决方案三:
<Window.Resources><StyleTargetType="Button"><!--<SetterProperty="Background"><Setter.Value><LinearGradientBrushStartPoint="0.5,0"EndPoint="0.5,1"><GradientStopColor="AliceBlue"Offset="0"/><GradientStopColor="AntiqueWhite"Offset="1"/></LinearGradientBrush></Setter.Value></Setter>--><SetterProperty="Template"><Setter.Value><ControlTemplateTargetType="Button"><Grid><EllipseFill="{TemplateBindingBackground}"Stroke="Gray"/><ContentPresenterMargin="5"HorizontalAlignment="Center"VerticalAlignment="Center"/></Grid></ControlTemplate></Setter.Value></Setter><SetterProperty="Background"><Setter.Value><ImageBrushImageSource="D:\TestWpfApplication2WpfApplication2Resources11.jpg"Stretch="UniformToFill"></ImageBrush><!--<LinearGradientBrushStartPoint="0.5,0"EndPoint="0.5,1"><GradientStopColor="AliceBlue"Offset="0"/><GradientStopColor="AntiqueWhite"Offset="1"/></LinearGradientBrush>--></Setter.Value></Setter><!--<Style.Triggers><TriggerProperty="IsMouseOver"Value="true"><SetterProperty="Background"Value="AntiqueWhite"/><SetterProperty="FontSize"Value="13"/></Trigger><TriggerProperty="IsKeyboardFocused"Value="True"><SetterProperty="FocusVisualStyle"Value="{x:Null}"/></Trigger></Style.Triggers>--></Style></Window.Resources><Grid><ButtonHeight="40"Margin="128,20,33,0"Name="button1"VerticalAlignment="Top">Button</Button><!--<EllipseMargin="13,90,94,0"Name="ellipse1"Stroke="Black"Height="30"VerticalAlignment="Top"><Ellipse.Fill><ImageBrushImageSource="D:\TestWpfApplication2WpfApplication2Resources11.jpg"></ImageBrush></Ellipse.Fill></Ellipse>--></Grid></Window>
解决方案四:
自己写一个Button。1.自定义ImageButton,该按钮拥有一个代表图片路径的Image依赖属性。publicclassImageButton:Button{publicstringImage{get{return(string)GetValue(ImageProperty);}set{SetValue(ImageProperty,value);}}publicstaticreadonlyDependencyPropertyImageProperty=DependencyProperty.Register("Image",typeof(string),typeof(ImageButton));}
2.为这个ImageButton写一个模板,在这个模板里根据你的要求放一个Image控件,将Image控件绑定到ImageButton的Image属性上,这样就能实现高复用性的图片按钮了。<StyleTargetType="control:ImageButton"><SetterProperty="SnapsToDevicePixels"Value="true"/><SetterProperty="OverridesDefaultStyle"Value="true"/><SetterProperty="FocusVisualStyle"Value="{x:Null}"/><SetterProperty="Cursor"Value="Hand"/><SetterProperty="Template"><Setter.Value><ControlTemplateTargetType="control:ImageButton"><Grid><ImageName="Image"Source="{BindingImage,RelativeSource={RelativeSourceTemplatedParent}}"><Image.BitmapEffect><BlurBitmapEffectRadius="0"/></Image.BitmapEffect></Image><BorderName="Mask"Background="Black"Opacity="0"><Border.OpacityMask><ImageBrushImageSource="{BindingImage,RelativeSource={RelativeSourceTemplatedParent}}"/></Border.OpacityMask></Border></Grid><ControlTemplate.Triggers><TriggerProperty="IsPressed"Value="True"><Trigger.EnterActions><BeginStoryboard><Storyboard><DoubleAnimationTo="8"Duration="0:0:0.1"Storyboard.TargetName="Image"Storyboard.TargetProperty="(Image.BitmapEffect).(BlurBitmapEffect.Radius)"/></Storyboard></BeginStoryboard></Trigger.EnterActions><Trigger.ExitActions><BeginStoryboard><Storyboard><DoubleAnimationDuration="0:0:0.1"Storyboard.TargetName="Image"Storyboard.TargetProperty="(Image.BitmapEffect).(BlurBitmapEffect.Radius)"/></Storyboard></BeginStoryboard></Trigger.ExitActions></Trigger></ControlTemplate.Triggers></ControlTemplate></Setter.Value></Setter></Style>