gpt4 book ai didi

wpf - 带有可调整大小圆圈的按钮控件模板

转载 作者:行者123 更新时间:2023-12-04 11:45:26 24 4
gpt4 key购买 nike

我正在学习 WPF 中的控件模板并检查如何用自定义模板样式替换按钮外观。我看到要制作一个圆形按钮,必须用相同的高度和宽度定义一个椭圆。

<Style x:Key="Button2" TargetType="Button">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Grid>
<Ellipse Fill="LightGreen" Width="80" Height="80"/>
<ContentPresenter HorizontalAlignment="Center"
VerticalAlignment="Center"/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
<Setter Property="Control.Margin" Value="10"/>
</Style>

当然,这只会强制使用该样式的所有按钮具有直径为 80 像素的圆,而不管按钮如何调整大小。我希望圆圈采用较短的高度/宽度值,以便它可以根据按钮大小动态缩放。

但是,我还没有阅读任何 Material 教如何在纯 XAML 模板中完成此操作?似乎需要一些代码隐藏才能达到这种效果?

最佳答案

这就是 TemplateBinding 的用武之地(TemplateBinding 用于控件模板内部,用于从模板化控件中检索值,在本例中为 Button)。

<Ellipse Fill="LightGreen" 
Width="{TemplateBinding ActualWidth}" Height="{TemplateBinding ActualHeight}"/>

请注意,这是一种较短的使用形式:
{Binding ActualWidth, RelativeSource={RelativeSource TemplatedParent}}

TemplateBinding 标记扩展仅针对 TemplatedParent 绑定(bind)进行了优化。

也就是说,如果你只希望它是一个圆圈,如果你的椭圆是 W/H 中的较小者,那么你的内容很容易流出它,我怀疑这是你真正想要的..?我曾想过使用多值转换器来做到这一点,但你不能绑定(bind)到转换器参数,所以就不行了。

在这种情况下,附加的行为会起作用,但它并不漂亮。
<Window x:Class="WpfApplication1.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:c="clr-namespace:WpfApplication1"
xmlns:local="clr-namespace:WpfApplication1"
Title="Window1" Height="300" Width="300">

<Grid>
<Button Content="Yo!" Width="50" Height="30">
<Button.Template>
<ControlTemplate TargetType="Button">
<Grid>
<Ellipse Fill="LightGreen" local:ConstrainWidthHeight.ConstrainedWidth="{TemplateBinding ActualWidth}" local:ConstrainWidthHeight.ConstrainedHeight="{TemplateBinding ActualHeight}"/>
<ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
</Grid>
</ControlTemplate>
</Button.Template>
</Button>
</Grid>
</Window>

...以及附加的行为:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;

namespace WpfApplication1 {
public class ConstrainWidthHeight {
public static readonly DependencyProperty ConstrainedWidthProperty =
DependencyProperty.RegisterAttached( "ConstrainedWidth", typeof( double ), typeof( ConstrainWidthHeight ), new PropertyMetadata( double.NaN, OnConstrainValuesChanged ) );
public static readonly DependencyProperty ConstrainedHeightProperty =
DependencyProperty.RegisterAttached( "ConstrainedHeight", typeof( double ), typeof( ConstrainWidthHeight ), new UIPropertyMetadata( double.NaN, OnConstrainValuesChanged ) );

public static double GetConstrainedHeight( FrameworkElement obj ) {
return (double) obj.GetValue( ConstrainedHeightProperty );
}

public static void SetConstrainedHeight( FrameworkElement obj, double value ) {
obj.SetValue( ConstrainedHeightProperty, value );
}

public static double GetConstrainedWidth( FrameworkElement obj ) {
return (double) obj.GetValue( ConstrainedWidthProperty );
}

public static void SetConstrainedWidth( FrameworkElement obj, double value ) {
obj.SetValue( ConstrainedWidthProperty, value );
}

private static void OnConstrainValuesChanged( object sender, DependencyPropertyChangedEventArgs e ) {
FrameworkElement element = sender as FrameworkElement;
if( element != null ) {
double width = GetConstrainedWidth( element );
double height = GetConstrainedHeight( element );

if( width != double.NaN && height != double.NaN ) {
double value = Math.Min( width, height );

element.Width = value;
element.Height = value;
}
}
}
}
}

好的,现在需要使用附加行为的原因(无论如何都是 AFAICT)是为了使椭圆居中(在非正方形/非圆形场景中),您需要 Horizo​​ntalAlignment 和 VerticalAlignment 才能生效.两者的默认值都是 Stretch,当设置显式 Width/Height 时,它的行为类似于 Center。

启用 Stretch="Uniform"后,您的 Ellipse 将始终在物理上占据整个空间,只有 Ellipse 的绘图会受到限制。使用它,您绘制的椭圆图形将始终从左上角开始。因此,在这种情况下,如果您的按钮宽度大于高度,椭圆的绘制部分将不会与文本一起居中。

此代码是您可能不需要的一个很好的示例:
<Ellipse Height="{TemplateBinding ActualHeight}" Width="{TemplateBinding ActualWidth}" Fill="LightGreen" Stretch="Uniform" />

...以及使用它的按钮(具有非方形宽度/高度):
<Button Content="YO!" Style="{StaticResource Button2}" Width="120" Height="53" VerticalAlignment="Top"></Button>

看起来像这样:

Ugly http://www.freeimagehosting.net/uploads/84e62c4982.png

...与附加属性选项相比:

alt text http://www.freeimagehosting.net/uploads/40755babcd.png

关于wpf - 带有可调整大小圆圈的按钮控件模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2271167/

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