How do you expand an element to fill an Expanders Header? - wpf

I'm trying to expand an element to fill the horizontal space of an Expander.Header. I asked a similar question earlier, though I have since realized that the premises of the question was incorrect. Any use of Grids, or DockingPanels seems to have no effect on the ability for an element to fill the horizontal space of a Expander.Header.
Some more digging revealed that the ContentPresenter for the header is automatically set to HorizontalAlignment.Left. How do I go about changing this?

The only way to do that is finding the source xaml for the expander and modify it the way you want, creating your own expander style.

What you're looking for is a ControlTemplate. See here. You have to override the default template for the Expander.
You can declare a template which will affect all Expander controls using the following:
<ControlTemplate TargetType="Expander">
...
</ControlTemplate>
Or, you can set the Template property in a Style.
<Style TargetType="Expander">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Expander">
...
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>

Related

WPF TreeView style template resetting IsExpanded on style switch

I have a basic TreeView on a window that has a style applied from a resource in a dll. The style dll is capable of switching between two styles. When I don't have a style for the TreeView in the dll I am able to expand the TreeViewItems, switch styles (of other controls), and the TreeViewItems remain expanded. However, as soon as I add a style for the TreeView, the TreeViewItems that have been expanded collapse as soon as the style is switched.
All fancy animations and TreeViewItem styling has been removed in order to track down the problem. The remaining style is simply:
<Style x:Key="{x:Type TreeView}" TargetType="{x:Type TreeView}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="TreeView">
<Border Name="Border"
Background="Transparent"
BorderThickness="1"
CornerRadius="1">
<ItemsPresenter />
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
Any suggestions would be greatly appreciated. I've ran out of ideas!
When you take away a template or replace by another some dependency properties will reset to their default value. The reason for that is dependency property precedence. Take a look at this here: http://msdn.microsoft.com/library/ms743230
Think of it like this: A dependency property may contain many values which are layered. The value in top most layer is always the current value. If you take a style away, you take layers away too. If you take all layers away, the dependency property will take default value as current value.
When you replace the style of your TreeView, all underneath styles will be updated/reinitalized/changed/resetted...
To fix this try keep same template and only change colors, borders and stuff like that.. or use Binding

Xaml Grid Styling

New to WPF, I'm having some trouble creating a styles in my code, I was able to make some buttons styles by drawing rectangles and making them into buttons, this opened a template editor so I was able to do it.
Now I'm wanting to create a template for a repeating stackpanel/grid layout, and I wrote it by hand this time, but I am getting an error that says the "template is not a valid member"
This is the kind of thing I was trying to create, but the Property="Template" bit is underlined in red. Can somebody explain to me the reason behind this? How do I create or initialize the template?
<Style x:Key="LaneStyle" TargetType="{x:Type Grid}">
<Setter Property="Width" Value="760"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Grid}">
<!-- Things here -->
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
If someone could direct me to a tutorial on styles/templates that would be nice as well, haven't been able to find one that explained it in more detail.
Grid is not a control, therefore you cannot apply a ControlTemplate to it. If you're looking for a "repeater" kind of thing, you should be using an ItemsControl.
The best way to create templates/styles is by using Microsoft Blend 3.0/4.0
Over there one can easily find out what's the progress after doing each change.
In your case, a grid cannot be styled as it is a container not a control. If you wish to customize some control need to modify the control template of the control.

WPF: add to margin without overriding existing value

I have two simple margin styles defined, one based off the other.
<Style x:Key="marginStyle" TargetType="FrameworkElement">
<Setter Property="Margin" Value="0,10,20,10"/>
</Style>
<!-- based on marginStyle -->
<Style x:Key="marginIndentStyle" TargetType="FrameworkElement" BasedOn="{StaticResource marginStyle}">
<Setter Property="Margin" Value="10,0,0,0"/>
</Style>
In the derived 'marginIndentStyle' style, I want adjust the margin's Left prop to be 10 more than the Left prop in the base 'marginStyle' style, that is 10 more than what it is currently set at. Using a like above overrides the values completely. I just want to add to it such that the resultant margin for the derived 'marginIndentStyle' style is "10,10,20,10".
Note, I dont want to strictly set its value to 10,10,20,10 b/c I want any changes in the 'marginStyle' style to be reflected in the derived 'marginIndentStyle' style.
Is this possible?
AFAIK, this is not possible without a sizable amount of code.
An easier way will be to have two styles with static margins that are applied to two different panels\decorators.
Something like:
<Border Style="{StaticResource marginIndentStyle}">
<Border Style="{StaticResource marginStyle}">
.....
</Border>
</Border>
This, effectively, will compound the margins. So what ever is in the second border will have the margin as combination of the first and the second margins.

Silverlight stretching TabItem Headers to parent control width

I am creating a SL App that has a TabControl with dynamically created TabItems which are added via code behind. I'd like said TabItems to size proportionally to the TabControls full width, much like what is described here.
Now Silverlight does not have IMultiValueConverter, and not knowing how many tab I will have, I am a bit stuck. Any ways around this?
Thanks for the help.
I haven't done this myself, but I believe you could just implement your own TabPanel and substitute that in via the appropriate template part. See this page for a list of template parts.
public class MyTabPanel : TabPanel
{
// custom layout logic as per your requirements
}
And in styles:
<Style TargetType="TabControl">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="TabControl">
<!-- copy standard template from Blend, but substitute in your MyTabPanel instead -->
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>

silverlight 3.0 grid row selector color

is it possible to specify the color for the row selector in silverlight grid
Yes but you need to copy the control template for the DataGridRowHeader control and place it in a Style object in a resource:-
<UserControl.Resources>
<Style x:Key="CustomRowHeader" TargetType="DataGridRowHeader">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="localprimitives:DataGridRowHeader">
<!-- Copy of the rest of the standard controls template -->
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<UserControl.Resources>
<DataGrid RowHeaderStyle="{StaticResouce CustomRowHeader}" ... >
Now you can fiddle around with the color value and pretty much anything else that is used to render the row selector.
You can probably do this with Blend fairly well if you have it and are familiar with using it. I prefer to copy the template from the documentation. See DataGrid Styles and Templates
No, but it's perfectly possible to subdivide a grid into rows/columns and fill them with rectangles+background or something like that.

Resources