Showing posts with label WPF. Show all posts
Showing posts with label WPF. Show all posts

Tuesday, February 21, 2012

Set Alternative Background Color for Wpf ComboBox

 

Using style we can set the alternative background color of a combo box without any code behind.first we have to define the style that targets the combo box item.i have defined this style under the resources section of window

 <Style x:Key="alternativeStyle" TargetType="{x:Type ComboBoxItem}">
     <Style.Triggers>
    <Trigger Property="ItemsControl.AlternationIndex" Value="0">
              <Setter Property="Background" Value="Aqua"></Setter>
         </Trigger>
         <Trigger Property="ItemsControl.AlternationIndex" Value="1">
   <Setter Property="Background" Value="Red"></Setter>
         </Trigger>
   </Style.Triggers>
</Style>



*Note-:You have to defined AlternativeIndex as attached property(ItemsControl Class) Other wise Xaml parser doesn't recognize the AlternationIndex property


Then bind relevant style to the our combo box using Item Container Style

 <ComboBox ItemTemplate="{StaticResource ResourceKey=ListViewModel}" Margin="148,116,180,159" Name="cmbTest" IsDropDownOpen="True" AlternationCount="2" ItemContainerStyle="{StaticResource ResourceKey= alternativeStyle}" />



Finally it shows as…


alternative

Wednesday, January 18, 2012

Spell Suggestion and Checking in WPF Text Box

 

WPF text box controller shipped with a inbuilt spell suggestion and checking feature.We can simply enable that feature setting the SpellCheck.IsEnabled="True"/.

Here is the Code

<TextBox Name="dummy" SpellCheck.IsEnabled="True"/>
And Here is the output
Simple 

Friday, January 13, 2012

Change Background Color of a Button in WPF using trigger

 

There was a situation where i need to change the Background and Foreground color of a button when mouse is over .

In this case i have tried to use normal Style like below.

<Trigger Property="IsMouseOver" Value="true">
<Setter Property="Foreground" Value="Bisque"/>
<Setter Property="Background" Value="IndianRed" TargetName="Chrome"/>
</Trigger>





But this is only changed the foreground.this style did not for the Background.There fore i have to change the control template of the button.

 



<Button.Template>
                        <ControlTemplate TargetType="{x:Type Button}">
                            <Microsoft_Windows_Themes:ButtonChrome x:Name="Chrome" BorderBrush="{TemplateBinding BorderBrush}" Background="{TemplateBinding Background}" RenderPressed="{TemplateBinding IsPressed}" RenderDefaulted="{TemplateBinding Button.IsDefaulted}" SnapsToDevicePixels="True">
                                <ContentPresenter ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" ContentStringFormat="{TemplateBinding ContentStringFormat}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
                            </Microsoft_Windows_Themes:ButtonChrome>
                            <ControlTemplate.Triggers>
                                <Trigger Property="IsKeyboardFocused" Value="True">
                                    <Setter Property="RenderDefaulted" TargetName="Chrome" Value="True"/>
                                </Trigger>
                                <Trigger Property="ToggleButton.IsChecked" Value="True">
                                    <Setter Property="RenderPressed" TargetName="Chrome" Value="True"/>
                                </Trigger>
                                <Trigger Property="IsEnabled" Value="False">
                                    <Setter Property="Foreground" Value="Lavender"/>
                                </Trigger>
                                <Trigger Property="IsMouseOver" Value="true">
                                    <Setter Property="Foreground" Value="Bisque"/>
                                    <Setter Property="Background" Value="IndianRed" TargetName="Chrome"/>
                                </Trigger>                               
                            </ControlTemplate.Triggers>
                        </ControlTemplate>
                    </Button.Template>





There are few things to be remember .




  • In our custom ControlTemplate used  "ButtonChrome"  class under the "Microsoft_Windows_Themes" namespace alias.In order to use "ButtonChrome" class inside UI, have to include "PresentationFramework.Aero" dll and




  • xmlns:Microsoft_Windows_Themes="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Aero" to user window level name spaces.