I have a WPF Listview containing another UserControl. It works fine, but I cannot remove the mouse blu highlight and selected.
Here the code:
<UserControl.Resources>
<Style TargetType="{x:Type ListViewItem}">
<Setter Property="Background" Value="Transparent" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ListViewItem}">
<Border
BorderBrush="Transparent"
BorderThickness="0"
Background="{TemplateBinding Background}">
<GridViewRowPresenter HorizontalAlignment="Stretch" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" Width="Auto" Margin="0" Content="{TemplateBinding Content}"/>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<DataTemplate DataType="{x:Type vm:ElementViewModel}" x:Key="ElementTemplate">
<vw:ElementView />
</DataTemplate>
</UserControl.Resources>
<GroupBox Header="{x:Static Translate:Translate.CreateLoop}">
<ListView ItemsSource="{Binding Path=ElementList, UpdateSourceTrigger=PropertyChanged}"
ItemTemplate="{StaticResource ElementTemplate}"
Background="{StaticResource EnvLayout}"
BorderBrush="Transparent">
<ListView.ItemContainerStyle>
<Style TargetType="{x:Type ListViewItem}">
<Style.Resources>
<!--Foreground for Selected ListViewItem-->
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}" Color="Black"/>
<!--Background for Selected ListViewItem-->
<SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Transparent"/>
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Transparent"/>
<SolidColorBrush x:Key="ItemBackgroundHover" Color="Transparent"/>
</Style.Resources>
</Style>
</ListView.ItemContainerStyle>
</ListView>
</GroupBox>
the datatemplate is used to bind the view with the corresponding viewmodel.
Here the mouse over style I'd like to remove
Try this
Add a style
<UserControl.Resources>
<Style x:Key="MyStyle" TargetType="ListViewItem">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListViewItem">
<Grid Background="{TemplateBinding Background}">
<ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalAlignment}"
/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</UserControl.Resources>
Remove your style and instead
ItemContainerStyle="{StaticResource MyStyle}"
Related
I would put a TextBlock inside the header of my TabItem as I have the problem concerning the missing underscore. I changed my style as follows:
<Style x:Key="TabItemDistintaStyle" TargetType="{x:Type TabItem}">
<Setter Property="FocusVisualStyle" Value="{StaticResource TabItemFocusVisual}"/>
<Setter Property="Foreground" Value="White"/>
<Setter Property="Padding" Value="6,1,6,1"/>
<Setter Property="BorderBrush" Value="{StaticResource TabControlNormalBorderBrush}"/>
<Setter Property="Background" Value="{StaticResource ButtonNormalBackground}"/>
<Setter Property="HorizontalContentAlignment" Value="Stretch"/>
<Setter Property="VerticalContentAlignment" Value="Stretch"/>
<Setter Property="HeaderTemplate">
<Setter.Value>
<DataTemplate>
<TextBlock />
</DataTemplate>
</Setter.Value>
</Setter>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type TabItem}">
<Grid SnapsToDevicePixels="true">
<Border x:Name="Bd" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="1,1,1,0" Padding="{TemplateBinding Padding}" CornerRadius="6,210,0,0">
<DockPanel>
<ContentPresenter x:Name="Content" DockPanel.Dock="Left" HorizontalAlignment="{Binding HorizontalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}" VerticalAlignment="{Binding VerticalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" ContentSource="Header" RecognizesAccessKey="True"/>
<Button x:Name="cmdClose" DockPanel.Dock="Right"
Command="ApplicationCommands.Close"
Margin="7,0,3,0" Style="{DynamicResource ButtonDistintaCloseStyle}" RenderTransformOrigin="0.5,0.5">
<Button.RenderTransform>
<TransformGroup>
<ScaleTransform/>
<SkewTransform/>
<RotateTransform/>
<TranslateTransform/>
</TransformGroup>
</Button.RenderTransform>
</Button>
</DockPanel>
</Border>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
But it does not work, as the first underscore is still missing. What's wrong?
You need bind (set) your TextBlock's Text with TabItem's "Header" property,
i think this sample helps you:
<Window x:Class="MyWpfAppSample1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:MyWpfAppSample1"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800" WindowStartupLocation="CenterOwner">
<Window.Resources>
<Style x:Key="MyTabItemStyle"
TargetType="{x:Type TabItem}">
<Setter Property="FocusVisualStyle" Value="{x:Null}" />
<Setter Property="HorizontalContentAlignment" Value="Stretch" />
<Setter Property="VerticalContentAlignment" Value="Stretch" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type TabItem}">
<Grid x:Name="Root"
Width="180"
Height="45"
Margin="0,0,0,0"
SnapsToDevicePixels="true">
<TextBlock x:Name="contentPresenter"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Focusable="False"
FontSize="14"
Foreground="LightCoral"
SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
Text="{TemplateBinding Header}" />
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
<Grid>
<TabControl Margin="0,5,0,0"
FocusVisualStyle="{x:Null}">
<TabItem Header="My First Tab"
IsSelected="{Binding FirstTabItemSelected}"
Style="{DynamicResource MyTabItemStyle}">
Tab Item1
</TabItem>
<TabItem Header="My Second Tab"
IsSelected="{Binding SecondTabItemSelected}"
Style="{DynamicResource MyTabItemStyle}">
TabItem2
</TabItem>
</TabControl>
</Grid>
</Window>
Result:
I'm simply trying to change the highlighted colour of a listviewitem in WPF. The solutions I've found online including Stackoverflow are having no effect on my listview. Am I going crazy? Am I missing something? Please show me how to do this. Here is my example code. The items are still showing the default blue.
<Window.Resources>
<Style TargetType="ListViewItem">
<Style.Resources>
<SolidColorBrush x:Key="{x:Static
SystemColors.HighlightBrushKey}" Color="Red" />
</Style.Resources>
</Style>
</Window.Resources>
<Grid>
<ListView VerticalAlignment="Top" Background="#2e2e2e"
Foreground="White">
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Style.Resources>
<SolidColorBrush x:Key="{x:Static
SystemColors.HighlightTextBrushKey}"
Color="Red" />
<SolidColorBrush x:Key="{x:Static
SystemColors.HighlightBrushKey}"
Color="Purple" />
</Style.Resources>
</Style>
</ListView.ItemContainerStyle>
<ListViewItem Content="ITEM" />
<ListViewItem Content="ITEM" />
<ListViewItem Content="ITEM" />
<ListViewItem Content="ITEM" />
<ListViewItem Content="ITEM" />
</ListView>
<Button VerticalAlignment="Bottom" Height="50" />
</Grid>
You have to override the ControlTemplate of the ListViewItem. This way you can also design apart form the highlighting any other visual user interaction behavior and create transition animations.
<Style TargetType="ListBoxItem">
<Style.Resources>
<SolidColorBrush x:Key="HighlightTextBrushKey"
Color="Red" />
<SolidColorBrush x:Key="HighlightBrushKey"
Color="Purple" />
<SolidColorBrush x:Key="HighlightMouseOverBrushKey"
Color="{Binding Source={StaticResource HighlightBrushKey}, Path=Color}"
Opacity="0.3" />
</Style.Resources>
<Style.Setters>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListBoxItem">
<Border BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}"
Background="{TemplateBinding Background}"
Padding="{TemplateBinding Padding}"
Margin="{TemplateBinding Margin}">
<ContentPresenter />
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Background" Value="{StaticResource HighlightBrushKey}"/>
<Setter Property="Foreground" Value="{StaticResource HighlightTextBrushKey}"/>
</Trigger>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="{StaticResource HighlightMouseOverBrushKey}"/>
<Setter Property="Foreground" Value="{StaticResource HighlightTextBrushKey}"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style.Setters>
</Style>
An alternative to the triggers you can animate transitions with the help of the VisualStateManager
I make use of a resource dictionary with all my styles, icons and more.
Now I'd like to add my own titlebar implementing the title of the solution, my image and a ContentPresenter.
When using the WindowStyle I'd like to add application specific items inside this ContentPresenter, but I don't know how to proceed.
This is my WindowStyle. Inside the Grid you'll find the ContentPresenter I'd like to fill.
<Style TargetType="{x:Type Window}" x:Key="tkDarkWindowStyle">
<Setter Property="AllowsTransparency" Value="True"></Setter>
<Setter Property="Foreground" Value="{StaticResource tkBrandBlueBrush}"></Setter>
<Setter Property="Background" Value="{StaticResource exQuiteDarkBrush}"></Setter>
<Setter Property="WindowStyle" Value="None"></Setter>
<Setter Property="BorderThickness" Value="0"></Setter>
<Setter Property="BorderBrush" Value="{StaticResource exQuiteDarkBrush}"></Setter>
<Setter Property="WindowChrome.WindowChrome">
<Setter.Value>
<WindowChrome CaptionHeight="80" />
</Setter.Value>
</Setter>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Window">
<DockPanel LastChildFill="True">
<Border Background="{TemplateBinding Background}" DockPanel.Dock="Top"
Height="80" x:Name="titlebar">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<DockPanel Grid.Column="0">
<Path DockPanel.Dock="Left" Margin="10" Stretch="Uniform" Fill="{TemplateBinding Foreground}" Data="{Binding Source={StaticResource tkPrimaryLogo}}" VerticalAlignment="Center">
</Path>
<Label Content="{TemplateBinding Title}" Foreground="{TemplateBinding Foreground}" Margin="10" DockPanel.Dock="Left" FontSize="26" VerticalAlignment="Center"/>
</DockPanel>
<ContentPresenter Grid.Column="1"/>
</Grid>
</Border>
<Border Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="1" Padding="4">
<ContentPresenter/>
</Border>
</DockPanel>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
I used the style like this and tried to edit the template and add for example some buttons to the titlebar.
<Window.Resources>
<Style TargetType="{x:Type Window}" x:Key="newWindow" BasedOn="{StaticResource tkDarkWindowStyle}">
<!--here add application specific items? -->
</Style>
</Window.Resources>
How can I use the WindowStyle in my application and add specific items to the space i left at the titlebar?
Since the Window only has a single Content property, it makes no sense to include more than one <ContentPresenter /> element in the ControlTemplate.
You may want to create a custom class that inherits from Window and adds a dependency property called "TitleBarContent" or something. You can then add a ContentControl to the template that binds to this property:
<ContentControl Content="{TemplateBinding TitleBarContent}" />
You can set the value of the dependency property in a style setter as usual:
<Style TargetType="{x:Type local:YourWindowClass}" x:Key="newWindow" BasedOn="{StaticResource tkDarkWindowStyle}">
<Setter Property="TitleBarContent">
<Setter.Value>
<TextBlock>title...</TextBlock>
</Setter.Value>
</Setter>
</Style>
An entry in my Application.Resources ResourceDictionary is a control template that, slimmed down, looks similar to the following:
<ControlTemplate TargetType="{x:Type ToggleButton}">
<Border>
<Border.BorderBrush>
<SolidColorBrush Color="{Binding Path=BorderColor, RelativeSource={RelativeSource AncestorType=UserControl}" />
</Border.BorderBrush>
</Border>
</ControlTemplate>
Each UserControl has its own property BorderColor which this pulls from. In this example, the binding fails to find the property.
Cannot find source for binding with reference 'RelativeSource
FindAncestor, AncestorType='System.Windows.Controls.UserControl',
AncestorLevel='1''.
However, it works in another entry in the dictionary:
<Style TargetType="TextBox" BasedOn="{StaticResource {x:Type TextBox}}">
<Setter Property="BorderBrush" Value="{Binding Path=BorderColor, RelativeSource={RelativeSource AncestorType=UserControl}"/>
</Style>
How can I fix the binding in the first example? Preferably I would like to not need additional properties on the instance of each control in the user control.
Two suggestions:
If the ControlTemplate is part of a Style you could set the BorderBrush property of the ToggleButton to the SolidColorBrush with the binding and use a TemplateBinding in the template:
<Style x:Key="myStyle" TargetType="ToggleButton">
<Setter Property="BorderBrush">
<Setter.Value>
<SolidColorBrush Color="{Binding Path=BorderColor, RelativeSource={RelativeSource AncestorType=UserControl}}" />
</Setter.Value>
</Setter>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ToggleButton}">
<Border BorderBrush="{TemplateBinding Background}" BorderThickness="10">
<TextBlock>....</TextBlock>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
If you want to define a standalone ControlTemplate for some reason a workaround would be to bind to a Brush property instead of a Color property:
<ControlTemplate TargetType="{x:Type ToggleButton}">
<Border BorderBrush="{Binding Path=BorderBrushProperty, RelativeSource={RelativeSource AncestorType=UserControl}}" BorderThickness="10">
<TextBlock>....</TextBlock>
</Border>
</ControlTemplate>
It works if you use Background instead of BorderColor. Is BorderColor your own property?
<Window.Resources>
<ControlTemplate x:Key="template" TargetType="{x:Type ToggleButton}">
<Border>
<Border.BorderBrush>
<SolidColorBrush Color="{Binding Path=Background,RelativeSource={RelativeSource AncestorType=UserControl}}" />
</Border.BorderBrush>
</Border>
</ControlTemplate>
</Window.Resources>
<UserControl Background="Aqua">
<ToggleButton Template="{StaticResource template}"></ToggleButton>
</UserControl>
I want to hide/show WPF RichTextBox left border sometimes in code behind and also for right border sometimes.
Can this be achieved?
I've already tried in XAML like this borderthickness ="3,3,0,3". It hides my right side, now I need to show that right side border and also hide my right side border.
How do I do that?
Updated:
<Window x:Class="View.SingleLineTextMode"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="SingleLineTextMode" Height="300" Width="300" WindowState="Maximized" WindowStyle="None" WindowStartupLocation="CenterScreen" Background="Black">
<Window.Resources>
<Style x:Key="MyStyle" BasedOn="{x:Null}" TargetType="{x:Type RichTextBox}">
<Setter Property="BorderThickness" Value="3"/>
<Setter Property="Padding" Value="0,5,0,0"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type RichTextBox}">
<Border x:Name="bg" BorderBrush="Yellow" BorderThickness="3,3,0,3" Background="{TemplateBinding Background}">
<ScrollViewer x:Name="PART_ContentHost" IsDeferredScrollingEnabled="True" CanContentScroll="False" />
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="BorderBrush" TargetName="bg" Value="Yellow"/>
<Setter Property="BorderThickness" TargetName="bg" Value="3,3,0,3"/>
</Trigger>
<Trigger Property="IsFocused" Value="True">
<Setter Property="BorderBrush" TargetName="bg" Value="Yellow"/>
<Setter Property="BorderThickness" TargetName="bg" Value="3,3,0,3"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
<Grid>
<!--<RichTextBox Name="txtAppendValue" Height="60" Width="1920" Style="{StaticResource MyStyle}" Margin="0,0,-10,0" FontSize="60" FontFamily="Arial" IsReadOnly="True" Focusable="False" Cursor="Arrow" BorderThickness="3,3,3,3" IsUndoEnabled="False" UndoLimit="0" TextOptions.TextFormattingMode="Display" VirtualizingPanel.IsVirtualizing="True" VirtualizingPanel.VirtualizationMode="Recycling" VirtualizingPanel.CacheLength="2,3" VirtualizingPanel.CacheLengthUnit="Page" TextBlock.LineHeight="100" Padding="0">
</RichTextBox>-->
<RichTextBox Name="txtAppendValue" HorizontalAlignment="Center" VerticalAlignment="Center" Height="60" Width="1920" Style="{StaticResource MyStyle}" Margin="0,0,0,0" FontSize="40" FontFamily="Arial" IsReadOnly="True" Focusable="False" Cursor="Arrow" IsUndoEnabled="False" UndoLimit="0" TextOptions.TextFormattingMode="Display" VirtualizingPanel.IsVirtualizing="True" VirtualizingPanel.VirtualizationMode="Recycling" VirtualizingPanel.CacheLength="2,3" VirtualizingPanel.CacheLengthUnit="Page" TextBlock.LineHeight="100" >
</RichTextBox>
<Label Name="lblStatusUpdate" ></Label>
</Grid>
</Window>
The above one is my XAML.
txtAppendValue.BorderThickness=new Thickness("0,3,0,3");
But its not working.
Regards
Arjun
You can do the same thing from code like this:
myRichTextBox.BorderThickness = new Thickness(3,3,0,3);
Then if all borders need to have same thickness, you can use only one number:
myRichTextBox.BorderThickness = new Thickness(3);
Edit:
Since you are using style, you can define multiple styles and then switch between them:
<Style x:Key="MyStyleNoBorders" BasedOn="{x:Null}" TargetType="{x:Type RichTextBox}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type RichTextBox}">
<Border x:Name="bg" BorderBrush="Yellow" BorderThickness="0,3,0,3" Background="{TemplateBinding Background}">
<ScrollViewer x:Name="PART_ContentHost" IsDeferredScrollingEnabled="True" CanContentScroll="False" />
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Style x:Key="MyStyleBothBorders" BasedOn="{x:Null}" TargetType="{x:Type RichTextBox}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type RichTextBox}">
<Border x:Name="bg" BorderBrush="Yellow" BorderThickness="3" Background="{TemplateBinding Background}">
<ScrollViewer x:Name="PART_ContentHost" IsDeferredScrollingEnabled="True" CanContentScroll="False" />
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
Make sure your RichTextBox is not too wide to fit the screen, or you will not be able to see the borders. Then in code just use a call to change styles:
txtAppendValue.Style = FindResource("MyStyleNoBorders") as Style;
and
txtAppendValue.Style = FindResource("MyStyleBothBorders") as Style;