C# WPF:哪些属性应用于ControlTemplate
我想了解xaml控件的哪些属性应用于该控件的ControlTemplateC# WPF:哪些属性应用于ControlTemplate,c#,wpf,xaml,binding,controltemplate,C#,Wpf,Xaml,Binding,Controltemplate,我想了解xaml控件的哪些属性应用于该控件的ControlTemplateF.e.如果我基于如下窗口类创建控件: (这是非常简单的-在我知道的当前状态下没有意义…) 以及模板: <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xam
F.e.如果我基于如下窗口类创建控件:
(这是非常简单的-在我知道的当前状态下没有意义…) 以及模板:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:shell="clr-namespace:Microsoft.Windows.Shell;assembly=Microsoft.Windows.Shell"
xmlns:local="clr-namespace:Arctic">
<Style TargetType="{x:Type local:BaseWindow}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:BaseWindow}">
<Grid Background="Transparent">
<Border Background="{TemplateBinding Background}"/>
<ContentPresenter/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
现在,当我在应用程序中指定BaseWindow控件时,Margin属性将应用于BaseWindow,而不指定TemplateBinding。背景不是,我必须在模板中声明TemplateBinding才能实现这一点
您能解释一下为什么某些属性默认应用于ControlTemplate,而其他属性则不应用于ControlTemplate吗?
我的猜测是,Window.xaml(WPF的默认窗口模板)绑定到一些属性,如边距,但忽略了一些类似背景的属性。如果这是真的,那么我不明白为什么我可以在窗口控件中设置背景并应用于它。似乎窗口绑定到某些属性,并在从中派生时停止执行该操作…
这可能是完全错误的-我只是想解释我的想法。
Window
类继承FrameworkElement
及其所有属性,包括FrameworkElement.Margin
。Control.Background也一样。你的问题是为什么你必须做一些事情来控制.Background
答案很简单:
Margin
用于布局,其功能由FrameworkElement
实现/提供,并且总是发生,对您不可见,并且忽略ControlTemplate
(因为所有框架元素都参与布局并使用Margin)
Background
,依次提供给视觉工具使用。如何使用它取决于您,因为只有您知道控件的外观<代码>控件
不知道如何处理该属性
因此,您必须使用
模板绑定
将背景
绑定到控制模板
中的某个颜色,但是边距
可以在控制模板中工作,而无需执行任何操作。啊,我明白了。所以我基本上可以检查所有这些属性,比如光标或最小高度都是自动应用的?你必须以不同的方式考虑每个属性<代码>光标将自动应用,是的,但您的框架元素必须提供可以鼠标覆盖的内容。如果什么都没有,它就不会工作<代码>边距和最小高度
将始终有效,因为所有框架元素
都参与布局,例如设置最小高度
将在测量和排列过程中保留该高度。你可以阅读。
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:shell="clr-namespace:Microsoft.Windows.Shell;assembly=Microsoft.Windows.Shell"
xmlns:local="clr-namespace:Arctic">
<Style TargetType="{x:Type local:BaseWindow}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:BaseWindow}">
<Grid Background="Transparent">
<Border Background="{TemplateBinding Background}"/>
<ContentPresenter/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>