how to add effect for text box to style - wpf

I'm trying to add an effect to style in order to reuse it, but from some reason it doesnt work...
<Style x:Key="NumericTextBoxStyle" TargetType="{x:Type TextBox}">
<Style.Resources>
<TextBox.Effect x:Key="EffectStyle">
<DropShadowEffect BlurRadius="56"
Direction="392"
Color="#FF872E2E"
RenderingBias="Quality"/>
</TextBox.Effect>
</Style.Resources>
<Setter Property="Height" Value="25"/>
<Setter Property="Width" Value="120"/>
<Setter Property="HorizontalAlignment" Value="Right"/>
<Setter Property="VerticalAlignment" Value="Top"/>
<Setter Property="TextAlignment" Value="Center"/>
</Style>
but how do i add the style part ? (also how do i declare for the effect ?)
thanks

Try to add the Effect as a Setter instead
<Style x:Key="NumericTextBoxStyle" TargetType="{x:Type TextBox}">
<Setter Property="Effect">
<Setter.Value>
<DropShadowEffect BlurRadius="56"
Direction="392"
Color="#FF872E2E"
RenderingBias="Quality"/>
</Setter.Value>
</Setter>
<Setter Property="Height" Value="25"/>
<Setter Property="Width" Value="120"/>
<Setter Property="HorizontalAlignment" Value="Right"/>
<Setter Property="VerticalAlignment" Value="Top"/>
<Setter Property="TextAlignment" Value="Center"/>
</Style>
Or if you want to have the Effect as a Resource in the Style you can do it like this
<Style x:Key="NumericTextBoxStyle" TargetType="{x:Type TextBox}">
<Style.Resources>
<DropShadowEffect x:Key="dropShadowEffect"
BlurRadius="56"
Direction="392"
Color="#FF872E2E"
RenderingBias="Quality"/>
</Style.Resources>
<Setter Property="Effect" Value="{StaticResource dropShadowEffect}"/>
<!--...-->
</Style>

You can also make your effect a global resource, in order to use it with other styles/controls:
<Grid>
<Grid.Resources>
<DropShadowEffect x:Key="dropShadowEffect" BlurRadius="56"
Direction="392"
Color="#FF872E2E"
RenderingBias="Quality"/>
<Style x:Key="NumericTextBoxStyle" TargetType="{x:Type TextBox}">
<Setter Property="Effect" Value="{StaticResource dropShadowEffect}" />
<Setter Property="Height" Value="25"/>
<Setter Property="Width" Value="120"/>
</Style>
</Grid.Resources>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition />
</Grid.RowDefinitions>
<TextBox Style="{StaticResource NumericTextBoxStyle}" />
<TextBox Style="{StaticResource NumericTextBoxStyle}" Grid.Row="1" />
<ComboBox Effect="{StaticResource dropShadowEffect}" Grid.Row="2" />
</Grid>

Related

WPF Style design TextBox Tooltip

I would like to style the Tooltip of Textboxes which are used for validation. I'm trying to get a rounded corners Tooltip without sucess. How may I get it from ResourceDictonary?
With the code below, I'm getting the Tooltip text on validation error with a rectangular border. Here is my code:
<ResourceDictionary>
<!-- Textbox with validation capabilities -->
<Style x:Key="ValidableTextBox" TargetType="TextBox">
<Style.Resources>
<Style x:Key="ToolTip" TargetType="ToolTip">
<Setter Property="OverridesDefaultStyle" Value="true" />
<Setter Property="HasDropShadow" Value="True" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ToolTip">
<Border Name="Border" CornerRadius="4" BorderThickness="1" Width="{TemplateBinding Width}" Height="{TemplateBinding Height}">
<ContentPresenter />
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Style.Resources>
<Setter Property="Height" Value="22"/>
<Setter Property="VerticalContentAlignment" Value="Center"/>
<Setter Property="VerticalAlignment" Value="Center"/>
<Style.Triggers>
<Trigger Property="Validation.HasError" Value="true">
<Setter Property="ToolTip" Value="{Binding RelativeSource={RelativeSource Self}, Path=(Validation.Errors)[0].ErrorContent}"/>
</Trigger>
</Style.Triggers>
</Style>
</ResourceDictionary>
I just realized that BorderBrush property was missing, thanks to Muhammad for he made it working (remove the x:Key="ToolTip"), here is the updated code:
<ResourceDictionary>
<!-- Textbox with validation capabilities -->
<Style x:Key="ValidableTextBox" TargetType="TextBox">
<Style.Resources>
<Style TargetType="ToolTip">
<Setter Property="OverridesDefaultStyle" Value="true" />
<Setter Property="HasDropShadow" Value="True" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ToolTip">
<Border Name="Border" CornerRadius="4" BorderThickness="1" BorderBrush="Red" Width="{TemplateBinding Width}" Height="{TemplateBinding Height}">
<ContentPresenter />
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Style.Resources>
<Setter Property="Height" Value="22"/>
<Setter Property="VerticalContentAlignment" Value="Center"/>
<Setter Property="VerticalAlignment" Value="Center"/>
<Style.Triggers>
<Trigger Property="Validation.HasError" Value="true">
<Setter Property="ToolTip" Value="{Binding RelativeSource={RelativeSource Self}, Path=(Validation.Errors)[0].ErrorContent}"/>
</Trigger>
</Style.Triggers>
</Style>
</ResourceDictionary>

Multiple Style Resource in WPF Metro Button

I am trying to achieve my save button to reusable button and original button style is like this=>
<Button Margin="5"
Padding="0"
Width="98"
Cursor="Hand"
x:Name="btnSave"
Click="btnSave_Click">
<StackPanel Orientation="Horizontal" Height="25" Width="90">
<Image Source="\Image\Other\Save.ico" Width="20" Margin="3 0"></Image>
<TextBlock VerticalAlignment="Center" Margin="15 0">Save</TextBlock>
</StackPanel>
</Button>
Just text and image. So I just want to use this button as a reusable button. So I move this button to App.xaml Like this=>
<Style TargetType="Button" x:Key="SaveButton" BasedOn="{StaticResource MetroButton}">
<Setter Property="Margin" Value="5"/>
<Setter Property="Padding" Value="0"/>
<Setter Property="Width" Value="98"/>
<Setter Property="Cursor" Value="Hand"/>
<Setter Property="FontSize" Value="12"/>
<Style.Resources>
<Style TargetType="StackPanel">
<Setter Property="Orientation" Value="Horizontal"/>
<Setter Property="Height" Value="25"/>
<Setter Property="Width" Value="90"/>
<Setter Property="Background" Value="Red"/>
<Style.Resources>
<Style TargetType="Image">
<Setter Property="Source" Value="/Image/Other/Save.ico"/>
<Setter Property="Width" Value="20"/>
<Setter Property="Margin" Value="3 0"/>
</Style>
<Style TargetType="TextBlock">
<Setter Property="VerticalAlignment" Value="Center"/>
<Setter Property="Margin" Value="15 0"/>
<Setter Property="Text" Value="Save"/>
</Style>
</Style.Resources>
</Style>
</Style.Resources>
</Style>
But after moving that, This button is not working anymore. Please let me know why this one is not working.
You said you wanted to reuse styles, so you shouldn't nest them. The right thing to do is:
<Style x:Key="SaveButton" TargetType="{x:Type Button}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<StackPanel Orientation="Horizontal" Height="25" Width="90" Background="Red">
<Image/>
</StackPanel>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
You need to define the StackPanel somewhere and set the Button's Content property to it. You may define the StackPanel as a non-shared resource next to the Style in App.xaml like this:
<StackPanel x:Key="sp" x:Shared="False" Orientation="Horizontal" Height="25" Width="90">
<Image Source="screen.png" Width="20" Margin="3 0"></Image>
<TextBlock VerticalAlignment="Center" Margin="15 0">Save</TextBlock>
</StackPanel>
<Style TargetType="Button" x:Key="SaveButton">
<Style.Resources>
</Style.Resources>
<Setter Property="Margin" Value="5"/>
<Setter Property="Padding" Value="0"/>
<Setter Property="Width" Value="98"/>
<Setter Property="Cursor" Value="Hand"/>
<Setter Property="FontSize" Value="12"/>
<Setter Property="Content" Value="{StaticResource sp}" />
</Style>

Add control in resource dictionary style

I'm not sure if it's even possible, but worth to try and ask. So - it is possible to add somehow control to resource dictionary?
For example I have style which I apply to every window in my application:
<Style x:Key="WindowStyle" TargetType="Window">
<Setter Property="WindowState" Value="Maximized" />
<Setter Property="WindowStyle" Value="None" />
<Setter Property="Width" Value="{DynamicResource {x:Static SystemParameters.PrimaryScreenWidth}}" />
<Setter Property="Height" Value="{DynamicResource {x:Static SystemParameters.PrimaryScreenHeight}}" />
<Setter Property="Background">
<Setter.Value>
<ImageBrush ImageSource="/myApp;component/Pictures/gradientBackground.png" />
</Setter.Value>
</Setter>
<Setter Property="Icon" Value="/myApp;component/Pictures/logo_small.png" />
</Style>
As you can see every window is maximized and have no borders etc - it's just full screen window. What I'd like to do is add simple button in the right-top corner, which will work as minimize.
Any idea?
Thanks in advance!
<Style x:Key="WindowStyle" TargetType="Window">
<Setter Property="WindowState" Value="Maximized" />
<Setter Property="WindowStyle" Value="None" />
<Setter Property="Width" Value="{DynamicResource {x:Static SystemParameters.PrimaryScreenWidth}}" />
<Setter Property="Height" Value="{DynamicResource {x:Static SystemParameters.PrimaryScreenHeight}}" />
<Setter Property="Background">
<Setter.Value>
<ImageBrush ImageSource="/myApp;component/Pictures/gradientBackground.png" />
</Setter.Value>
</Setter>
<Setter Property="Icon" Value="/myApp;component/Pictures/logo_small.png" />
</Setter>
<Setter Property="ContentTemplate">
<Setter.Value>
<DataTemplate>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<Button Content="X" HorizontalAlignment="Right" Margin="2"></Button>
<ContentControl Grid.Row="1" Content="{TemplateBinding Content}"></ContentControl>
</Grid>
</DataTemplate>
</Setter.Value>
</Setter>
</Style>

How to wrap Text in a ListViewItem?

WPF ListBoxItem how to wrap text in it? My Item container style looks like this:
<Style x:Key="GroupListBoxItemStyle"
TargetType="ListBoxItem">
<Setter Property="OverridesDefaultStyle"
Value="True" />
<Setter Property="FocusVisualStyle"
Value="{x:Null}" />
<Setter Property="FontSize"
Value="11" />
<Setter Property="FontWeight"
Value="Bold" />
<Setter Property="Width"
Value="95" />
<Setter Property="HorizontalAlignment"
Value="Center" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListBoxItem">
<SlidingBar:SlidingBarRadioButton GroupName="PermissionsRadioButtonGroup"
IsChecked="{Binding Path=IsSelected,RelativeSource={RelativeSource TemplatedParent},BindsDirectlyToSource=True,Mode=TwoWay}"
Text="{Binding Converter={StaticResource resourceStringToResourceConverter}}"
ImageSource="{Binding Converter={StaticResource PermissionTypeToImageConverter}}"
Margin="1"
/>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
Make sure you set the following properties to the ListBox/ListView:
HorizontalContentAlignment="Stretch"
ScrollViewer.HorizontalScrollBarVisibility="Disabled"

Common Style for PasswordBox and TextBox

How can I define a common Style for the TextBox and the PasswordBox?
My approach, I have define a Style for the TargetType FrameworkElement but this doesnt work as common style, then few propertys doesnt exist on the FrameworkElement.
My TextBox Style is equals the PasswordBoxStyle
<Style TargetType="{x:Type TextBox}">
<Setter Property="SnapsToDevicePixels" Value="True"/>
<Setter Property="OverridesDefaultStyle" Value="True"/>
<Setter Property="KeyboardNavigation.TabNavigation" Value="None"/>
<Setter Property="FocusVisualStyle" Value="{x:Null}"/>
<Setter Property="MinHeight" Value="30"/>
<Setter Property="AllowDrop" Value="true"/>
<Setter Property="FontFamily" Value="Verdana"/>
<Setter Property="FontSize" Value="14"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type TextBox}">
<Border Name="Border" CornerRadius="4" Padding="2" Background="White" BorderBrush="Black" BorderThickness="1" >
<ScrollViewer Margin="0" x:Name="PART_ContentHost"/>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
Try something like this
<Style x:Key="textboxPasswordboxStyles">
<Setter Property="Control.ControlProperty" Value="value" />
<Setter Property="TextBox.TextboxSpecproperty" Value="value" />
<Setter Property="PasswordBox.PasswordBoxSpecProperty" Value="value" />
</Style>
<Style TargetType="{x:Type TextBox}"
BasedOn="{StaticResource textboxPasswordboxStyles}">
</Style>
<Style TargetType="{x:Type PasswordBox}"
BasedOn="{StaticResource textboxPasswordboxStyles}">
</Style>
<Style x:Key="textboxPasswordboxStyles" TargetType="Control">
<Setter Property="SnapsToDevicePixels" Value="True" />
<Setter Property="OverridesDefaultStyle" Value="True" />
<Setter Property="KeyboardNavigation.TabNavigation" Value="None" />
<Setter Property="Height" Value="20" />
<Setter Property="MinHeight" Value="20" />
<Setter Property="MaxHeight" Value="22"/>
<Setter Property="BorderThickness" Value="1.5"/>
<Setter Property="AllowDrop" Value="true" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Control}">
<Grid>
<Border Name="Border" CornerRadius="8" BorderThickness="{TemplateBinding BorderThickness}" >
<Border.Background>
<SolidColorBrush Color="White" x:Name="Background" />
</Border.Background>
<Border.BorderBrush>
<SolidColorBrush Color="Gray" x:Name="BorderBrush" Opacity="1"/>
</Border.BorderBrush>
<ScrollViewer Margin="10,0,10,0" x:Name="PART_ContentHost" VerticalAlignment="Center"/>
</Border>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Style TargetType="TextBox"
BasedOn="{StaticResource textboxPasswordboxStyles}">
</Style>
<Style TargetType="PasswordBox"
BasedOn="{StaticResource textboxPasswordboxStyles}">
</Style>

Resources