wpf - WPF TemplateBinding vs RelativeSource TemplatedParent

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}"/>
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以使用以下步骤将 ComboBox 的 SelectionChanged 事件绑定到 ViewModel 中的一个命令: 1. 在 ViewModel 中定义一个 ICommand 属性,用于处理 ComboBox 的 SelectionChanged 事件。 ```csharp private ICommand _comboBoxSelectionChangedCommand; public ICommand ComboBoxSelectionChangedCommand { get { if (_comboBoxSelectionChangedCommand == null) { _comboBoxSelectionChangedCommand = new RelayCommand<object>(ComboBoxSelectionChanged, CanComboBoxSelectionChanged); } return _comboBoxSelectionChangedCommand; } } private bool CanComboBoxSelectionChanged(object parameter) { // 可选:检查命令是否可用 return true; } private void ComboBoxSelectionChanged(object parameter) { // 处理 ComboBox 的 SelectionChanged 事件 } ``` 2. 在 XAML 中,将 ComboBox 的 SelectionChanged 事件绑定到上面定义的命令,并使用 CommandParameter 属性将 ComboBox 本身作为参数传递给命令。 ```xml <ComboBox ItemsSource="{Binding Items}" SelectedItem="{Binding SelectedItem}" SelectionChanged="{Binding ComboBoxSelectionChangedCommand}" CommandParameter="{Binding RelativeSource={RelativeSource Self}}"> <ComboBox.ItemTemplate> <DataTemplate> <TextBlock Text="{Binding}" /> </DataTemplate> </ComboBox.ItemTemplate> </ComboBox> ``` 注意,在上面的代码中,我使用了一个 RelayCommand 类来实现 ICommand 接口,你可以根据需要使用其他实现方式来绑定命令。另外,我还使用了 CommandParameter 属性将 ComboBox 本身作为参数传递给命令,这样在 ViewModel 中就可以访问 ComboBox 的属性和方法了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值