I've created a custom style for the context menu. But I have to put 3 lines of code under each widget(Textbox):
<TextBox.ContextMenu>
<ContextMenu/>
</TextBox.ContextMenu>
Why do I have to do that ? My tooltip style works without any extra code.
My context menu code:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Color x:Key="BackgroundColor" A="255" R="19" G="19" B="19"/>
<BitmapImage x:Key="BiCut" UriSource="Images/cut.tif"/>
<BitmapImage x:Key="BiCopy" UriSource="Images/copy.tif"/>
<BitmapImage x:Key="BiPaste" UriSource="Images/paste.tif"/>
<SolidColorBrush x:Key="BorderBrush" Color="#ECECEC"/>
<Style TargetType="ContextMenu">
<Setter Property="Foreground" Value="{StaticResource BorderBrush}"/>
<Setter Property="SnapsToDevicePixels" Value="True" />
<Setter Property="OverridesDefaultStyle" Value="True" />
<Setter Property="Grid.IsSharedSizeScope" Value="true" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ContextMenu">
<Border BorderThickness="1" BorderBrush="#2468d9" Padding="2" Background="#131313">
<StackPanel Orientation="Horizontal">
<StackPanel Orientation="Horizontal" Height="20">
<Image Source="{StaticResource BiCut}" Width="20"/>
<Button Content="Ausschneiden" Margin="5,0,0,0"/>
<TextBlock Text="Strg+X" TextAlignment="Center" VerticalAlignment="Center" Margin="5,0,0,0"/>
</StackPanel>
</StackPanel>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
Controls do not have ContextMenu by default (check property - it is null). So your style will be not applied to any Control.
Tooltip is null by defult, but it will be filled by TooltipService if it is necessary.
There is no service to fill your ContextMenu for all of your element. Use a default style for this (for Control).
You can create a default Style for your TextBox and add that ContextMenu in Style. In this way you will have that ContextMenu for every TextBox you will add in your application and you won't have to add these three lines everywhere.
<Style TargetType="{x:Type TextBox}" >
<Setter Property="ContextMenu">
<Setter.Value>
<ContextMenu />
</Setter.Value>
</Setter>
</Style>
Related
I'm trying to override Avalonia's Slider theme. It's working on the Slider as a whole, and the Thumb gets updated too, but for some reason I'm unable to apply custom theme to the RepeatButtons PART_DecreaseButton and PART_IncreaseButton
In this case, the background DOES get set to Green, but the template property isn't being applied. Why?
<Style Selector="local|MediaPlayer Slider.seekBar RepeatButton#PART_DecreaseButton">
<Setter Property="Focusable" Value="False" />
<Setter Property="Background" Value="Green" />
<Setter Property="Template">
<ControlTemplate>
<Grid>
<Rectangle Height="11" /> <!-- Fill="#01FFFFFF" -->
<Border Padding="0" BorderThickness="1,1,0,1" BorderBrush="{DynamicResource MediaPlayerSeekBarBorderBrush}"
Background="{DynamicResource MediaPlayerSeekBarDecreaseBrush}" Height="7"
IsHitTestVisible="False" />
</Grid>
</ControlTemplate>
</Setter>
</Style>
In contrast, the thumb is working
<Style Selector="local|MediaPlayer Slider.seekBar Thumb">
<Setter Property="Template">
<ControlTemplate>
<Border BorderThickness="1" Height="11" Width="9"
Background="{DynamicResource MediaPlayerThumbFillBrush}"
BorderBrush="{DynamicResource MediaPlayerThumbBorderBrush}" />
</ControlTemplate>
</Setter>
</Style>
Default Slider.xaml implementation is here.
EDIT: Here's a simple window to reproduce it.
<Window xmlns="https://github.com/avaloniaui"
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"
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
x:Class="HanumanInstitute.MediaPlayer.Avalonia.Sample.Window1"
Title="Window1" Width="400" Height="200">
<Window.Styles>
<Style Selector="Slider.seekBar RepeatButton#PART_DecreaseButton">
<Setter Property="Focusable" Value="False" />
<Setter Property="Background" Value="Black" />
<Setter Property="Template">
<ControlTemplate>
<Grid>
<Border Padding="0" BorderThickness="3,3,0,3" BorderBrush="Blue"
Background="Blue" Height="7"
IsHitTestVisible="False" />
</Grid>
</ControlTemplate>
</Setter>
</Style>
<Style Selector="Slider.seekBar Thumb">
<Setter Property="Template">
<ControlTemplate>
<Border Width="10" Height="30" Background="Gray" />
</ControlTemplate>
</Setter>
</Style>
</Window.Styles>
<Slider Classes="seekBar" VerticalAlignment="Center" />
</Window>
I got the following issue which I need some help with.
(The code is simplified to show the problem I'm having)
I got a repeat button with a contenttemplate and style:
<UserControl x:Class="SR.Testing.MyUserControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<Border x:Name="Border" SnapsToDevicePixels="True">
<StackPanel>
<RepeatButton x:Name="IncreaseButton" ContentTemplate="{StaticResource ArrowUpNormal}" Style="{StaticResource IncreaseRepeatButtonStyle}" Click="IncreaseClick" />
</StackPanel>
</Border>
</Grid>
</UserControl>
This is the datatemplate and the style:
<Geometry x:Key="UpArrowGeometry">M0,5 L4.5,.5 9,5 6,5 4.5,3.5 3,5 z</Geometry>
<DataTemplate x:Key="ArrowUpNormal">
<Path Width="18"
Height="10"
Stretch="Fill"
Data="{StaticResource UpArrowGeometry}"
Fill="DarkOrange"
SnapsToDevicePixels="True"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Focusable="False" />
</DataTemplate>
<DataTemplate x:Key="ArrowUpDisabled">
<Path Width="18"
Height="10"
Stretch="Fill"
Data="{StaticResource UpArrowGeometry}"
Fill="Green"
SnapsToDevicePixels="True"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Focusable="False" />
</DataTemplate>
<Style x:Key="IncreaseRepeatButtonStyle" TargetType="{x:Type RepeatButton}">
<Setter Property="BorderThickness" Value="0" />
<Setter Property="Background" Value="Transparent" />
<Setter Property="Height" Value="40" />
<Style.Triggers>
<Trigger Property="IsEnabled" Value="False">
<Setter Property="ContentTemplate" Value="{StaticResource ArrowUpDisabled}" />
</Trigger>
</Style.Triggers>
</Style>
After starting the application, the repeatbutton looks as intended (darkorange):
However, when I disable the RepeatButton (via codebehind) with "IncreaseButton.IsEnabled = false;"
I expected my style trigger to turn the arrow green:
But instead I get this (Arrow stays orange and the background turns white/gray):
What is causing this behaviour and how do I fix this? Thanks!
The Background turns white/gray because there is a ControlTemplate trigger defined in the base style. To remove this you need to override the base style. But then you need to create a new template.
<Setter Property="OverridesDefaultStyle" Value="True"/>
<Setter Property="Template">
<Setter.Value>
<!-- Template -->
</Setter.Value>
</Setter>
Link to the base style with template
And the reason why the arrow stays orange is that you have set the template directly on the button. This overrides the property in the style and also the triggers. To fix this just add (and remove the property on the button)
<Setter Property="ContentTemplate" Value="{StaticResource ArrowUpNormal" />
to your style.
I am trying to write a custom ListBox that will popup a user specifed control at runtime. I have made a custom RadioButton that I want to have display a popup when the user selects one of the buttons in the list
Here Is the xaml for the custom ListBox control. I am trying to use my custom radio button as the ListboxItem:
<ListBox
x:Class="PopUp.SystemAccessPanel"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:s="clr-namespace:System;assembly=mscorlib"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:PopUp"
d:DataContext="{d:DesignInstance local:PanelButton}"
mc:Ignorable="d"
>
<ListBox.Resources>
<Style TargetType="ListBoxItem">
<Setter Property="SnapsToDevicePixels" Value="true"/>
<Setter Property="OverridesDefaultStyle" Value="true"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListBoxItem">
<StackPanel>
<local:PanelButton
x:Name="Button1"
Foreground="{TemplateBinding Foreground}"
GroupName="{Binding GroupName}"
ButtonText="{Binding ButtonText}"
Margin="0,0,0,20"
/>
<Popup
x:Name="PART_Popup"
IsOpen="{Binding IsChecked, ElementName=Button1}"
PlacementTarget="{Binding ElementName=Button1}"
Placement="Absolute"
VerticalOffset="{Binding AbsoluteYPlacement}"
HorizontalOffset="{Binding AbsoluteXPlacement}"
local:BaseExtendedPopup.ClosesOnInput="True"
>
<ContentPresenter/>
</Popup>
</StackPanel>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ListBox.Resources>
<ListBox.Template>
<ControlTemplate>
<Border
BorderThickness="0"
Padding="1,1,1,1"
Background="Transparent"
Name="theBorder"
SnapsToDevicePixels="True"
>
<ItemsPresenter
SnapsToDevicePixels="{TemplateBinding UIElement.SnapsToDevicePixels}"
/>
</Border>
<!--
<ControlTemplate.Triggers>
<Trigger Property="ItemsControl.IsGrouping" Value="True">
<Setter Property="ScrollViewer.CanContentScroll" Value="False" />
</Trigger>
</ControlTemplate.Triggers>
-->
</ControlTemplate>
</ListBox.Template>
</ListBox>
And here I how i want the control to be used:
<usr:SystemAccessPanel Foreground="Pink" Background="Aqua">
<usr:PanelButton
ButtonText="What"
ImageWidth="50"
ImageHeight="50"
Width="40"
GroupName="Test"
AbsoluteXPlacement="900"
AbsoluteYPlacement="100"
>
<usr:UserControl2/>
</usr:PanelButton>
<usr:PanelButton
ButtonText="The Hell"
ImageWidth="45"
Height="20" Width="100"
GroupName="Test"
AbsoluteXPlacement="900"
AbsoluteYPlacement="100"
>
<usr:UserControl1/>
</usr:PanelButton>
</usr:SystemAccessPanel>
However All I end up with is a PaneButton as a Popup =S. What do i need to do to have my usercontrol be in the popup?
I have the following checkbox
<CheckBox x:Name="checkBox" Content="CheckBox" Width="74"/>
and I have a button
<Button Name="TestButton" Content="Test" />
I want to set a "default" color for the textblock. I achieve that by having a resourcedictionary which has the following content:
<Style TargetType="{x:Type TextBlock}">
<Setter Property="Foreground" Value="White"/>
</Style>
The problem is, that the Button should still have a black Textblock foreground, but although in another sourcedictionary i have the following, it still changes to white:
<Style TargetType="Button">
<Style.Setters>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Border BorderThickness="1,0,0,1" CornerRadius="5" Background="{TemplateBinding Background}">
<ContentPresenter
x:Name="ContentPresenter"
Margin="1"
VerticalAlignment="Center"
HorizontalAlignment="Center"
TextBlock.Foreground="Black"
Opacity="1.0"/>
</Border>
</ControlTemplate/>
<Setter.Value/>
</Setter>
</Style.Setters>
</Style
Edit:
The ResourceDictionaries are defined in Application.xaml like this:
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="TextBlock.xaml"/>
<ResourceDictionary Source="Buttons.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
You could try overriding the default TextBlock style locally for the ContentPresenter by defining another one in its Resources:
<ContentPresenter ... >
<ContentPresenter.Resources>
<Style TargetType="TextBlock">
<Setter Property="Foreground" Value="Black" />
</Style>
</ContentPresenter.Resources>
</ContentPresenter>
But a better way to set a default control text color is like this in App.Resources. TextElement.Foreground will override this on any given individual element.
<SolidColorBrush
x:Key="{x:Static SystemColors.ControlTextBrushKey}"
Color="White"
/>
If you use that and discard your default TextBlock style, your original ContentPresenter should work as you had it.
A user control's background is set, but that background hides the contained controls. How can this be resolved?
<UserControl x:Class="PL_Wpf.CustomControls.Conditioner"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:PL_Wpf.CustomControls"
mc:Ignorable="d" Height="166" Width="349">
<UserControl.Background>
<ImageBrush ImageSource="Conditioner.png" Stretch="None"/>
</UserControl.Background>
<Grid >
<ComboBox x:Name="combo1" HorizontalAlignment="Right" Width="57" Margin="0,68,53.206,70" />
<ToggleButton x:Name="ToggleSwitch" Height="50" Click="ToggleSwitch_Click" Margin="97.205,55,115.206,61" d:LayoutOverrides="Width" >
<ToggleButton.Style>
<Style TargetType="{x:Type ToggleButton}">
<Style.Triggers>
<Trigger Property="IsChecked" Value="True">
<Setter Property="Content">
<Setter.Value>
<Image Source="../Pics/switch_on.png" />
</Setter.Value>
</Setter>
</Trigger>
<Trigger Property="IsChecked" Value="False">
<Setter Property="Content">
<Setter.Value>
<Image Source="../Pics/switch_off.png" />
</Setter.Value>
</Setter>
</Trigger>
</Style.Triggers>
</Style>
</ToggleButton.Style>
</ToggleButton>
</Grid>
jsfiddle.net/wgfw1Ld7/
The Margin properties on the controls set themselves when dropped on the design surface. This feature may be needed if adjusting items on a Canvas but are mostly an annoyance when dealing with most Xaml control placement.
Remove the Margin properties to see the controls in their correct location on the grid.