Wpf 在设计窗口中调整大小时,自定义控件的高度和宽度将消失
我已经创建了一个WPF自定义控件,我把它放在一个窗体上,给它一个高度和宽度——所有这些都很好。我在控件中使用TemplateBinding引用高度和宽度,所以需要设置它们 如果我决定使用gui调整控件的大小,那么高度和宽度将完全消失,控件格式也会出错 发生这种情况时,如何获取控件的新高度和宽度并进行设置 要求的投寄代码:Wpf 在设计窗口中调整大小时,自定义控件的高度和宽度将消失,wpf,custom-controls,Wpf,Custom Controls,我已经创建了一个WPF自定义控件,我把它放在一个窗体上,给它一个高度和宽度——所有这些都很好。我在控件中使用TemplateBinding引用高度和宽度,所以需要设置它们 如果我决定使用gui调整控件的大小,那么高度和宽度将完全消失,控件格式也会出错 发生这种情况时,如何获取控件的新高度和宽度并进行设置 要求的投寄代码: <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:GraphControls">
<Style TargetType="{x:Type local:ContourPlot}" >
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:ContourPlot}">
<Border Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}">
<Grid>
<Viewbox>
<Border BorderBrush="Black" BorderThickness="1">
<Canvas x:Name="cvGraph" Width="{TemplateBinding Width}" Height="{TemplateBinding Height}" >
<Rectangle Canvas.Left="40" Canvas.Top="31" Width="48" Height="41" Fill="AliceBlue"/>
</Canvas>
</Border>
</Viewbox>
<Canvas Width="{TemplateBinding Width}" Height="{TemplateBinding Height}" >
<Label x:Name="lblTest" Canvas.Left="0" Canvas.Top="10" Content="Label" FontSize="12" />
</Canvas>
</Grid>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
控件的使用形式如下所示:
<Window x:Class="GraphWrapper.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:gc="clr-namespace:GraphControls;assembly=GraphControls"
Title="MainWindow" Height="350" Width="525">
<Grid>
<gc:ContourPlot Background="#FFC3EAC3" Margin="65,42,128,118" Width="310" Height="150"/>
</Grid>
</Window>
如前所述,当通过gui调整控件大小时,宽度和高度属性将消失。
<Style TargetType="{x:Type local:ContourPlot}" >
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:ContourPlot}">
<Border Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}"
Width="{TemplateBinding Width}"
Height="{TemplateBinding Height}">
<Grid>
<Viewbox Width="{TemplateBinding Width}"
Height="{TemplateBinding Height}">
<Border BorderBrush="Black" BorderThickness="1">
<Canvas x:Name="cvGraph" >
<Rectangle Canvas.Left="40" Canvas.Top="31" Width="48" Height="41" Fill="AliceBlue"/>
</Canvas>
</Border>
</Viewbox>
<Canvas >
<Label x:Name="lblTest" Canvas.Left="0" Canvas.Top="10" Content="Label" FontSize="12" />
</Canvas>
</Grid>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
不建议在其控件模板内的多个控件中使用模板化的宽度和高度。在您的情况下,高度和宽度将与上面的设置相同
此外,您已将
视图框
和画布
放置在同一区域中,因此它将以相同的宽度和高度重叠。问题可能在于Viewbox
发布您迄今为止尝试过的代码。谢谢-我会尝试一下-Viewbox和画布重叠是经过深思熟虑的,因为我希望在调整大小时,Viewbox画布中的任何内容都可以缩放,但其他画布中的任何内容都不能缩放。