In WPF, the default behavior of the TextBox on focus is to put the cursor where it was the last time the TextBox had lost focus or if it hasn’t had focus yet, at the beginning. The users of the application wanted this to be changed so that when the TextBox got focus, all current text was selected. I found various ways to do this, but am putting this blog post together to get everything I ended up doing all in one place for easy access.
Read More
WPF MarkupExtension Class
This week I was going through StackOverflow, looking for anything I could help with and cam across someone asking about how to bind to an attribute of a class member in XAML. I wrote a small example IValueConverter class they could use to accomplish the task which was the only way I knew of that it could be done. Then, another poster (H.B.) solved the task using something I’d never heard of before…the MarkupExtension class.
Using the example H.B. gave, creating a class to display the DisplayName attribute of a property, you can see that this approach gets the job done just as well as a converter, but to me, it looks like it solves the problem in a much more direct way than a converter. The converters are generally used to convert a value from one Type to another Type, for instance, from a boolean to a value of System.Windows.Visibility or from a Color to a Brush.
To create a MarkupExtension class, you derive from the abstract MarkupExtension class. There is one method to override, called ProvideValue. A default constructor is required unless you intend to only support attribute usages of the class. If you want to support arguments in the usage, might also need to define additional constructors to match the settable properties.
Attached is a sample WPF application with code that implements the MarkupExtension and IValueConverters I’ve mentioned in this short write-up.
Using Style Triggers to Give ToolTips to DataGrid Columns
Working with a DataGrid on a recent project, I had to get ToolTips on all of the DataGrid column headers. At first go around, I had used the first thing that came to my head which was to insert a TextBlock into my Header and give it a ToolTip.
- <DataGridTemplateColumn.Header>
- <TextBlock Text="Column B" ToolTip="A short explanation of Column B"/>
- </DataGridTemplateColumn.Header>
This worked OK for a while, I got my expected result which was a ToolTip when a user moused over the header…although it wasn’t exactly what I wanted because they had to mouse over the text of my header, but I was OK with that.
Later on, another requirement came in, to allow the users to choose which columns were visible and which were hidden and that is when the flaw in my first approach was exposed (more on this later) and I found an easier way using a trigger to display tool tips and didn’t require creating a TextBlock in DataGridTemplateColumn.Header.
- <Style TargetType="{x:Type DataGridColumnHeader}">
- <Style.Triggers>
- <Trigger Property="IsMouseOver" Value="True">
- <Setter Property="ToolTip" Value="{Binding Column.(ToolTipService.ToolTip), RelativeSource={RelativeSource Self}}"/>
- </Trigger>
- </Style.Triggers>
- </Style>
- …
- <!– Use ToolTipService.ToolTip to assign the tooltip value –>
- <DataGridTemplateColumn x:Name="colA" Width="40*" IsReadOnly="True" Header="Column A" ToolTipService.ToolTip="Explanation of Column A">