WPF扩展TextBox控件在窗口中不可见

时间:2018-06-07 11:48:46

标签: wpf

我正在尝试创建我的第一个WPF应用程序。我创建了一个带有一个IsRequired属性的新自定义Textbox控件。项目已成功建成。我已将TextBox放在窗口中,但根本看不到它。不确定出了什么问题。由于我是WPF的初学者,期待您的指导。以下是我添加的代码。

XTextBox类:

public class XTextBox : TextBox
{
    public XTextBox():base()
    {
        DrawBorder();
    }
    public static readonly DependencyProperty IsRequiredProperty =
        DependencyProperty.Register("IsRequired", typeof(bool), typeof(XTextBox), new PropertyMetadata(false));
    static XTextBox()
    {
        DefaultStyleKeyProperty.OverrideMetadata(typeof(XTextBox), new FrameworkPropertyMetadata(typeof(XTextBox)));
    }

    private bool isRequired = false;
    private string requiredText = String.Empty;

    public bool IsRequired
    {
        get
        {
            return isRequired;
        }
        set
        {
            isRequired = value;
        }
    }

    public string RequiredText
    {
        get
        {
            return requiredText;
        }
        set
        {
            requiredText = value;
        }
    }

    protected override void OnTextChanged(TextChangedEventArgs e)
    {
        if(isRequired)
        {
            DrawBorder();
        }
        base.OnTextChanged(e);
    }


    private void DrawBorder()
    {
        if (String.IsNullOrWhiteSpace(this.Text))
        {
            this.BorderBrush = (Brush)TryFindResource("FaultyBorderBrush");
            this.ToolTip = requiredText;
        }
        else
        {
            this.BorderBrush = (Brush)TryFindResource("DefaultBorderBrush");
            this.ToolTip = null;
        }
    }
}

Generic.xaml:

<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:MyApp.XControls">


<Style TargetType="{x:Type local:XTextBox}">
    <Setter Property="MinWidth" Value="120"/>
    <Setter Property="MinHeight" Value="20"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type local:XTextBox}">
                <Border Background="{TemplateBinding Background}"
                        BorderBrush="{TemplateBinding BorderBrush}"
                        BorderThickness="{TemplateBinding BorderThickness}">
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

<LinearGradientBrush x:Key="FaultyBorderBrush" EndPoint="0,20" StartPoint="0,0" MappingMode="Absolute">
    <GradientStop Color="#FFABADB3" Offset="0.05"/>
    <GradientStop Color="#FFE2E3EA" Offset="0.07"/>
    <GradientStop Color="#FFFF0000" Offset="1"/>
</LinearGradientBrush>

<LinearGradientBrush x:Key="DefaultBorderBrush" EndPoint="0,20" StartPoint="0,0" MappingMode="Absolute">
    <GradientStop Color="#FFABADB3" Offset="0.05"/>
    <GradientStop Color="#FFE2E3EA" Offset="0.07"/>
    <GradientStop Color="#FFE3E9EF" Offset="1"/>
</LinearGradientBrush>

Window1.xaml:

<Grid Name="gridMasters" Margin="3">
    <Grid.ColumnDefinitions>
         <ColumnDefinition Width="100" />
         <ColumnDefinition Width="*" />
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
          <RowDefinition/>
          <RowDefinition/>
          <RowDefinition Height="*"/>
    </Grid.RowDefinitions>
    <Label Grid.Column="0" Grid.Row="0" FontFamily="Verdana" HorizontalAlignment="Left" Margin="0,0,0,5">Group Name</Label>
    <local:XTextBox Grid.Column="1" Grid.Row="0" IsRequired="True" RequiredText="Please enter the group name" Width="100" Height="20" VerticalAlignment="Top" HorizontalAlignment="Left" />

    <Label Grid.Column="0" Grid.Row="1" FontFamily="Verdana" HorizontalAlignment="Left" Margin="0,0,0,5">Firm</Label>
    <ComboBox Grid.Column="1" Grid.Row="1" ToolTip="Select your firm" Name="cboFirm" FontFamily="Verdana" Margin="2,2,20,5" />

</Grid>

在窗口中,我可以看到ComboBox,但文本框在那里看不到。我做错了吗?

0 个答案:

没有答案