Templates and Custom Classes [modified]
-
Okay, I've scraped a template from a 3rd party control (the Telerik RadGridView if you must know), and I've managed to add a titlebar and an export button to the template (in the XAML). It looks like this:
<Style x:Key="MyGridStyle">
<Border x:Name="PART_TitleBar" Grid.Row="0" BorderBrush="Black" Background="Black"
BorderThickness="1" Height="28">
<Grid>
<Label x:Name="PART_Title" Content="Title" FontWeight="Bold" FontStyle="Italic"
VerticalContentAlignment="Center" HorizontalContentAlignment="Center"
Foreground="White" Background="Transparent" IsTabStop="False"/>
<Button x:Name="buttonExport" Content="Export" Width="75" HorizontalAlignment="Right"
VerticalAlignment="Top" Margin="0,2.5,3,0"/>
</Grid>
</Border>...
...<Style x:Key="MyGridStyle">
Now, I want to be able to modify the properties of these two new template items in the controls that use this style. Do I need to write CS code to do this, or can I do it all in the XAML of the element that uses that style?
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
-----
"...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001modified on Thursday, June 25, 2009 9:42 AM
-
Okay, I've scraped a template from a 3rd party control (the Telerik RadGridView if you must know), and I've managed to add a titlebar and an export button to the template (in the XAML). It looks like this:
<Style x:Key="MyGridStyle">
<Border x:Name="PART_TitleBar" Grid.Row="0" BorderBrush="Black" Background="Black"
BorderThickness="1" Height="28">
<Grid>
<Label x:Name="PART_Title" Content="Title" FontWeight="Bold" FontStyle="Italic"
VerticalContentAlignment="Center" HorizontalContentAlignment="Center"
Foreground="White" Background="Transparent" IsTabStop="False"/>
<Button x:Name="buttonExport" Content="Export" Width="75" HorizontalAlignment="Right"
VerticalAlignment="Top" Margin="0,2.5,3,0"/>
</Grid>
</Border>...
...<Style x:Key="MyGridStyle">
Now, I want to be able to modify the properties of these two new template items in the controls that use this style. Do I need to write CS code to do this, or can I do it all in the XAML of the element that uses that style?
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
-----
"...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001modified on Thursday, June 25, 2009 9:42 AM
-
If it answer's your question - Style Triggers[^] ?
No, I don't think that's what I want. Given the template code provided in the OP, I want to be able to *override* certain properties of the new parts without having to completely replace the template in XAML. For instance, I may want to change the background color of the border or the color or font of the label text, or I may want to make the button invisible.
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
-----
"...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001 -
Okay, I've scraped a template from a 3rd party control (the Telerik RadGridView if you must know), and I've managed to add a titlebar and an export button to the template (in the XAML). It looks like this:
<Style x:Key="MyGridStyle">
<Border x:Name="PART_TitleBar" Grid.Row="0" BorderBrush="Black" Background="Black"
BorderThickness="1" Height="28">
<Grid>
<Label x:Name="PART_Title" Content="Title" FontWeight="Bold" FontStyle="Italic"
VerticalContentAlignment="Center" HorizontalContentAlignment="Center"
Foreground="White" Background="Transparent" IsTabStop="False"/>
<Button x:Name="buttonExport" Content="Export" Width="75" HorizontalAlignment="Right"
VerticalAlignment="Top" Margin="0,2.5,3,0"/>
</Grid>
</Border>...
...<Style x:Key="MyGridStyle">
Now, I want to be able to modify the properties of these two new template items in the controls that use this style. Do I need to write CS code to do this, or can I do it all in the XAML of the element that uses that style?
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
-----
"...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001modified on Thursday, June 25, 2009 9:42 AM
It's the same as with any other element - if there's properties that can be set on the element then you can override them in XAML where you use the control. It's up to you to design the element and the template if you need access to properties down the tree in the control. TemplateBinding is your friend here. Pretty much none of the properties you've shown in your template are overridable.
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
It's the same as with any other element - if there's properties that can be set on the element then you can override them in XAML where you use the control. It's up to you to design the element and the template if you need access to properties down the tree in the control. TemplateBinding is your friend here. Pretty much none of the properties you've shown in your template are overridable.
Mark Salsbery Microsoft MVP - Visual C++ :java:
This is what I've done. In the code below, I've only included one attached property since I figure that should serve as enough of an example of what I'm doing. First, I have my custom control class:
public class UDPTelerikGridView: RadGridView
{
static UDPTelerikGridView()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(UDPTelerikGridView),
new FrameworkPropertyMetadata(typeof(UDPTelerikGridView)));
}//-------------------------------------------------------------------------------- public static readonly DependencyProperty TitleBarVisibilityProperty = DependencyProperty.RegisterAttached("TitleBarVisibility", typeof(Visibility), typeof(UDPTelerikGridView)); public static void SetTitleBarVisibility(UIElement element, Visibility value) { element.SetValue(TitleBarVisibilityProperty, value); } public static Visibility GetTitleBarVisibility(UIElement element) { return (Visibility)element.GetValue(TitleBarVisibilityProperty); } //-------------------------------------------------------------------------------- public override void OnApplyTemplate() { base.ApplyTemplate(); Border titleBar = base.GetTemplateChild("PART\_TitleBar") as Border; if (titleBar != null) { titleBar.Visibility = GetTitleBarVisibility(titleBar); } }
}
Next, I have the Style I wrote to include the new components as part of the existing RadGridView control (all of the original template that follows my additional stuff is omitted in the interest of brevity:
<Style x:Key="UDPRadGridViewTopTitle" TargetType="{x:Type udpcontrols:UDPTelerikGridView}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type telerik:RadGridView}">
<AdornerDecorator>
<Border x:Name="PART_MasterGridContainer">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="28" />
<RowDefinition />
</Grid.RowDefinitions>
<Border x:Name="PART_TitleBar" Grid.Row="0"
BorderBrush="{TemplateBinding BorderBrush}"
Background="{TemplateBinding Background}"