Пользовательский элемент управления не отображается - PullRequest
0 голосов
/ 24 сентября 2011

Идея, лежащая в основе элемента управления ниже, заключается в том, чтобы иметь возможность отображать метку внутри текстового поля, когда текста нет.Чтобы сделать это, я просто взял рабочий элемент управления, похожий на SearchTextBox, но в нем слишком много всего, что мне нужно.

Я подозреваю, что шаблон элемента управления в стиле ниже, но я не вижу ничего явно не так с ним.Я мог бы легко пропустить что-то, что очевидно для кого-то с большим опытом.

Как мне исправить или устранить проблему?

Приветствия,
Беррил

Код

/// <summary>Light weight version of a <see cref="SearchTextBox"/></summary>
public class LabelTextBox : TextBox
{

    #region Dependency Properties

    #region Label Text

    /// <summary>Sets the 'watermark' text that acts as a label, identifying what this will search for.</summary>
    public static readonly DependencyProperty LabelTextProperty =
        DependencyProperty.Register("LabelText", 
            typeof (string), typeof (LabelTextBox), new PropertyMetadata("Search"));

    public string LabelText
    {
        get { return (string) GetValue(LabelTextProperty); }
        set { SetValue(LabelTextProperty, value); }
    }

    /// <summary>Brush for the label text.</summary>
    public static readonly DependencyProperty LabelTextColorProperty =
        DependencyProperty.Register("LabelTextColor", typeof (Brush), typeof (LabelTextBox));

    public Brush LabelTextColor
    {
        get { return (Brush)GetValue(LabelTextColorProperty); }
        set { SetValue(LabelTextColorProperty, value); }
    }

    #endregion

    #region Has Text

    private static readonly DependencyPropertyKey HasTextPropertyKey =
        DependencyProperty.RegisterReadOnly("HasText", 
            typeof (bool), typeof (LabelTextBox), new PropertyMetadata());

    /// <summary>True if the user has typed in text.</summary>
    public static readonly DependencyProperty HasTextProperty = HasTextPropertyKey.DependencyProperty;

    public bool HasText
    {
        get { return (bool)GetValue(HasTextProperty); }
        private set { SetValue(HasTextPropertyKey, value); }
    }

    #endregion

    #endregion

    #region Construction

    static LabelTextBox()
    {
        DefaultStyleKeyProperty.OverrideMetadata(typeof (LabelTextBox), new FrameworkPropertyMetadata(typeof (LabelTextBox)));
    }

    #endregion

    #region Event Handlers

    protected override void OnTextChanged(TextChangedEventArgs e)
    {
        base.OnTextChanged(e);

        HasText = Text.Length != 0;
    }

    #endregion

}

Стиль

<Style x:Key="{x:Type cc:LabelTextBox}" TargetType="{x:Type cc:LabelTextBox}">
    <Setter Property="Background" Value="{StaticResource SearchTextBox_Background}" />
    <Setter Property="BorderBrush" Value="{StaticResource SearchTextBox_Border}" />
    <Setter Property="Foreground" Value="{StaticResource SearchTextBox_Foreground}" />
    <Setter Property="BorderThickness" Value="1" />
    <Setter Property="SnapsToDevicePixels" Value="True" />
    <Setter Property="LabelText" Value="Search" />
    <Setter Property="FocusVisualStyle" Value="{x:Null}"/>
    <Setter Property="LabelTextColor" Value="{StaticResource SearchTextBox_LabelTextColor}" />

    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type cc:LabelTextBox}">

                <Border x:Name="Border" BorderBrush="{TemplateBinding BorderBrush}" Padding="2" 
                        BorderThickness="{TemplateBinding BorderThickness}"
                        Background="{TemplateBinding Background}"
                        >

                    <Grid x:Name="LayoutGrid">

                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="*" />
                            <ColumnDefinition Width="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=ActualHeight}" />
                        </Grid.ColumnDefinitions>

                        <ScrollViewer x:Name="PART_ContentHost" Grid.Column="0" />

                        <Label x:Name="LabelText" Grid.Column="0" 
                               Foreground="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=LabelTextColor}" 
                               Content="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=LabelText}" 
                               Padding="2,0,0,0" FontStyle="Italic" 
                               />

                    </Grid>

                </Border>

                <ControlTemplate.Triggers>

                    <!--input triggers (mouse over && keyboard focused-->
                    <Trigger Property="IsMouseOver" Value="True">
                        <Setter Property="BorderBrush" Value="{StaticResource SearchTextBox_BorderMouseOver}" />
                    </Trigger>
                    <Trigger Property="IsKeyboardFocusWithin" Value="True">
                        <Setter Property="BorderBrush" Value="{StaticResource SearchTextBox_BorderMouseOver}" />
                    </Trigger>

                    <!--Once the user has typed in search text-->

                    <Trigger Property="HasText" Value="True">
                        <!--Hide the label text-->
                        <Setter Property="Visibility" TargetName="LabelText" Value="Hidden" />
                    </Trigger>

                </ControlTemplate.Triggers>

            </ControlTemplate>
        </Setter.Value>

    </Setter>

    <!--
    Error display
    -->
    <Setter Property="Validation.ErrorTemplate">
        <Setter.Value>
            <ControlTemplate>
                <DockPanel LastChildFill="True">
                    <TextBlock DockPanel.Dock="Right" Text=" *" 
                               Foreground="Red" 
                               FontWeight="Bold" FontSize="16" 
                               ToolTip="{Binding ElementName=placeholder, Path=AdornedElement.(Validation.Errors).CurrentItem.ErrorContent}"/>
                    <Border BorderBrush="Red"  BorderThickness="1">
                        <AdornedElementPlaceholder Name="placeholder"></AdornedElementPlaceholder>
                    </Border>
                </DockPanel>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
    <Style.Triggers>
        <Trigger Property="Validation.HasError" Value="True">
            <Setter Property="Background" Value="LightYellow"/>
        </Trigger>
    </Style.Triggers>


</Style>

1 Ответ

1 голос
/ 26 сентября 2011

Кажется, это работает.Все, что я сделал, это скопировал ваш код в класс LabelTextBox и ваш Style в Generic.xaml словарь ресурсов в папке Themes, расположенной в корневом каталоге проекта (я изменил цвета кисти на LightBlue, LightGray, Blue и т. Д.)

...