Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 972353
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T03:06:41+00:00 2026-05-16T03:06:41+00:00

What is the simplest way to get a WPF resource from code? I need

  • 0

What is the simplest way to get a WPF resource from code?

I need to disable a text box in a WPF form if a checkbox in the same window is checked. I have wired the checkbox to an event handler in code-behind. The event handler disables the checkbox and changes its background to a light gray, to indicate that the control is disabled:

private void OnCheckBoxChecked(object sender, RoutedEventArgs e)
{
    MyTextBox.IsEnabled = false;
    MyTextBox.Background = (Brush)FindResource("DisabledControlBackgroundBrush");
}

The disabled control background color is defined as a resource in a resource dictionary that is imported into the WPF window. I tested the resource by setting the textbox background in XAML, and the resource works fine.

I also know the event handler is working, because it disables the text box when the checkbox is clicked.

My problem is that the event handler isn’t changing the Background property as it should. I suspect that there is a problem with my call to FindResource, but I am not getting an exception, and the Output window has nothing on it.

So, how do I get this resource from code and apply it to my text box? Thanks for your help.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T03:06:41+00:00Added an answer on May 16, 2026 at 3:06 am

    David. I’ve put together a sample window that does this using triggers on the TextBox.Style:

    <Window x:Class="WpfApplication1.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Window1">
        <Window.Resources>
            <SolidColorBrush x:Key="IsCheckedColor" Color="DarkGray"  />
        </Window.Resources>
        <StackPanel>
            <TextBox x:Name="textbox" Margin="36" Height="24"  >
                <TextBox.Style>
                    <Style TargetType="TextBox">
                        <Setter Property="Background" Value="White" />
                        <Style.Triggers>
                            <DataTrigger Binding="{Binding IsChecked, ElementName=checkbox}"  Value="True" >
                                <Setter Property="Background" Value="{StaticResource IsCheckedColor}"  />
                                <Setter Property="IsEnabled" Value="False" />
                            </DataTrigger>
                        </Style.Triggers>
                    </Style>
                </TextBox.Style>
            </TextBox>
            <CheckBox x:Name="checkbox" Content="Click Me" Height="24" Margin="36"/>
        </StackPanel>
    </Window>
    

    You can’t use a Trigger to change another control’s properties, but you can use them to change a control’s properties based on something else, like a DataContext or another control.

    Each control can have a Triggers collection, but this can only contain EventTriggers. In a Style you can use plain Trigger which can be used to control animation, as well as DataTrigger, which I’ve used in this sample to control the TextBox settings based on the properties of the CheckBox.

    Notice that I’ve also used a Setter outside of the Triggers collection to set the default value, and I don’t need a second Setter for when the CheckBox is not checked — it just goes back to the "default" state.

    edit – how to change background of disabled TextBox

    I do this in Blend, but if you don’t have Blend you can of course put the XAML in directly. This has to do with controls states. As the TextBox transitions among Normal, MouseOver, and Disabled, you can animate changes to the appearance. In this case we use an animation of virtually zero duration, so the change is immediate.

    Add the following to the StackPanel:

        <VisualStateManager.VisualStateGroups>
            <VisualStateGroup x:Name="VisualStateGroup">
                <VisualState x:Name="Disabled">
                    <Storyboard>
                        <ColorAnimationUsingKeyFrames BeginTime="00:00:00" Duration="00:00:00.0010000" Storyboard.TargetName="textbox" Storyboard.TargetProperty="(Panel.Background).(SolidColorBrush.Color)">
                            <SplineColorKeyFrame KeyTime="00:00:00" Value="{StaticResource IsCheckedColor}"/>
                        </ColorAnimationUsingKeyFrames>
                    </Storyboard>
                </VisualState>
            </VisualStateGroup>
        </VisualStateManager.VisualStateGroups>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What is the simplest way to get the plain-text only from filetypes: Word [2003-2007],
What's the simplest way to get the URL's and Titles from a given Google
What is the simplest way to get the file name that from a path?
Can anyone tell me the simplest way to get a contact list from Exchange
Looking for the simplest way to get a single value from a database. This
what is the simplest way to get all rows where a complex condition holds
What is the simplest way of taking the comment/s from the metadata of a
What is the simplest way to call a program from with a piece of
What is the simplest way to remove the header row from a flat file
What is the simplest way to get the tail of an array in C#

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.