Reputation: 1903
I'll save my why did they do it this way rants for elsewhere, so...
I'm trying to prevent TextBlocks from getting focus across my Silverlight app. It seems like adding a setter for this property on any TextBlock within my application's base page class (Page-inheriting) makes some sense, but 1) I'm probably wrong and 2) I can't seem to quite get it right.
I've tried variations on adding code like this:
this.Style.Setters.Add(new Setter(TextBlock.IsHitTestVisibleProperty, false));
to the constructor of my app's base Page class (inherits directly from Page) or in a page Loaded event handler. But most variations on that theme don't seem to work (generally seeming as though all the right members don't yet exist or XamlParseExceptions.
It's probably absurdly simple, but so is my brain, apparently. Any ideas?
Upvotes: 0
Views: 593
Reputation: 9354
You could just use an implicit style
<Style TargetType="TextBlock">
<Setter Property="IsHitTestVisible" Value="False" />
</Style>
Generally, styles are merged at the top level of the App.xaml file if in separate resource dictionaries or you could simply add your style there. I'm pretty new to Silverlight myself, so I'm only reporting what I've seen.
Assuming you have an empty App.xaml starting out, it may look something like this:
<Application
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
x:Class="YourApp.App"
mc:Ignorable="d">
<Application.Resources>
<ResourceDictionary>
<Style TargetType="TextBlock">
<Setter Property="IsHitTestVisible" Value="False" />
</Style>
</ResourceDictionary>
</Application.Resources>
</Application>
This is pretty simple to test, you could add a Foreground color setter and see if all of your TextBlocks pick up the style.
Upvotes: 2