ControlTemplate元素在其绑定的控件模板调整大小时也会调整大小-WPF XAML
我想知道是否可以通过调整绑定的元素的大小来调整controlTemplate中的元素的大小 这是按钮控件模板的代码:ControlTemplate元素在其绑定的控件模板调整大小时也会调整大小-WPF XAML,wpf,visual-studio,xaml,controltemplate,Wpf,Visual Studio,Xaml,Controltemplate,我想知道是否可以通过调整绑定的元素的大小来调整controlTemplate中的元素的大小 这是按钮控件模板的代码: <ControlTemplate x:Name="normalButton" x:Key="roundedEdge" TargetType="Button"> <Grid> <Rectangle Fill="{TemplateBinding Background}" RadiusX="16" Radiu
<ControlTemplate x:Name="normalButton" x:Key="roundedEdge" TargetType="Button">
<Grid>
<Rectangle Fill="{TemplateBinding Background}" RadiusX="16" RadiusY="16" Stroke="{TemplateBinding BorderBrush}" StrokeThickness="4"/>
<Ellipse Width="38" Height="38" VerticalAlignment="Top" HorizontalAlignment="Center" Margin="0,12,0,0" Fill="{StaticResource tertiaryColor}"/>
<ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Bottom" Margin="0, 0, 0, 6"/>
</Grid>
</ControlTemplate>
它是绑定到按钮的控件模板:
<Button x:Name="button2_Copy2" Width="112" Height="80"Template="{StaticResource roundedEdge}""/>
您可以使用绑定和转换:
<Window.Resources>
<local:DoubleToDoubleConvert x:Key="DoubleToDoubleConvert"></local:DoubleToDoubleConvert>
<ControlTemplate x:Name="normalButton" x:Key="roundedEdge" TargetType="Button">
<Grid>
<Rectangle Fill="{TemplateBinding Background}" RadiusX="16" RadiusY="16" Stroke="{TemplateBinding BorderBrush}" StrokeThickness="4"/>
<Ellipse Width="{TemplateBinding Width ,Converter={StaticResource DoubleToDoubleConvert}}" Height="{TemplateBinding Height,Converter={StaticResource DoubleToDoubleConvert}}" VerticalAlignment="Top" HorizontalAlignment="Center" Margin="0,12,0,0" Fill="Red"/>
<ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Bottom" Margin="0, 0, 0, 6"/>
</Grid>
</ControlTemplate>
</Window.Resources>
public class DoubleToDoubleConvert : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
double reValue = System.Convert.ToInt32(value);
return reValue / 3;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
double strValue = (double)value;
return value;
}
}
公共类DoubleToDoubleConvert:IValueConverter
{
公共对象转换(对象值、类型targetType、对象参数、CultureInfo区域性)
{
双重重估=系统转换为32(值);
返回重估值/3;
}
公共对象转换回(对象值、类型targetType、对象参数、CultureInfo区域性)
{
双标准值=(双)值;
返回值;
}
}
谢谢。只要设置固定的宽度和高度,椭圆就不会调整大小。为了按比例调整大小(如果这是您的要求),请定义适当的网格行和/或列。@Clemens我定义了一些行和列定义,但它没有任何作用。如果您不显示代码,如果您不清楚地解释您试图实现的目标,我们将无法帮助您。