WPF how to set a tooltip to textbox (in style file) - wpf

I wanted to have nice ToolTip for my TextBox, so I started with simple ToolTip:
<TextBox>
<TextBox.ToolTip>
<StackPanel>
<TextBlock>Nice text</TextBlock>
<TextBlock>Nice text</TextBlock>
</StackPanel>
</TextBox.ToolTip>
</TextBox>
However I have a dozen TextBoxes and I wanted them all to have the ToolTip above. That's why I decided to transfer the code above into a Style file.
My Style file looks like:
<Style x:Key="DefaultStyle" TargetType="{x:Type TextBox}">
<Setter Property="FontFamily" Value="Tahoma"/>
<Setter Property="FontSize" Value="15"/>
<Setter Property="VerticalContentAlignment" Value="Center"/>
<Setter Property="TextAlignment" Value="Center"/>
<Setter Property="TextWrapping" Value="Wrap"/>
<Setter Property="BorderBrush" Value="#A9C2DE"/>
<Setter Property="BorderThickness" Value="1"/>
<Setter Property="Background">
<Setter.Value>
<LinearGradientBrush StartPoint="0.5,0" EndPoint="0.5,1" >
<GradientStop Color="#CDE1F7" Offset="0.01"/>
<GradientStop Color="#DFECFA" Offset="0.8" />
</LinearGradientBrush>
</Setter.Value>
</Setter>
<Setter Property="ToolTip">
<Setter.Value>
<StackPanel>
<TextBlock Text="Nice toolbox"/>
<TextBlock Text="Nice tooltip"/>
</StackPanel>
</Setter.Value>
</Setter>
</Style>
However now I get error XAML parse exception.
How can I set this kind of ToolTip (with StackPanels etc) to TextBox (from style file)??

Dotnet Version < 4
The style looks good but there seems to be an issue with specifing the tooltip directly in the styles value. Declare the ToolTip as a resource and then set it in the style via StaticResource.
<ToolTip x:Key="YourToolTip" >
<StackPanel>
<TextBlock Text="Nice toolbox"/>
<TextBlock Text="Nice tooltip"/>
</StackPanel>
</ToolTip>
... Your Style...
<Setter Property="TextBox.ToolTip" Value="{StaticResource YourToolTip}"/>
...
Dotnet Version == 4
If you work with .net4, it's something other. You wrote that you put it into a style file. Do you mean a resource-file? If yes, may be you have not loaded it during runtime. Something like:
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/YourStyleFile.xaml"/>
<ResourceDictionary>
<!-- Other local resources -->
</ResourceDictionary>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Window.Resources>
Another possibility is that you try to use it on another type than TextBox. This does not work because you declared TextBox as target type.

Related

How to set a control resource dictionary in a Style in XAML?

I have a UI in Silverlight with some borders, and with a TextBlock inside that borders. There will be other TextBlocks outside borders.
<Border>
<TextBlock>This text should be centered</TextBlock>
</Border>
<Border>
<TextBlock>This one too</TextBlock>
</Border>
<TextBlock>this one shouldn't</TextBlock>
What I'm trying to achieve is to format all TextBlock's inside a Border, without having to set the style in every TextBlock. If it was CSS , it will be something like that : .border .textBlock { (...) }
I know i can set a style inside the scope of a border:
<Border>
<Border.Resources>
<Style TargetType="TextBlock" BasedOn="{StaticResource DefaultTextBlockStyle}">
<Setter Property="HorizontalAlignment" Value="Center"></Setter>
</Style>
</Border.Resources>
<TextBlock>Centered Text</TextBlock>
</Border>
But I would still need to set this to every border in my page. So now i presente the question, can i set in a style, in order to set it one time to affect all Borders? I tried the code bellow, but it didn't work. It didn't give me any errors, but it didn't affect the TextBlock formatting either.
<Style TargetType="Border">
<Setter Property="Resources">
<Setter.Value>
<ResourceDictionary>
<Style TargetType="TextBlock">
<Setter Property="HorizontalAlignment" Value="Center" />
</Style>
</ResourceDictionary>
</Setter.Value>
</Setter>
</Style>
Try this:
<Style TargetType="Border">
<Style.Resources>
<Style TargetType="TextBlock">
<Setter Property="HorizontalAlignment" Value="Center" />
</Style>
</Style.Resources>
</Style>
This works in WPF. In Silverlight I am afraid you cannot do this though.

Not able to change WPF Toolkit Area Series background

I am using WPF Toolkit Area Chart control. I am not able to change background of the Area Series to any other color. It just assigns color code #FFFFA500.
My XAML code is as below
<chartingToolkit:Chart.Palette x:Uid="tt">
<datavis:ResourceDictionaryCollection>
<ResourceDictionary>
<Style x:Key="DataPointStyle" TargetType="Control">
<Setter Property="Background" Value="#FF3EA0C0"/>
</Style>
</ResourceDictionary>
<ResourceDictionary>
<Style x:Key="DataPointStyle" TargetType="Control">
<Setter Property="Background" Value="#FF44C8F5"/>
</Style>
</ResourceDictionary>
</datavis:ResourceDictionaryCollection>
</chartingToolkit:Chart.Palette>
<chartingToolkit:Chart.LegendStyle>
<Style TargetType="Control">
<Setter Property="Width" Value="0"/>
<Setter Property="Height" Value="0"/>
</Style>
</chartingToolkit:Chart.LegendStyle>
<chartingToolkit:AreaSeries Background="Black" x:Name="SecondSeries" DependentValuePath="Value" IndependentValuePath="Key" IsSelectionEnabled="True">
<chartingToolkit:AreaSeries.DataPointStyle>
<Style TargetType="chartingToolkit:AreaDataPoint">
<Setter Property="IsTabStop" Value="False" />
<Setter Property="Width" Value="Auto" />
<Setter Property="Height" Value="30" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="chartingToolkit:AreaDataPoint">
<Button Name="pointButton" Content="{Binding}" Click="pointButton_Click_1" BorderThickness="1" >
<Button.Template>
<ControlTemplate>
<StackPanel>
<TextBlock Text="{Binding Path=Value}" Cursor="Hand"></TextBlock>
</StackPanel>
</ControlTemplate>
</Button.Template>
</Button>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</chartingToolkit:AreaSeries.DataPointStyle>
<chartingToolkit:AreaSeries.DependentRangeAxis>
<chartingToolkit:LinearAxis Orientation="Y" FontFamily="Rockwell" Title="Expenses" FontSize="14" >
</chartingToolkit:LinearAxis>
</chartingToolkit:AreaSeries.DependentRangeAxis>
</chartingToolkit:AreaSeries>
</chartingToolkit:Chart>
Please help me out. This is driving me crazy.
The problem is that there is a Grid within the AreaSeries control Template which has a style applied to it setting the background to a LinearGradientBrush.
In order to change the background of the plot area, add this style
<Style TargetType="Grid" x:Key="plotStyle">
<Setter Property="Background">
<Setter.Value>
<SolidColorBrush Color="Pink"/>
</Setter.Value>
</Setter>
</Style>
and use it as the Chart control's PlotAreaStyle
<chartingToolkit:Chart Background="Black" PlotAreaStyle="{StaticResource plotStyle}">

Customize WPF Toolkit autocompletebox

WPF Toolkit library has Autocompletebox control.
1. How to place an icon at the left of its Textbox part?
2. How to add shadow to the borders of the suggestions popup?
Changing TextBoxStyle property does not give any results.
<!-- xmlns:extendedControls="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Input.Toolkit" -->
<extendedControls:AutoCompleteBox>
<extendedControls:AutoCompleteBox.TextBoxStyle>
<Style TargetType="TextBox">
<Setter Property="Padding" Value="20,0,0,0" />
<Setter Property="Background">
<Setter.Value>
<ImageBrush ImageSource="/img.png" Stretch="None" AlignmentX="Left" />
</Setter.Value>
</Setter>
</Style>
</extendedControls:AutoCompleteBox.TextBoxStyle>
</extendedControls:AutoCompleteBox>
The Autocompletebox have got a property : TextBoxStyle where you can chose the style of the textbox.
Or you can do that in the property Style like that :
<UserControl.Resources>
<Style TargetType="TextBox" x:Key="TextboxStyle2">
<Setter Property="MaxLength" Value="74" />
<Setter Property="Width" Value="300" />
</Style>
<Style TargetType="controls:AutoCompleteBox" x:Key="TextBoxStyle">
<Setter Property="TextBoxStyle" Value="{StaticResource TextboxStyle2}" />
<Setter Property="IsTextCompletionEnabled" Value="True" />
</Style>
</UserControl.Resources>
/*...*/
<controls:AutoCompleteBox Name="Titre" Style="{StaticResource TextBoxStyle}"/>
But i don't know how to add the shadow. Try the others properties. =)
Edit :
To change background, there is already some tutoriels.
how to separate the background image in wpf textbox?
Exemple :
<Style TargetType="TextBox" x:Key="TextboxStyle2">
<Setter Property="MaxLength" Value="74" />
<Setter Property="Width" Value="300" />
<Setter Property="Background">
<Setter.Value>
<ImageBrush ImageSource="../../../Ressources/Icones/Find_5650.ico" Stretch="None" AlignmentX="Left" AlignmentY="Top" />
</Setter.Value>
</Setter>
</Style>

WPF wired Rectangle with Resource Icon ResourceDictoinary Binding Issue

I'm working on a WPF app, and trying to give a button an icon from Icons ResourceDictionary
I created a new ResourceDictionary Merge the Icons ResourceDictionary, and created a few Button Rectangle Styles to use.
And then I found out I can't directly bind the Icon to VisualBrush, I have to create another VisualBrush ResourceKey to show the icon.
I listed working and not working code below.
Anybody know the explanation of this? Any help is appreciated. Thanks a lot.
Binding xaml:
<Button Width="80" Height="80">
<Rectangle Style="{DynamicResource Icon14040}"></Rectangle>
</Button>
Not Working Style:
<Style x:Key="Icon14040" TargetType="{x:Type Rectangle}">
<Setter Property="Height" Value="40" />
<Setter Property="Width" Value="40" />
<Setter Property="Fill">
<Setter.Value>
<VisualBrush Visual="{StaticResource Icon1}"></VisualBrush>
</Setter.Value>
</Setter>
</Style>
Working Style:
<VisualBrush x:Key="Icon1Brush4040" Visual="{StaticResource Icon1}" />
<Style x:Key="Icon14040" TargetType="{x:Type Rectangle}">
<Setter Property="Height" Value="40" />
<Setter Property="Width" Value="40" />
<Setter Property="Fill">
<Setter.Value>
<StaticResource ResourceKey="Icon1Brush4040" />
</Setter.Value>
</Setter>
</Style>

Silverlight 4 and Page.Resources Binding

In my SL4 application, I have a Class called Images which has two properties (ImagePath, ImageName) and a method getImages() which retruns ObservableCollection list of all Images.
I need to create a Resource on my Page so that when a DataTemplate is loaded dynamically it can have access to the Images in one of its ComboBox field.
How can I do that?
<Page.Resources>
<local:Images x:Key="MyImages"/>
</Page.Resources>
But in my code behind file I cannot access the MyImages
You have 3 options
1. Use global app resource file.
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Assets/StylesA.xaml"/>
<ResourceDictionary Source="Assets/StylesB.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
And within StylesA.xaml just put style you have to use
<StackPanel>
<StackPanel.Resources>
<Style x:Key="HeaderStyle" TargetType="TextBlock">
<Setter Property="FontWeight" Value="Bold"></Setter>
<Setter Property="FontSize" Value="12"></Setter>
</Style>
</StackPanel.Resources>
<TextBlock Style="{StaticResource HeaderStyle}"></TextBlock> ....
2. Use local control Resources section.
<StackPanel>
<StackPanel.Resources>
<Style x:Key="HeaderStyle" TargetType="TextBlock">
<Setter Property="FontWeight" Value="Bold"></Setter>
<Setter Property="FontSize" Value="12"></Setter>
</Style>
</StackPanel.Resources>
<TextBlock Style="{StaticResource HeaderStyle}"></TextBlock> ....
3. Just use Page.Resources within your page
<navigation:Page.Resources>
<Style x:Key="ButtonFocusVisual">
<Setter Property="Control.Template">
<Setter.Value>
<ControlTemplate>
<Border>
<Rectangle Margin="2" StrokeThickness="1" Stroke="#60000000" StrokeDashArray="1 2" />
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</navigation:Page.Resources>
I hope it will help. :)

Resources