I have a tab control with two different potential item templates:
<TabControl ItemTemplateSelector="{StaticResource tabTemplateSelector}"/>
Now, I also have styles for it:
<Style TargetType="{x:Type TabItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type TabItem}">
...
The thing is, I want the tab item style template to be different depending on the value of ItemTemplateSelector. How can I do this? Basically, I don't want that entire style rule to be applied to every tab item, just the ones with a specific tab item template.
Update: to make it clearer, the style has TargetType set to TabItem. I want to apply that style only to those tab items that have a specific item template.
The ContentPresenter in the ControlTemplate will display what's in the ItemTemplate. So you won't be able to switch the Template from inside the ControlTemplate.
Instead, you could use a DataTrigger to set the Template. The DataTrigger will check if the ItemTemplate that will be generated should have the other Template or not.
You will probably need a converter for this but here is an easy example. Say that your ItemTemplateSelector returns the other DataTemplate if Name is equal to "Tab 2". Then your Style with the DataTrigger would look like this
<Style TargetType="TabItem">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="TabItem">
<!-- Your first Template -->
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<DataTrigger Binding="{Binding Name}" Value="Tab 2">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="TabItem">
<!-- Your other Template -->
</ControlTemplate>
</Setter.Value>
</Setter>
</DataTrigger>
</Style.Triggers>
</Style>
Related
I am making a ListView which loads users in from an Active Directory. To accomplish the brand/styling of the company I am developing the application for I would like to tweak some of the styling of the ListView element.
I have made it so the border of the headers in the Listview are transparent. In the editor in Visual Studio it looks how I want it to be, but when I look at the headers in the ListView in runtime I still get to see borders separating the headers (See image below).
https://i.gyazo.com/99dc8d60d6c5b2e1761456df685d850f.png
I have already tried Googling and I even went to the second page of the Google search result. Can you imagine?
Down here is the style I have used for the headers in my XAML file
<Style x:Key="ListViewHeaderStyle" TargetType="{x:Type GridViewColumnHeader}">
<Setter Property="Background" Value="Transparent"></Setter>
<Setter Property="BorderBrush" Value="Transparent"></Setter>
<Setter Property="IsHitTestVisible" Value="False"></Setter>
</Style>
What I want is to remove those borders separating the headers in my ListView element.
You can override the template of the GridViewColumnHeader
<Window.Resources>
<Style x:Key="GridHeader" TargetType="{x:Type GridViewColumnHeader}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type GridViewColumnHeader}">
<TextBlock Text="{TemplateBinding Content}" Padding="5"
Width="{TemplateBinding Width}" TextAlignment="Right" />
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
<ListView>
<ListView.View>
<GridView ColumnHeaderContainerStyle="{StaticResource GridHeader}">
</ListView.View>
</ListView>
Solution taken form here: Remove Separators in ListView Columns - WPF
hi im new in wpf I have a control that has been created in user control I want to create this control in style I do not know what to do:
this is xaml codes:
https://gist.github.com/ghost1372/8b3db759241b3ddb838789e446efb0b4#file-multiselectcombo-xaml
and this is cs codes:
https://gist.github.com/ghost1372/8b3db759241b3ddb838789e446efb0b4#file-multiselectcombo-cs
Well basically all you have to do is to define a Style and set the properties using Setters. Something like:
<Style x:Key="MultiSelectCombo" TargetType="{x:Type ComboBox}">
<Setter Property="SnapsToDevicePixels" Value="True"/>
...
<Setter Property="Template">
<Setter.Value>
<!-- Put the control template you used in your UserControl -->
</Setter.Value>
</Setter>
<Setter Property="ItemTemplate">
<Setter.Value>
<!-- Put the DataTemplate you used in your UserControl -->
</Setter.Value>
</Setter>
</Style>
Now this style can be applied to any ComboBox and will change its look. However if you want also change the behavior you need to create a MultiSelectCombo class that overrides ComboBox and implements the logic you want.
I use a custom control from an external library, it has a property with ControlTemplate.
<Style x:Key="{x:Type controls:HeaderDiscountButton}" TargetType="{x:Type controls:HeaderDiscountButton}" BasedOn="{StaticResource {x:Type core:ValueButton}}">
<Setter Property="core:ButtonWithPopup.PopupTemplate">
<Setter.Value>
<ControlTemplate>
some complicated xaml code
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
I would like to override it, but use old one in my new ControlTemplate, for example
<Style x:Key="{x:Type controls:HeaderDiscountButton}" TargetType="{x:Type controls:HeaderDiscountButton}" BasedOn="{StaticResource {x:Type core:ValueButton}}">
<Setter Property="core:ButtonWithPopup.PopupTemplate">
<Setter.Value>
<ControlTemplate>
<StackPanel>
<Border>some complicated xaml code</Border>
<Border>some additional xaml code</Border>
</StackPanel>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
I would like to avoid copying whole old code
This is not possible I am afraid. You can indeed base a Style on another Style and override specfic setters but you cannot "override" or inherit only a part of a ControlTemplate. Unfortunately you must (re)define the entire template as a whole.
So in this case you actually need to copy the (whole) original template and then edit it as per your requirements.
I made a custom resource dictionary style for a TreeViewItem, but I am having difficulties with it.
<Style x:Key="StageTreeViewItem" TargetType="{x:Type TreeViewItem}" BasedOn="{StaticResource {x:Type TreeViewItem}}">
<Setter Property="Foreground" Value="Gold"/>
<Setter Property="FontFamily" Value="ArialN"/>
<Setter Property="FontSize" Value="20"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="TreeViewItem">
<Grid>
<Image Name="PrimaryButtonImage" Source="pack://application:,,,/Images/TreeViewItem/TreeViewItem_Normal.png"/>
<ContentPresenter VerticalAlignment="Center" HorizontalAlignment="Center"/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
The content/header of the TreeViewItem does not exist. I put "Stage One" as Header of the TreeViewItem, but it doesn't show up. Also, if I add multiple tree view items on another, it does not expand at all.
Another thing:
How can I remove the highlights when I select the tree view item? I want it to be transparent even when I hover over it and even when I click it. I don't want anything to happen, but I just don't know how, I tried everything.
Your provided code is not making it clear how you're setting header of TreeViewItem.
For other part of the question, you can use Triggers for events happening in WPF forms. Also have a look at this link, as you'll have to define a template for changing background color on mouse hover.
IsMouseOver Trigger not working in WPF
I've got a custom control in WPF, which has a variety of dependency properties that allow visual customization. For the sake of brevity I won't post the entire control, but it basically is setup like this:
<UserControl.Resources>
<Style TargetType="{x:Type MyControl}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type MyControl}">
<Border BorderBrush="{TemplateBinding BorderColor}">
// more stuff here
<ContentPresenter/>
</Border>
</ControlTemplate>
</Setter>
</Setter>
</Style>
</UserControl.Resources>
The BorderColor property works fine if I set it directly, like this:
<ctl:MyControl BorderColor="Brushes.Red">....</ctl:MyControl>
But I want to set it application-wide. The problem I have is if I simply set the style with no key, it does not apply. Like this:
<Window.Resources>
<Style TargetType="{x:Type ctl:MyControl}">
<Setter Property="BorderColor" Value="Brushes.Red"/>
</Style>
</Window.Resources>
This does not do anything to the control. So I thought I'd just set a key and apply that style, like this:
<Style TargetType="{x:type ctl:MyControl}" x:Key="myStyle">....</Style>
<ctl:MyControl Style="{StaticResource myStyle}">.....</ctl:MyControl>
But this causes the control to vanish, I'm assuming because it's removing the Template. What am I doing wrong? With other framework controls you can just set the properties you want without losing the control template.
You need to inherit from the default style you have created.
inherit style from default style