I am having a problem trying to change the style of the default ContextMenu in WPF.
I not want to override de ContextMenu, I simple want to override the ContextMenu STYLE.
In all the sites, says that I must create each MenuItem of the ContextMenu, but I want to use the defaults MenuItems, and only change the style and add a border in the background.
How can I do it?
<Style TargetType="{x:Type ContextMenu}">
<Setter Property="SnapsToDevicePixels" Value="True" />
<Setter Property="OverridesDefaultStyle" Value="True" />
<Setter Property="Grid.IsSharedSizeScope" Value="true" />
<Setter Property="HasDropShadow" Value="True" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ContextMenu}">
<Border x:Name="Border" Background="#000" BorderThickness="1">
<ScrollViewer x:Name="ScrollViewer">
<ItemsPresenter />
</ScrollViewer>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
Make sure that :
The resource dictionary is loaded and not unloaded later on in your code.
(As far as I remember the list of loaded resource dictionaries can be found in Application.Current.Resources.MergedDictionaries)
You don't set the Style or other properties of the target ContextMenu in it's declaration.
Related
I have a customListView, in that I have created an image button with transparent background. But instead of being shown in all rows, It's only there in the last row
Button Style:
<Style TargetType="Button" x:Key="editButtonStyle" BasedOn="{StaticResource {x:Static ToolBar.ButtonStyleKey}}">
<Setter Property="Width" Value="20" />
<Setter Property="Height" Value="20" />
<Setter Property="Content">
<Setter.Value>
<Image Source="edit_icon.png" />
</Setter.Value>
</Setter>
<Setter Property="Background" Value="Transparent"/>
<Setter Property="BorderThickness" Value="0"/>
<Setter Property="BorderBrush" Value="Transparent"/>
</Style>
The Call:
<DataTemplate>
<Button Style="{StaticResource editButtonStyle}">
<Button.Template>
<ControlTemplate TargetType="Button">
<ContentPresenter Content="{TemplateBinding Content}"/>
</ControlTemplate>
</Button.Template>
</Button>
</DataTemplate>
Set x:Shared="False" on your editButtonStyle. You define an instance of Image in your style which of course can only be used once if it's a shared style (some kind of WPF optimization).
This would work in a DataTemplate since the FrameworkElementFactory would create a new Image for every item, but it doesn't work with styles.
i based my application on this
example
i need my own button-style (without mouseover animations etc.), so i made this in the app.xaml:
<Style TargetType="Button" BasedOn="{StaticResource {x:Type Button}}">
<Setter Property="SnapsToDevicePixels" Value="true"/>
<Setter Property="OverridesDefaultStyle" Value="true"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Border x:Name="Border"
CornerRadius="2" BorderThickness="1"
Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}">
<ContentPresenter Margin="2" HorizontalAlignment="Center" VerticalAlignment="Center" RecognizesAccessKey="True"/>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsEnabled" Value="false">
<Setter TargetName="Border" Property="Background" Value="{StaticResource DisabledBackgroundBrush}" />
<Setter TargetName="Border" Property="BorderBrush" Value="{StaticResource DisabledBorderBrush}" />
<Setter Property="Foreground" Value="{StaticResource DisabledForegroundBrush}"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
my button: <Button IsEnabled="true"/>
now if i change the button to <Button IsEnabled="false"/> my app crashes at the start with an error like: "{DependencyProperty.UnsetValue}" is not a valid value for property "BorderBrush".
what am i doing wrong?
It has to do with your static reference.
In particular, XAML parsing is very touchy on order - you have to make sure that a brush with x:Key="DisabledForegroundBrush" is referenced before the parser gets to the line in the above style - even if your style above is in the same file as the DisabledForegroundBrush.
If you don't have a brush yet for the DisabledForegroundBrush, you can either remove the line referencing it in the above code if you don't require it, or, if you want it, you can create one as follows:
<SolidColorBrush x:Key="DisabledForegroundBrush" Color="Red" />
Where you can choose the color as appropriate. Alternatively, you can choose some other kind of brush here: http://msdn.microsoft.com/en-us/library/aa970904(v=vs.110).aspx
If you have a brush you'd like to use already, then if you could provide a little more information as to where the brush is in the code base (eg is it in a resource dictionary?) and where the DisabledForegroundBrush brush is, this might help me pinpoint an actual solution / best way of ensuring the brush is referenced.
Note: an alternative if you can't ensure that DisabledForegroundBrush is parsed first is to change the StaticResource to a DynamicResource, but this isn't recommended unless the resource's link might actually change at run time (see What's the difference between StaticResource and DynamicResource in WPF? )
An easier solution:
If you just want to hard-code the style in, instead of reference the foreground brush externally, then you could change the line:
<Setter Property="Foreground" Value="{StaticResource DisabledForegroundBrush}"/>
to:
<Setter Property="Foreground" Value="[SOME COLOR]"/>
To get rid of the need to create a separate brush object for your font.
I have a WPF menu:
XAML:
<Style x:Key="{x:Type ContextMenu}" TargetType="{x:Type ContextMenu}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ContextMenu}">
<Border Background="#FF171616" CornerRadius="5" BorderBrush="DarkGray" BorderThickness="5" Opacity="0.0">
<StackPanel ClipToBounds="True" Orientation="Vertical" IsItemsHost="True" Margin="5,4,5,4"/>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
Am I using the correct x:Type ContextMenu to alter the drop down visualization? The menu item visual is altered because I've manually changed the style. But it is the context drop down on which I want to apply visuals.
How can I modify the context drop down itself?
Here is a paint sample of what I'm after:
To modify style for menu, you need to override Menu style.
Like below:
<Style TargetType="{x:Type Menu}">
<Setter Property="BorderThickness" Value="0" />
<Setter Property="Foreground" Value="White" />
<Setter Property="Background" Value="#FF171616" />
</Style>
Edit: you can actually modify the whole template also, if you like. But I think these properties should get you the visualization you are looking for.
I am trying to implement dragdrop functionality in Listbox(with in the listbox). For dragging i am using Thumb control (Mythumb is the class inherited from thumb)
so i have set the items panel to Canves and set the style for list box item to following
<Style TargetType="{x:Type ListBoxItem}">
<Setter Property="Width" Value="150" />
<Setter Property="Height" Value="150" />
<Setter Property="Margin" Value="5,2" />
<Setter Property="Padding" Value="3" />
<Setter Property="dcc:OutputConfigurationPanel.Left" Value="{Binding Left}"></Setter>
<Setter Property="dcc:OutputConfigurationPanel.Top" Value="{Binding Top}"></Setter>
<Setter Property="IsSelected" Value="{Binding IsSelected}"></Setter>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ListBoxItem}">
<Grid DockPanel.Dock="Bottom" >
<Grid>
<Border BorderBrush="Black" Background="Black">
<ContentPresenter x:Name="ContentHost" Margin="{TemplateBinding Padding}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" />
</Border>
<MythumbTemplate="{StaticResource MoveThumbTemplate}" Cursor="Hand" />
</Grid>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="Selector.IsSelected" Value="True">
<Setter Property="Background" Value="#FF233B00" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
The drag drop is working fine but the thumb doesn’t allow the item to be selected. After checking i found that the mousedown event is not firing.
Is there a way to enable the select the items in usual ways?
A Thumb will always eat mouse clicks, same as button.
As I see it, you have two options:
From the code of the thumb, search up until you find the list box item and then set it's IsSelected property to true.
Scrap the thumb and create a Behavior that deals with dragging but doesn't handle mouse events (i.e. doesn't set e.Handled=true;) - this will preserve list box functionality.
Option 1 is probably faster, but I'd strongly consider to go with option 2 - less meddling with core controls functionality = better.
First of all, I would like to add customized text blocks to my GUI with the least possible overhead. For instance: <TextBlock style={StaticRessources myTextBlock}>Text</TextBlock>
For now I have the following border style:
<Style x:Key="greenBox" TargetType="Border">
<Setter Property="Background" Value="#00FF00"/>
<Setter Property="CornerRadius" Value="10"/>
<Setter Property="BorderThickness" Value="1"/>
<Setter Property="BorderBrush" Value="Black"/>
<Setter Property="Height" Value="40"/>
<Setter Property="Width" Value="100"/>
</Style>
And I apply it in the following way:
<Border Style="{StaticResource greenBox}">
<TextBlock HorizontalAlignment="Center" VerticalAlignment="Center">Custom Text</TextBlock>
</Border>
My problem is, it needs 2 Tags and the properties set in the TextBlock will be redunant. I cannot figure out how to abstract both definitions into a single element.
that's where Label comes into play:
<Style TargetType="Label" x:Key="greenLabel">
<Setter Property="OverridesDefaultStyle" Value="true"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Label">
<Border Style="{StaticResource greenBox}">
<ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Label Style="{StaticResource greenLabel}">Custom Text</Label>
(in accordance with your other question: if this is the only place you use that borderstyle you can of course include these directly in that border not using an extra style)
You would need to create a custom control as described here. Or you could create a UserControl as well.