Styling for ListBox, ListView and GridView Items - xaml

The default colors for these controls seems to be similar to the Windows theme colors. How do you change the hover, selected, selected hover and pressed colors (code or XAML)? The following isn't working for the ListView:
<ListView>
<ListViewItemPresenter
PointerOverBackground="#99CEEA"
SelectedPressedBackground="#72BFE9"
SelectedBackground="#72BFE9"
SelectedPointerOverBackground="#99CEEA"
/>

In your VS/Blend Designer, right click on your ListView and select
Edit Additional Templates > Edit Generated Item Container
(ItemContainerStyle) > Edit a Copy...
In the popup window above, if you want this style to be applied to all your ListViewItem, select Apply to all otherwise just give it a name.
I'd recommend to create a new Resource dictionary for storing all ListView related styling. To do so, just hit the New... button and give the resource dictionary a name (e.g. ListViewStyles.xaml).
Finally, hit the OK button and you now have a fully generated style.
In the style's ControlTemplate, you can locate the ListViewItemPresenter control and update its colors accordingly.

The ListViewItemPresenter was in the wrong place in the XAML. Change this:
<ListView>
<ListViewItemPresenter
PointerOverBackground="#99CEEA"
SelectedPressedBackground="#72BFE9"
SelectedBackground="#72BFE9"
SelectedPointerOverBackground="#99CEEA"
/>
</ListView>
to this:
<ListView>
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListViewItem">
<ListViewItemPresenter
PointerOverBackground="#99CEEA"
SelectedPressedBackground="#72BFE9"
SelectedBackground="#72BFE9"
SelectedPointerOverBackground="#99CEEA" />
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ListView.ItemContainerStyle>
<ListView>

Related

What gives the hover and click styles in w8 xaml?

When using a listitem in a w8 app, how can I determine what gives the hover and click styles?
My listview looks like this:
<ListView x:Name="itemsListView"
TabIndex="1"
Visibility="Visible"
Padding="10,0,0,0" Foreground="Black"
ItemsSource="{Binding Nodes.Nodes}"
behaviors:ListViewItemClickedToAction.Action="{Binding SelectNodeAction}"
IsItemClickEnabled="True" FontFamily="Global User Interface"
>
<ListView.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Name}" />
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
When I hover using the mouse I get white letters and an almost white background.
I have tried reusing parts of the adventureworks shopper app, so there are styles from there copied. However, I can't understand what is applied to the ListView items.
You maybe already new this but if you check this screenshot you can see how you easily in VS2012 can create a copy of a built in style. When you press the "Edit a copy ..." a dialog will appear where you can choose where in the Project you want the style to be placed.
You can inherit styles. The inheritence of styles work in the following way:
<Style x:Name="BasicStyle" TargetType="Button">
<Setter Property="Background" Value="Green" />
</Style>
<Style x:Name="ButtonStyle" TargetType="Button" BasedOn="{StaticResource BasicStyle}">
<Setter Property="Foreground" Value="Red" />
</Style>
You can do inheritence in several steps so Another button style can inherit the "ButtonStyle".
You can thus make a style that only contains the Template property if you want to seperate it or reuse the behaviour and look of your style. But you cannot split the Visual State Manager into several styles since if you inherit a style which sets the template property and then if you want to change the Hover state of that style you need to make a copy of the whole template and only change that part in the code.
I Think this would be a nice improvement by MS if you could make a style which only contains the pressed state and then Another style which only contains the hover effect and so on.
I hope this answers your questions :) I would love to answer more questions regarding XAML if you have any!

Forward all properties of a control

I'm making a custom control (that's what I do, I'm a theme designer) and I'm having trouble with something.
The control I'm making is a custom RichTextBox control. Since I want a custom border (rounded edges) arround the RichTextBox, I simply have a control with custom borders, and a child control which is actually a normal RichTextBox. With the BorderStyle set to none, it looks like this:
Now, of course, the parentcontrol needs to have all the properties a normal RichTextbox has.
I manually did all the properties for a regular textbox, but a RichTextBox has far more properties and I was asking myself, is there any way to "forward" all properties of the child control to the main control?
Maybe I'm explaining it a bit oddly. Basically, when you change the main control's "Text" property the RichTextBox's Text property should change too. This means I have to write a custom event for every Property a RichTextBox has, isn't there a way to do this for every property automatically?
Thanks in advance,
Mavamaarten.
Turns out there is no way to do this.
Note: I answered this before the posted had the WinForms tag in their question. (I actually added it based on his reply here.) Still, if you're using WPF, this is how you'd do it...
The easiest thing to do is to replace the ControlTemplate with your internal implementation. In other words, you're replacing the 'visual' portions of the RichTextBox control but you're still a RichTextBox control.
For instance, this is how I replaced the visuals of a TextBox to completely strip away all the chrome and leave it with nothing more but a simple border. But it was still a full TextBox with all properties, etc.
Again, look up Control Templating. You may also want to search for 'Lookless Controls'.
<Style TargetType="{x:Type glc:EditableTextBlock2}" BasedOn="{StaticResource {x:Type TextBox}}">
<Setter Property="VerticalContentAlignment" Value="Center" />
<Setter Property="MinWidth" Value="20" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type glc:EditableTextBlock2}">
<Border Name="Bd"
SnapsToDevicePixels="True"
BorderThickness="1"
BorderBrush="{TemplateBinding Border.BorderBrush}"
Background="{TemplateBinding Border.Background}" >
<ScrollViewer Name="PART_ContentHost"
SnapsToDevicePixels="True"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}" />
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>

Giving a specific style to DataGrid element (implicitly)

I'm trying implicitly apply a style for DataGrid and TextBlocks.
For TextBlock's ForeGround I need White color.
For DataGrid's rows I need Black Color.
Beside this I need White again for DataGrid's header columns.
When I globally apply an implicit style for on MainPage by
<UserControl>
<UserControl.Resorces>
<Style targetType="TextBlock">
<Setter Property="Foreground" Value="White"/>
</Style>
</UserControl.Resorces>
</UserControl>
Making TextBlock's Foreground White operation is done! But beside this all of elements
in DataGrid (By default content elements are textblock's I think) turn to White color.
It doesn't look good White on white as you guess :)
So how can I particularly specify DataGrid's elements Foreground to black?
I can do it by using same technic shown below ,but this is an expensive operation for each DataGrid. As a con more I want DataGrid's HeaderColumns white again.This operation make them all black.
Is there an explicit way such as we do in css styles?
Here is what I tried to achieve this goal by Control template. But no chance because of being DataGrid's ContentControl is dynamic.
<DataGrid>
<DataGrid.Resources>
<Style targetType="TextBlock">
<Setter Property="Foreground" Value="Black"/>
</Style>
<DataGrid.Resources>
In fact we use Telerik's RadGridView but I give a sdk's DataGrid example to make question more global.
<Style TargetType="sdk:DataGrid">
<Setter Property="Foreground" Value="Black"/>
<Setter Property="RowDetailsTemplate" Value="{StaticResource DataTemplate1}"/>
<Setter Property="Template" Value="{StaticResource ControlTemplate1}"/>
</Style>
<ControlTemplate x:Key="ControlTemplate1" TargetType="sdk:DataGrid">
<Grid/>
</ControlTemplate>
<DataTemplate x:Key="DataTemplate1">
<Grid/>
</DataTemplate>
Thanks in advance!
If it were me I would pull out the full control templates and style them accordingly instead of trying to just do adhoc setter changes to override bits of the original template. In Expression Blend right click, choose "Edit Template -> Edit A Copy" and break out the templates for your rows etc and apply those implicitly with StaticResource instead.

Where the styles are taken from?

I have a question
I've created new win8 metro app and I don't get from where it is taking styles.
I've change in App.xaml name of styles file. My Style.xaml looks like this
<!-- Page layout roots typically use entrance animations and a theme-appropriate background color -->
<Style x:Key="LayoutRootStyle" TargetType="Panel">
<Setter Property="Background" Value="White"/>
<Setter Property="ChildrenTransitions">
<Setter.Value>
<TransitionCollection>
<EntranceThemeTransition/>
</TransitionCollection>
</Setter.Value>
</Setter>
</Style>
But still I got gold letters, my buttons change white when I'm hover it. Same like with default StandardStyles.xaml
What am I doing wrong ? Or it works just fine ?
The style you show would only affect the the panel that uses that style. If you put a button inside a the panel, the panel's style does not cascade to the button. The button has its own style, as you correctly deduced from standardstyles.xaml, that controls its appearance. If you want to change the style of the button, you need to create a new style or update the current style.
The problem is you gave your style a key using the x:Key attribute. Once a style has a key it will only be applied to controls that implicitly use that named style. If you remove the key, the style should be applied to all controls that match TargetType (in this case Panel).

How to disable ListView's Hover and Tile effects?

I want to disable Tile effect that is some kind of pushed effect and hover background color effect of ListView control, how can i do that?
Thanks
After some googling I found that the highlighting happens in the ListViewItemPresenter, which turns out to be pretty hidden. It's located inside the ControlTemplate of an ListViewItem, which is the ItemContainer for the ListView. The simplest way I've found to disable the effect is to simply override this ControlTemplate:
<ListView>
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<ContentPresenter/>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ListView.ItemContainerStyle>
<TextBlock Text="List Item" />
...
<TextBlock Text="List Item" />
source: https://blog.jonstodle.com/uwp-listview-without-highlighting-and-stuff/
Look at this question:
Disable cross-slide selection for a listview
You can also make changes to the template to remove any visual states and adornments - go to the designer and right click your ListView/Edit Additional Templates/Edit Generated Item Container (ItemContainerStyle)/Edit a Copy... - that will extract the template you can modify using your preferred method.