Reputation: 757
I have a combobox defined in xaml:
<ComboBox Width="100"/>
This ComboBox, along with all other combobxes I have, is styled with a ControlTemplate which I copied and edited some colors and such in it.
<ControlTemplate TargetType="{x:Type ComboBox}">....
In this control template, how can I access the value of the Width attribute from the element above?
So for example:
<ControlTemplate TargetType="{x:Type ComboBox}">
<Grid Width="??{Binding WidthValue}??" >....
Where the {Binding WidthValue} is 100, from the Width="100" above.
Upvotes: 0
Views: 80
Reputation: 274
<Grid Width="{TemplateBinding Width}">
P.S. you'll often see this used in the default control templates for controls for attributes like Padding
, Margin
, and SnapsToDevicePixels
Upvotes: 1
Reputation: 33364
You can use TemplatedParent
binding
<Grid Width="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Width}">
but you shouldn't need to as Grid
should stretch so if you limit ComboBox
to 100 you should automatically limit Grid
inside
EDIT
If you need to bind to width then I would suggest to bind to ActualWidth
instead of Width
<Grid Width="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=ActualWidth}">
as Width
does not need to be defined in all controls where this template is used
Upvotes: 0