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!
Related
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>
I'm making a ResourceDictionary of common styles that are used throughout my application and one of them is:
<Style x:Key="ME_BASE_AppbarButtonSaveStyle"
TargetType="AppBarButton">
<Setter Property="Label"
Value="Save" />
<Setter Property="ToolTipService.ToolTip"
Value="Save" />
<Setter Property="Icon">
<Setter.Value>
<FontIcon FontFamily="Segoe MDL2 Assets"
Glyph="" />
</Setter.Value>
</Setter>
</Style>
It's all ok if I apply the style only one AppbarButton on the Page, but if I want to have two buttons with the same style, I get the following error:
The parameter is incorrect
It's of ok (no error) if I remove the icon property out of the style...
But that's kind of missing the point...
Anyone experienced something similar? Perhaps...
Thank you for all the help.
Error HRESULT E_Fail has been returned from a call to a COM component.
This error will occurred when you use this style for the second AppBarButton. This error usually happens when a reference to a style or an event handler that does not exist or is not with the context of the XAML, you can see the exception information of your problem:
If you read this document: XAML resources must be shareable, you will find:
Custom types used as resources can't have the UIElement class in their inheritance, because a UIElement can never be shareable (it's always intended to represent exactly one UI element that exists at one position in the object graph of your runtime app).
Whether a Icon property of AppBarButton or a FontIcon derives from UIElement, so I guess this is the reason why can't this property be styled in the resource dictionary.
Besides, I will consider if this is a right direction to define the Icon property for each AppBarButton in the style, normally I'd like give each button a different icon as content.
But if you insist to do this, I can provide you a workaround method by defining the Content of the AppBarButton, this is the construction of your AppBarButton:
You use a FontIcon as the content of the AppBarButton, so we can modify your style like this:
<Style x:Key="ME_BASE_AppbarButtonSaveStyle" TargetType="AppBarButton">
<Setter Property="Label" Value="Save" />
<Setter Property="ToolTipService.ToolTip" Value="Save" />
<Setter Property="ContentTemplate">
<Setter.Value>
<DataTemplate>
<FontIcon FontFamily="Segoe MDL2 Assets"
Glyph="" />
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
Given a style in a Page.Resource:
<Style x:Name="ItemTitle" TargetType="TextBlock">
<Setter Property="FontSize" Value="16"></Setter>
<Setter Property="FontWeight" Value="Bold"></Setter>
</Style>
It is correctly applied to any regular TextBlock on the same page.
However, when I use a DataTemplate for an Item in a GridView on that page, this style does not apply.
<DataTemplate x:Key="Output" x:DataType="vm:Output">
<TextBlock Text="{x:Bind Text}"></TextBlock>
</DataTemplate>
It does work when I apply the style explicitly on the DataTemplate, e.g.:
<DataTemplate x:Key="Output" x:DataType="vm:Output">
<TextBlock Style="{StaticResource ItemTitle}" Text="{x:Bind Text}"></TextBlock>
</DataTemplate>
Does anyone know what's up?
It's expected and intentional. If it doesn't derive from Control (like DataTemplate) then it won't inherit an implicit style unless they're in the application resource dictionaries as global defaults.
Or more specifically;
Templates are viewed as an encapsulation boundary when looking up an implicit style for an element which is not a subtype of Control.
Hope this helps. Cheers.
Addendum:
If it's a situation where you have a lot of the same element nested in a Template you can just set it once and allow it to inherit to all the nested controls of the type like (in pseudo);
<Parent>
<Parent.Resources>
<Style TargetType="TextBlock" BasedOn="{StaticResource ItemTitle}"/>
<Parent.Resources>
<!-- These will all inherit the Style resource now,
without explicit style setting individually. -->
<TextBlock/>
<TextBlock/>
<TextBlock/>
</Parent>
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>
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.