-1

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)."

Accessing a resource via codebehind in WPF

4
  • I guess Resources work with the x:key value only. Moreover changing the style at runtime may not require code behind (for quiet some things). You can use Triggers. Commented Feb 13, 2020 at 22:49
  • @PrateekShrivastava thanks for your response. Even using a string that won't notify me of an error until run-time would be cleaner than using trigger for what I'm trying to do. Commented Feb 13, 2020 at 22:58
  • @Clemens, I did see the second comment on the answer. The commentator was asking the answer-poster to elaborate but the answer-poster didn't respond. I was asking to see if anyone knows what the answer-poster was talking about. If he called it a good practice, I presume it is something he had done before. Commented Feb 13, 2020 at 23:00
  • @Clemens, thanks for your response. So then are you saying that there is no way to access a resource in code using a qualified reference? I have to use a string of the resource key to refer to it in code? Commented Feb 13, 2020 at 23:18

1 Answer 1

1

You may create a class with static string members for you resource keys, like

public static class ResourceKeys
{
    public static string Key1 = "Key1";
}

then declare a resource with such a key, for example

<Window.Resources>
    <SolidColorBrush x:Key="{x:Static local:ResourceKeys.Key1}" Color="Red"/>
</Window.Resources>

and use that resource by its key like

<Grid Background="{StaticResource ResourceKey={x:Static local:ResourceKeys.Key1}}">

or like

grid.Background = (Brush)FindResource(ResourceKeys.Key1);
Sign up to request clarification or add additional context in comments.

1 Comment

yes, something like "grid.Background = (Brush)FindResource(ResourceKeys.Key1); " is just what I was going for

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.