can't write in textbox WPF

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • vishal1082
    New Member
    • Apr 2009
    • 92

    can't write in textbox WPF

    i made a custom skinned textbox in App.xaml, here's the XAML:
    Code:
    <Style TargetType="{x:Type TextBox}">
                
                <Setter Property ="Background">
                    <Setter.Value>
                        <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0" MappingMode="RelativeToBoundingBox">
                            <GradientStop Color="#FFF8F8F8" Offset="0.116"/>
                            <GradientStop Color="#FFE8E8E8" Offset="0.672"/>
                            <GradientStop Color="#FFF0F0F0" Offset="0.388"/>
                        </LinearGradientBrush>
                    </Setter.Value>
                </Setter>
                
                <Setter Property="BorderBrush">
                    <Setter.Value>
                        <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                            <GradientStop Color="#FFD0D0D0" Offset="0.073"/>
                            <GradientStop Color="White" Offset="1"/>
                            <GradientStop Color="#FED4D4D4" Offset="0.366"/>
                            <GradientStop Color="#FED6D6D6" Offset="0.724"/>
                            <GradientStop Color="#FED0D0D0" Offset="0.108"/>
                        </LinearGradientBrush>
                    </Setter.Value>
                </Setter>
    
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type TextBox}">
                            
                            <Border BorderThickness="1" BorderBrush="{TemplateBinding BorderBrush}" Background="{TemplateBinding Background}"/>
    
                            <ControlTemplate.Triggers>
    
                                <Trigger Property="IsMouseOver" Value="True">
    
                                    <Setter Property="BorderBrush">
                                        <Setter.Value>
                                            <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                                                <GradientStop Color="#FFA7A7A7" Offset="1"/>
                                                <GradientStop Color="#FFD0D0D0"/>
                                                <GradientStop Color="DarkGray" Offset="0.44"/>
                                            </LinearGradientBrush>
                                        </Setter.Value>
                                    </Setter>
    
                                </Trigger>
                                
                                <Trigger Property="IsFocused" Value="True">
                                    
                                    <Setter Property="Background">
                                        <Setter.Value>
                                            <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0" MappingMode="RelativeToBoundingBox">
                                                <GradientStop Color="#FFF8F8F8"/>
                                                <GradientStop Color="#FFF0F0F0" Offset="0.672"/>
                                                <GradientStop Color="#FFF0F0F0" Offset="0.401"/>
                                            </LinearGradientBrush>
                                        </Setter.Value>
                                    </Setter>
                                    
                                    <Setter Property="BorderBrush">
                                        <Setter.Value>
                                            <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                                                <GradientStop Color="#FFD0D0D0" Offset="0.073"/>
                                                <GradientStop Color="#FED0D0D0" Offset="0.909"/>
                                            </LinearGradientBrush>
                                        </Setter.Value>
                                    </Setter>
                                    
                                    <Setter Property="Effect">
                                        <Setter.Value>
                                            <DropShadowEffect BlurRadius="48" Opacity="0.295" ShadowDepth="3" Color="#FF939393" Direction="191"/>
                                        </Setter.Value>
                                    </Setter>
                                    
                                </Trigger>
    
                                <Trigger Property="IsEnabled" Value="False">
    
                                    <Setter Property="Background">
                                        <Setter.Value>
                                            <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                                                <GradientStop Color="#4CECECEC" Offset="0"/>
                                                <GradientStop Color="#FFD4D4D4" Offset="1"/>
                                            </LinearGradientBrush>
                                        </Setter.Value>
                                    </Setter>
    
                                    <Setter Property="Foreground" Value ="#FF7A7A7A"/>
    
                                    <Setter Property="Effect">
                                        <Setter.Value>
                                            <BlurEffect Radius="2" RenderingBias="Quality"/>
                                        </Setter.Value>
                                    </Setter>
    
                                </Trigger>
    
                            </ControlTemplate.Triggers>
                            
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
    this gives all the textbox's same theme, i declare it in a Window like this:
    Code:
    <TextBox Height="23" HorizontalAlignment="Left" Margin="106,175,0,0" Name="textBox1" VerticalAlignment="Top" Width="201" KeyDown="textBox1_KeyDown" />

    Now the problem is, i dont see the blinking cursor in textbox, nor can i type/backspace/paste/copy anything in it, i subscribed to the KeyDown event to see if it is detecting key presses, it does but it for some reason not showing the text nor the cursor, also note that if i do not include the " <Setter Property="Templ ate">" and its childs, the textbox works fine, but then it is not completely skinned... any clues?
  • jhardman
    Recognized Expert Specialist
    • Jan 2007
    • 3405

    #2
    The problem is the control template has no instructions for where to put the content, you have told it to draw your textbox as a border ONLY. Unfortunately, I'm not sure what is the best solution, I'm pretty new at WPF myself. I tried this variation (in place of your controltemplate border)
    Code:
    <Grid>
        <Border BorderThickness="1" 
                    BorderBrush="{TemplateBinding BorderBrush}"
                    Background="{TemplateBinding Background}"/>
        <ContentPresenter HorizontalAlignment="Center"
                                     VerticalAlignment="Center"/>
    </Grid>
    and this:
    Code:
    <Border BorderThickness="1" 
            BorderBrush="{TemplateBinding BorderBrush}"
            Background="{TemplateBinding Background}">
        <ContentPresenter HorizontalAlignment="Center"
                                    VerticalAlignment="Center"/>
    </Border>
    and neither worked, but I'm sure this is the issue (or at least part of the issue).

    Jared
    Last edited by jhardman; Feb 18 '10, 11:38 PM. Reason: re-formatted lines in code to be more readable

    Comment

    • jhardman
      Recognized Expert Specialist
      • Jan 2007
      • 3405

      #3
      Found it!
      Code:
      <Border BorderThickness="1" 
              BorderBrush="{TemplateBinding BorderBrush}"
              Background="{TemplateBinding Background}">
          <ScrollViewer Margin="0" x:Name="PART_ContentHost"/>
      </Border>
      Let me know if this helps.

      Jared

      Comment

      • vishal1082
        New Member
        • Apr 2009
        • 92

        #4
        great! works but the text looks... choppy.. like... not anti alised, i dont know.. wierd

        Comment

        Working...