2017-01-18 3 views
0

мне нужен заполнитель для TextBox и нашел этот рабочий код:StaticResource изменяет границы TextBox

<Style x:Key="placeHolder" TargetType="{x:Type TextBox}"> 
     <Setter Property="Template"> 
      <Setter.Value> 
       <ControlTemplate TargetType="{x:Type TextBox}"> 
        <Grid> 
         <TextBox Text="{Binding Path=Text, 
              RelativeSource={RelativeSource TemplatedParent}, 
              Mode=TwoWay, 
              UpdateSourceTrigger=PropertyChanged}" 
          x:Name="textSource" 
          Background="Transparent" 
          Panel.ZIndex="2" 
           BorderThickness="0,0,0,1"/> 
         <TextBox Text="{TemplateBinding Tag}" Background="{TemplateBinding Background}" Panel.ZIndex="1"> 
          <TextBox.Style> 
           <Style TargetType="{x:Type TextBox}"> 
            <Setter Property="Foreground" Value="Transparent"/> 
            <Style.Triggers> 
             <DataTrigger Binding="{Binding Path=Text, Source={x:Reference textSource}}" Value=""> 
              <Setter Property="Foreground" Value="LightGray"/> 
             </DataTrigger> 
            </Style.Triggers> 
           </Style> 
          </TextBox.Style> 
         </TextBox> 
        </Grid> 
       </ControlTemplate> 
      </Setter.Value> 
     </Setter> 
    </Style> 

Это мой TextBox:

<TextBox x:Name="isci_tb" HorizontalAlignment="Left" Height="25" Margin="0,0,0,0" TextWrapping="Wrap" 
      VerticalAlignment="Center" Width="120" BorderThickness="0, 0, 0, 1" TextChanged="isci_tb_TextChanged" Tag="išči" Style="{StaticResource placeHolder}"> 
      <TextBox.Background> 
       <ImageBrush/> 
      </TextBox.Background> 
     </TextBox> 

Поскольку я новичок в XAML я не вижу где или что изменит границу моей кнопки. Я хочу иметь только нижнюю границу, но вместо этого отображаются все 4 границы.

ответ

0

Установите BorderThickness свойство внутренней TextBox в шаблоне 0:

<Style x:Key="placeHolder" TargetType="{x:Type TextBox}"> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="{x:Type TextBox}"> 
       <Grid> 
        <TextBox Text="{Binding Path=Text, 
              RelativeSource={RelativeSource TemplatedParent}, 
              Mode=TwoWay, 
              UpdateSourceTrigger=PropertyChanged}" 
          x:Name="textSource" 
          Background="Transparent" 
          Panel.ZIndex="2" 
           BorderThickness="0,0,0,1"/> 
        <TextBox Text="{TemplateBinding Tag}" Background="{TemplateBinding Background}" Panel.ZIndex="1" 
            BorderThickness="0"> 
         <TextBox.Style> 
          <Style TargetType="{x:Type TextBox}"> 
           <Setter Property="Foreground" Value="Transparent"/> 
           <Style.Triggers> 
            <DataTrigger Binding="{Binding Path=Text, Source={x:Reference textSource}}" Value=""> 
             <Setter Property="Foreground" Value="LightGray"/> 
            </DataTrigger> 
           </Style.Triggers> 
          </Style> 
         </TextBox.Style> 
        </TextBox> 
       </Grid> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
</Style> 
+0

Это сделал это, спасибо! Могу ли я спросить, почему именно здесь и почему в этом шаблоне есть 2 разных текстовых поля? Я искал руководство для изучения расширенного XAML (привязки, controltemplates ...) и ничего не нашел. – someone1

+0

Не знаю. Это ваш шаблон :) В общем, вы не поместили бы TextBox в ControlTemplate из TextBox, но, я думаю, вы получили этот стиль откуда-то. – mm8

+0

Как я уже сказал, я нашел его на Stackoverflow, и он работает, да. Я попытаюсь найти несколько руководств по XAML, так как мне это понадобится много, хе-хе. – someone1