`

wpf - WPF TemplateBinding vs RelativeSource TemplatedParent

    博客分类:
  • WPF
wpf 
阅读更多

This is a post that summarizes the dicussion on WPF TemplateBinding vs RelativeSource TemplatedParent; This is a quote from the original site, however, I may add some verifcation code to attest/affirm/adjure it.

 

 

The orignal quesetion is what is the differences between 

 

 

<ControlTemplate TargetType="{x:Type Button}">
   <Border BorderBrush="{TemplateBinding Property=Background}">
      <ContentPresenter />
   </Border>
</ControlTemplate>

 

and 

 

 

<ControlTemplate TargetType="{x:Type Button}">
   <Border BorderBrush="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Background}">
      <ContentPresenter />
   </Border>
</ControlTemplate>

 

one of the best answers have something like this:

 

Grant BlahaErath 写道
TemplateBinding is not quite the same thing. MSDN docs are often written by people that have to quiz monosyllabic SDEs about software features, so the nuances are not quite right.

TemplateBindings are evaluated at compile time against the type specified in the control template. This allows for much faster instantiation of compiled templates. Just fumble the name in a templatebinding and you'll see that the compiler will flag it.

The binding markup is resolved at runtime. While slower to execute, the binding will resolve property names that are not visible on the type declared by the template. By slower, I'll point out that its kind of relative since the binding operation takes very little of the application's cpu. If you were blasting control templates around at high speed you might notice it.

As a matter of practice use the TemplateBinding when you can but don't fear the Binding.
 

and another answers is that 

 

Pual Fischer 写道
TemplateBinding - More limiting than using regular Binding

More efficient than a Binding but it has less functionality
Only works inside a ControlTemplate's visual tree
Doesn't work with properties on Freezables
Doesn't work within a ControlTemplate's Trigger
Provides a shortcut in setting properties(not as verbose),e.g. {TemplateBinding targetProperty}
Regular Binding - Does not have above limitations of TemplateBinding

Respects Parent Properties
Resets Target Values to clear out any explicitly set values
Example: <Ellipse Fill="{Binding RelativeSource={RelativeSource TemplatedParent},Path=Background}"/>
 
分享到:
评论

相关推荐

    wpf-DataGrid-checkBox

    , RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGrid}}}" Click="CheckBox_Click" /&gt; ``` 然后在后台代码中处理Click事件: ```csharp private void CheckBox_Click(object ...

    WPF-文字按钮的封装

    , RelativeSource={RelativeSource AncestorType=UserControl}}" FontSize="14" VerticalAlignment="Center" HorizontalAlignment="Center" /&gt; ``` 接下来,在`TextButton.xaml.cs`文件中,我们需要处理鼠标...

    WPF自定义进度条控件,横竖都有

    &lt;Binding Path="Maximum" RelativeSource="{RelativeSource TemplatedParent}" /&gt; ``` 3. **设计竖置进度条** 要创建竖置进度条,我们需调整控件模板的布局,使用`StackPanel`或者`Grid`来实现垂直...

    WPF 例程-窗口和控件随内容自动扩展

    在VS2010中创建一个新WPF项目,你可以选择一个空白的Window模板,然后在XAML设计视图中添加所需的控件和布局。利用上述布局策略,确保窗口和控件能随着内容增加而自动扩展。 3. **C#代码与数据绑定**:在WPF中,...

    WPF自定义圆形按钮控件

    , RelativeSource={RelativeSource TemplatedParent}}" Height="{Binding ActualWidth, RelativeSource={RelativeSource TemplatedParent}}"/&gt; 圆形按钮" /&gt; ``` 在这个例子中,我们创建了一个50x50...

    WPF ComboBox样式

    Text="{Binding Path=Text, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}, UpdateSourceTrigger=PropertyChanged}" Visibility="{Binding Path=IsEditable, Converter={StaticResource ...

    wpf的button样式

    (Control.Background).(SolidColorBrush.Color), RelativeSource={RelativeSource TemplatedParent}}" Offset="0.4"/&gt; ,0" EndPoint="0,1"&gt; ...

    WPF 自定义CheckBox样式

    , RelativeSource={RelativeSource TemplatedParent}}" Foreground="Black" /&gt; &lt;ContentPresenter Grid.Column="1" HorizontalAlignment="Left" VerticalAlignment="Center" Content="{TemplateBinding Content}" ...

    WPF自定义TreeViewItem 样式

    Visibility="{Binding Path=HasItems, RelativeSource={RelativeSource TemplatedParent}, Converter={StaticResource BoolToVisibilityConverter}}"&gt; &lt;!-- 这里可以自定义展开/折叠按钮 --&gt; ...

    一个用WPF写的CheckedComboBox

    , RelativeSource={RelativeSource TemplatedParent}}" AllowsTransparency="True" Grid.ColumnSpan="2" Focusable="False" PopupAnimation="{DynamicResource {x:Static SystemParameters....

    WPF tab宽度自适应 可关闭

    Visibility="{Binding IsClosable, Converter={StaticResource BoolToVisibilityConverter}, RelativeSource={RelativeSource TemplatedParent}}" Margin="5,0,0,0" Width="15" Height="15" Click="CloseButton...

    wpf进度条(图片模版)

    Source="{Binding Path=Value, RelativeSource={RelativeSource TemplatedParent}, Converter={StaticResource ProgressConverter}}" Width="..." Height="..." Margin="..." /&gt; &lt;!-- 添加动画效果 --&gt; ...

    最漂亮的treeview(wpf)

    , Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}"&gt; ... ... ``` 最后,标签“功能齐全”提示我们可能需要涵盖一些高级特性,如拖放操作、选择事件、自定义节点模板等。例如...

    WPF 中TreeView的子节点选中时父节点也会高亮(样式)

    IsChecked="{Binding Path=IsExpanded, RelativeSource={RelativeSource TemplatedParent}}" Focusable="False"&gt; &lt;!-- ToggleButton的模板可以自定义 --&gt; Grid.Column="1" ContentSource="Header" ...

    WPF 虚线 TreeView

    Visibility="{Binding IsExpanded, Converter={StaticResource BoolToVisibilityConverter}, RelativeSource={RelativeSource AncestorType=TreeViewItem}}"/&gt; &lt;!-- ... --&gt; ``` 这里使用了一个`...

    wpf listview 滚动条和自动排序

    Value="{Binding Path=HorizontalOffset, RelativeSource={RelativeSource TemplatedParent}}" Maximum="{TemplateBinding ScrollableWidth}" Minimum="0" /&gt; Visibility="{TemplateBinding ...

    用WPF实现多选下拉框

    , Mode=TwoWay, RelativeSource={RelativeSource AncestorType=ComboBoxItem}}"/&gt; ``` 3. **实现多选逻辑**:在上述代码中,我们绑定了CheckBox的IsChecked属性到ComboBoxItem的IsSelected属性,这使得选择...

    wpf GridView 合并单元格 Demo

    , RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}" Visibility="{Binding Path=Column1, Converter={StaticResource FirstItemHeaderVisibilityConverter}, Mode=OneWay}"&gt; &lt;!-- 添加样式以跨...

    WPF列表平滑+惯性

    &lt;ScrollBar x:Name="PART_VerticalScrollBar" Orientation="Vertical" Visibility="{TemplateBinding ComputedVerticalScrollBarVisibility}" Value="{Binding Path=VerticalOffset, Mode=OneWay, RelativeSource={...

    WPF 仿淘宝评级控件

    , RelativeSource={RelativeSource AncestorType=UserControl}, Converter={StaticResource StarColorConverter}}"/&gt; ``` 在这个例子中,我们使用了一个IValueConverter来将星级转换为颜色,这样就可以根据评分...

Global site tag (gtag.js) - Google Analytics