I am changing a control's style at run-time. Say changing a textbox to read-only (and thus changing other properties on the textbox affecting the display) if the user is in "review mode".
Let's say I have a style in the Application Resources.
<Style x:Key="ReadOnlyTextBoxStyle" TargetType="{x:Type TextBox}">
<Setter Property="BorderBrush" Value="Black"/>
<Setter Property="BorderThickness" Value="1"/>
<Setter Property="Focusable" Value="False"/>
<Setter Property="Cursor" Value="Arrow"/>
<Setter Property="Background" Value="White"/>
<Setter Property="IsReadOnly" Value="True"/>
<Setter Property="TextBlock.FontWeight" Value="Bold"/>
<Setter Property="ScrollViewer.CanContentScroll" Value="True"/>
<Setter Property="VerticalScrollBarVisibility" Value="Visible"/>
<Setter Property="TextWrapping" Value="Wrap"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<Border x:Name="Bd" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" SnapsToDevicePixels="True" Background="{TemplateBinding Background}">
<ScrollViewer Name="PART_ContentHost"/>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
I've found several approaches to referencing a resource in code-behind via a string, e.g.
myTextBox.Style = (Style).Resources["ReadOnlyTextBoxStyle"];
But I can't find an example of accessing a resource via something like this:
myControl.Style = (Style).Resources[nameOf(IsThere.Some.Way.To.Get.QualifiedReferenceToStyleResourceKey)];
If I use something like:
myControl.Style = (Style).Resources["ReadOnlyTextBoxStyle"];
and then I update the key to a new string, like "ReadOnlyTextBoxStyleModified" then I have to change it everywhere I reference it in code. If I miss one of the replacements, I won't get an error until run-time. I'd prefer to see the error at compile time if possible.
The answer in the below linked question suggested using a named constant, but then didn't provide an example. That seems like it would accomplish what I am going for, but I don't understand how to implement his comment which said
"Also, a good practice is to create a string constant which maps the name of your key in the resource dictionary (so that you can change it at only one place)."