Get WPF resource from code?

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

I need to disable a text box in a WPF form if a checkbox is selected in the same window. I associated this checkbox with an event handler in the code. The event handler disables the check box and changes its background to light gray to indicate that the control is disabled:

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

Disabled background color is defined as a resource in the resource dictionary that is imported into the WPF window. I tested the resource by setting the background text in XAML and the resource is working fine.

I also know that the event handler works because it disables the text box when clicking on the checkmark.

My problem is that the event handler does not change the Background property as it should. I suspect there is a problem with my FindResource call, but I am not getting an exception and there is nothing in the output window.

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

+3
source share
2 answers

David. I put together a test window that does this using triggers on 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 cannot use Triggerto change other control properties, but you can use them to change control properties based on something else, for example, DataContextor another control.

Triggers, EventTriggers. Style Trigger, , DataTrigger, TextBox CheckBox.

, Setter Triggers , Setter, CheckBox - " ".

edit - TextBox

Blend, Blend, , , XAML. . TextBox Normal, MouseOver Disabled, . , .

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>
+1

Windows Forms, WPF. , , :

 <Style x:Key="BackGroundCheckBoxStyle">  < !--apply the style to checkbox -->
  <Style.Triggers> 
   <Trigger Property="IsChecked" Value="True"> 
     <Setter Property="{Binding ElementName=m_txtBox, Path=IsEnabled, Mode=TwoWay}" Value="false}" /> 
     <!-- bind your resource here with a setter as well -->
     </Trigger> 
    </Style.Triggers> 
  </Style> 

Aplication.Resources . - Freezable, GUI ( ())

, :)

-1

Source: https://habr.com/ru/post/1758118/


All Articles