What is the difference between Binding and TemplateBinding?
2008-10-15 15:01
603 查看
Binding provides much more flexibility than TemplateBinding, but it’s more costly. TemplateBinding is limited to one scenario but very efficient in what it does.
Everytime you want to bind to a property of an element from within its template, you should consider using a TemplateBinding. For example, consider the following scenario:
<Window x:Class=”CommonQuestions.Window1″
xmlns=”http://schemas.microsoft.com/winfx/2006/xaml/presentation”
xmlns:x=”http://schemas.microsoft.com/winfx/2006/xaml”
Title=”CommonQuestions” Height=”300″ Width=”300″
>
<Window.Resources>
<ControlTemplate TargetType=”{x:Type Button}” x:Key=”buttonTemplate”>
<Border BorderBrush=”{TemplateBinding Property=Background}” BorderThickness=”3″ >
<ContentPresenter Margin=”10″/>
</Border>
</ControlTemplate>
</Window.Resources>
<StackPanel Margin=”20″>
<Button Template=”{StaticResource buttonTemplate}” HorizontalAlignment=”Center” Background=”SteelBlue”>Hello</Button>
</StackPanel>
</Window>
In this case, I want the BorderBrush property of Border to be whatever color is specified in the Button’s Background. This is exactly the scenario that TemplateBinding is optimized for. Instead of the TemplateBinding in this snippet, you could use the following Binding: {Binding RelativeSource={RelativeSource TemplatedParent}, Path=Background}. The resulting behavior would be the same, but this is not as efficient, and it’s quite a bit more complex to write, so TemplateBinding is the preferred approach. TemplateBinding also allows you to specify a Converter and a ConverterParameter, which increases the scope of the supported scenarios quite a bit. However, a TemplateBinding can only transfer data in one direction: from the templated parent to the element with the TemplateBinding. If you need to transfer data in the opposite direction or both ways, a Binding with RelativeSource of TemplatedParent is your only option. For example, interaction with a TextBox or Slider within a template will only change a property on the templated parent if you use a two-way Binding.
In summary: if you want a one-way binding from within a template to a property of its templated parent, use TemplateBinding. For all other scenarios, or for extra flexibility in this last scenario, use Binding.
Everytime you want to bind to a property of an element from within its template, you should consider using a TemplateBinding. For example, consider the following scenario:
<Window x:Class=”CommonQuestions.Window1″
xmlns=”http://schemas.microsoft.com/winfx/2006/xaml/presentation”
xmlns:x=”http://schemas.microsoft.com/winfx/2006/xaml”
Title=”CommonQuestions” Height=”300″ Width=”300″
>
<Window.Resources>
<ControlTemplate TargetType=”{x:Type Button}” x:Key=”buttonTemplate”>
<Border BorderBrush=”{TemplateBinding Property=Background}” BorderThickness=”3″ >
<ContentPresenter Margin=”10″/>
</Border>
</ControlTemplate>
</Window.Resources>
<StackPanel Margin=”20″>
<Button Template=”{StaticResource buttonTemplate}” HorizontalAlignment=”Center” Background=”SteelBlue”>Hello</Button>
</StackPanel>
</Window>
In this case, I want the BorderBrush property of Border to be whatever color is specified in the Button’s Background. This is exactly the scenario that TemplateBinding is optimized for. Instead of the TemplateBinding in this snippet, you could use the following Binding: {Binding RelativeSource={RelativeSource TemplatedParent}, Path=Background}. The resulting behavior would be the same, but this is not as efficient, and it’s quite a bit more complex to write, so TemplateBinding is the preferred approach. TemplateBinding also allows you to specify a Converter and a ConverterParameter, which increases the scope of the supported scenarios quite a bit. However, a TemplateBinding can only transfer data in one direction: from the templated parent to the element with the TemplateBinding. If you need to transfer data in the opposite direction or both ways, a Binding with RelativeSource of TemplatedParent is your only option. For example, interaction with a TextBox or Slider within a template will only change a property on the templated parent if you use a two-way Binding.
In summary: if you want a one-way binding from within a template to a property of its templated parent, use TemplateBinding. For all other scenarios, or for extra flexibility in this last scenario, use Binding.
相关文章推荐
- Multi-thread: What is the difference between OpenMP and MPI
- What is the difference between Web Farm and Web Garden?
- Whats is the difference between train, validation and test set, in neural networks?
- What is the difference between SSL, TLS, and HTTPS?
- what is the difference between little-endian and big-endian.
- macro与inline的区别 What is the difference between macro and inline?
- what is the difference between Association, Dependence, Aggregation and Composition
- What is the basic difference between NSTimer, NSTask, NSThread and NSRunloop?
- what is the difference between processes and threads
- What is the difference between '/n' and '/r/n'? /n /r/n的区别是什么
- What is the difference between a von Neumann architecture and a Harvard architecture?
- What is the difference between ContentPresenter and ContentControl?
- What is the difference between "transit","transfer","transmit... and "transport"
- What is the difference between a URI, a URL and a URN
- What is the difference between Array.Clone and Array.Copy Method
- Multi-thread: What is the difference between OpenMP and MPI
- What is the difference between angle bracket < > and double quotes " " while including header files
- What is the difference between modified duration, effective duration and duration?
- What is the difference between Views and Materialized Views in Oracle?
- What is the difference between new and malloc()?