Why can't I reset the TextBox Background in my UserControl?

I created a UserControl that extends ComboBox functionality in interesting and useful ways. It looks like this when it fell:

My user control

I created a whole bunch of functions in the control, and they all work smoothly. It makes me think that I have something that I do. You would think that it would be a trivial matter for the UserControl style to set the TextBox editable text brush. In fact, it seems that this is simply impossible. And I am puzzled.

UserControl XAML, extremely abbreviated (thank you for that), looks like this:

<UserControl x:Class="MyApp.CodeLookupBox" x:Name="MainControl">
    <UserControl.Resources>
       <!-- tons of DataTemplates and Styles, most notably the style that
            contains the control template for the ComboBox -->
    <UserControl.Resources>
    <ComboBox x:Name="ComboBox" 
                   Margin="0" 
                   Style="{DynamicResource ComboBoxStyle1}" 
                   VerticalAlignment="Top"
                   ItemTemplate="{StaticResource GridViewStyleTemplate}"/>
</UserControl>

There is a lot of code in this element, mostly dependency properties, which I use for things like selecting the template that was used in the drop-down list.

, . , - , XAML, :

<Style TargetType="{x:Type local:CodeLookupBox}">
    <Style.Triggers>
        <DataTrigger Binding="{Binding IsRequired}" Value="True">
            <Setter Property="EditableTextBoxBackground" Value="{StaticResource RequiredFieldBrush}"/>
        </DataTrigger>
    </Style.Triggers>
</Style>

UserControl, TextBox. TextBox .

ComboBox , TextBox:

<Style x:Key="ComboBoxEditableTextBox" TargetType="{x:Type TextBox}">
    <Setter Property="OverridesDefaultStyle" Value="true"/>
    <Setter Property="AllowDrop" Value="true"/>
    <Setter Property="MinWidth" Value="0"/>
    <Setter Property="MinHeight" Value="0"/>
    <Setter Property="FocusVisualStyle" Value="{x:Null}"/>

    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type TextBox}">
                <ScrollViewer 
                            x:Name="PART_ContentHost" 
                            Focusable="false" 
                            HorizontalScrollBarVisibility="Hidden" 
                            VerticalScrollBarVisibility="Hidden"/>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

TextBox ( ComboBox) :

<TextBox 
    x:Name="PART_EditableTextBox" 
    Margin="{TemplateBinding Padding}" 
    Style="{StaticResource ComboBoxEditableTextBox}" 
    HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}" 
    VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}"
    IsReadOnly="{Binding IsReadOnly, RelativeSource={RelativeSource TemplatedParent}}"/>

, ComboBoxEditableTextBox. ScrollViewer? . , , TextBox ControlTemplate, .

: TextBox Background , . Background PART_EditableTextBox, . ( Foreground FontFamily, .)

Background ScrollViewer Green, , voila, TextBox .

, TextBox . , , , . ScrollViewer, , TextBox. , .

, TextBox Background, ScrollViewer. , , EditableTextBoxBackground? , PropertyChanged . ScrollViewer XAML :

Background="{Binding ElementName=MainControl, 
    Path=EditableTextBoxBackground, 
    Converter={StaticResource DebuggingConverter}}"

. , . null. . UserControl, .

, : UserControl . TextBox UserControl. ScrollViewer TextBox . PropertyChanged , , ScrollViewer Background.

.

, : 1) ? 2) , , ScrollViewer , ? , , . 3) Blend , , ?

, .

+3
1

. .

1- ScrollViewer ?

a TextBox , . ScrollViewer. TextBox , ScrollViewer Background null. , Background.Transparent. .

, , : TextBox Background.Transparent, .

2- ScrollViewer?

TextBox - Control, gory - , , Visual - "TextView". TextBox - / .

TextBox PART_ContentHost, ContentPresenter, ScrollViewer. ContentPresenter, TextView . ScrollViewer, TextBox , , .

ScrollViewer - TextBox , .

3. Blend

Blend ControlTemplate XAML , PresentationFramework.dll DLL . , , .NET Framework. XAML , , .NET Framework XAML.

:

4 TextBox Background?

WPF Control Background. Background DependencyProperty - , , . TextBox, Control. TextBox "chrome", , , . ComboBox "", TextBox, ScrollViewer, . Background TextBox a ComboBox .

5 TextBox ComboBox

, ScrollViewer <Border> <Border>. , , ControlTemplate ComboBox.

+6

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


All Articles