- 浏览: 399939 次
- 性别:
- 来自: 上海
文章分类
- 全部博客 (309)
- xaml C# wpf (0)
- scala java inner clas (1)
- Tools UML Eclipse UML2 (1)
- Timer .NET Framework (1)
- perl (6)
- python function paramter (1)
- Python Docstring (1)
- Python how to compare types (1)
- Python (8)
- java (5)
- C# (76)
- C# WPF (0)
- p4 (0)
- WPF (46)
- .net (6)
- xaml (1)
- javascript (40)
- windows (10)
- scala (4)
- winform (1)
- c++ (48)
- tools (12)
- cmd (1)
- os (0)
- CI (0)
- shell (0)
- C (2)
- haskell (49)
- functional (1)
- tool (1)
- gnu (1)
- linux (1)
- kaskell (0)
- svn (0)
- wcf (3)
- android (1)
最新评论
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.
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}"/>
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 - example to enhance ComboBox for AutoComplete
2014-09-19 15:56 1977first let’s see an example ... -
WPF – Virtualization – VirutalizationStackPanel and ItemsPanelTemplate
2013-08-05 21:55 1412Topic: WPF – Virtualization – ... -
wpf - BehaviorBase and one use examples
2013-06-18 18:41 1313Behavior is something that we ... -
WPF - Setting foreground color of Entire window
2013-06-13 16:00 1920You might as well as I would s ... -
WPF - Enhanced TabControl - TabControlEx aka Prerendering TabControl
2013-06-13 13:12 5331As an opening word, let's che ... -
wpf - ControlTemplate and AddLogicChild/RemoveLogicalChild
2013-06-10 15:42 1186Recently I was trying to debug ... -
wpf - default implicit style
2013-05-10 10:24 795We know that if you left out ... -
wpf - Style setter on the attached property
2013-05-08 14:54 2852I believe that you are familia ... -
wpf - specify enum values in xaml
2013-05-08 11:31 3588Many a situation you find tha ... -
wpf - IG xamDataGrid bind to XmlDataProvider with Xml Island
2012-12-18 14:28 1287Sometimes you may bind to some ... -
wpf - translate winform button/mouse event to wpf events
2012-12-12 17:37 2162It is common that we sometimes ... -
wpf - Freezable and its meaning
2012-09-27 12:38 0This article is based on the di ... -
wpf - Customize the grid lines for original wpf Grid control
2012-09-27 12:01 1458The System.WIndows.Controls.Gri ... -
c# - Convert from System.Drawing.Image to System.WIndows.Media.ImageSource
2012-09-25 14:27 7417In Previous discussion, we have ... -
wpf - Get Effective DependencyProperty value on a DependencyObject
2012-08-28 19:05 1045As discussed in my previous pos ... -
wpf - Default(Theme) style and its DefaultStyleKey
2012-08-28 17:54 1387As dicsused in the subsection o ... -
wpf - Dependency Property Value Precedence
2012-08-28 18:56 883A dependency property to an Dep ... -
wpf - ICutomTypeDescriptor , PropertyDescriptor and its use in PropertyGrid
2012-08-28 14:04 3582The type ICustomTypeDe ... -
wpf - tips to convert UI controls in WPF/Silverlight/Winforms into a Bitmap
2012-08-27 17:44 976In previous discussion, we have ... -
wpf - input binding examples
2012-08-27 11:47 1223Input binding is the mechanism ...
相关推荐
, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGrid}}}" Click="CheckBox_Click" /> ``` 然后在后台代码中处理Click事件: ```csharp private void CheckBox_Click(object ...
, RelativeSource={RelativeSource AncestorType=UserControl}}" FontSize="14" VerticalAlignment="Center" HorizontalAlignment="Center" /> ``` 接下来,在`TextButton.xaml.cs`文件中,我们需要处理鼠标...
<Binding Path="Maximum" RelativeSource="{RelativeSource TemplatedParent}" /> ``` 3. **设计竖置进度条** 要创建竖置进度条,我们需调整控件模板的布局,使用`StackPanel`或者`Grid`来实现垂直...
在VS2010中创建一个新WPF项目,你可以选择一个空白的Window模板,然后在XAML设计视图中添加所需的控件和布局。利用上述布局策略,确保窗口和控件能随着内容增加而自动扩展。 3. **C#代码与数据绑定**:在WPF中,...
, RelativeSource={RelativeSource TemplatedParent}}" Height="{Binding ActualWidth, RelativeSource={RelativeSource TemplatedParent}}"/> 圆形按钮" /> ``` 在这个例子中,我们创建了一个50x50...
Text="{Binding Path=Text, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}, UpdateSourceTrigger=PropertyChanged}" Visibility="{Binding Path=IsEditable, Converter={StaticResource ...
(Control.Background).(SolidColorBrush.Color), RelativeSource={RelativeSource TemplatedParent}}" Offset="0.4"/> ,0" EndPoint="0,1"> ...
, RelativeSource={RelativeSource TemplatedParent}}" Foreground="Black" /> <ContentPresenter Grid.Column="1" HorizontalAlignment="Left" VerticalAlignment="Center" Content="{TemplateBinding Content}" ...
Visibility="{Binding Path=HasItems, RelativeSource={RelativeSource TemplatedParent}, Converter={StaticResource BoolToVisibilityConverter}}"> <!-- 这里可以自定义展开/折叠按钮 --> ...
, RelativeSource={RelativeSource TemplatedParent}}" AllowsTransparency="True" Grid.ColumnSpan="2" Focusable="False" PopupAnimation="{DynamicResource {x:Static SystemParameters....
Visibility="{Binding IsClosable, Converter={StaticResource BoolToVisibilityConverter}, RelativeSource={RelativeSource TemplatedParent}}" Margin="5,0,0,0" Width="15" Height="15" Click="CloseButton...
Source="{Binding Path=Value, RelativeSource={RelativeSource TemplatedParent}, Converter={StaticResource ProgressConverter}}" Width="..." Height="..." Margin="..." /> <!-- 添加动画效果 --> ...
, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}"> ... ... ``` 最后,标签“功能齐全”提示我们可能需要涵盖一些高级特性,如拖放操作、选择事件、自定义节点模板等。例如...
IsChecked="{Binding Path=IsExpanded, RelativeSource={RelativeSource TemplatedParent}}" Focusable="False"> <!-- ToggleButton的模板可以自定义 --> Grid.Column="1" ContentSource="Header" ...
Visibility="{Binding IsExpanded, Converter={StaticResource BoolToVisibilityConverter}, RelativeSource={RelativeSource AncestorType=TreeViewItem}}"/> <!-- ... --> ``` 这里使用了一个`...
Value="{Binding Path=HorizontalOffset, RelativeSource={RelativeSource TemplatedParent}}" Maximum="{TemplateBinding ScrollableWidth}" Minimum="0" /> Visibility="{TemplateBinding ...
, Mode=TwoWay, RelativeSource={RelativeSource AncestorType=ComboBoxItem}}"/> ``` 3. **实现多选逻辑**:在上述代码中,我们绑定了CheckBox的IsChecked属性到ComboBoxItem的IsSelected属性,这使得选择...
, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}" Visibility="{Binding Path=Column1, Converter={StaticResource FirstItemHeaderVisibilityConverter}, Mode=OneWay}"> <!-- 添加样式以跨...
<Setter Property="ToolTip" Value="{Binding RelativeSource={x:Static RelativeSource.Self}, Path=(Validation.Errors)[0].ErrorContent}"/> ``` 4. 处理验证错误: 在代码-behind中,可以通过`...
<ScrollBar x:Name="PART_VerticalScrollBar" Orientation="Vertical" Visibility="{TemplateBinding ComputedVerticalScrollBarVisibility}" Value="{Binding Path=VerticalOffset, Mode=OneWay, RelativeSource={...