-1

通过为 TextBox 控件定义新的控件模板来创建 WPF 应用程序。您为 TextBox 提供自定义外观并实现功能以在 TextBox 获得焦点、失去焦点以及文本元素中的内容发生更改时更改该外观(即发生 TextChanged 事件)。

提示:初始状态应该是默认状态(您可以选择样式),然后您可以在 GotFocus 和 LostFocus 事件之间切换。

这是我到目前为止的标记......以及 textchanged 要求的代码隐藏。当我更改文本块的文本时,我无法将背景更改为蓝色。有什么建议么?

<Window x:Class="WpfApplication1.MainWindow"

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    Title="MainWindow" Height="350" Width="525">

<Window.Resources>



    <ControlTemplate x:Key="myTextBoxTemplate">

    <Border

  x:Name="templateBorder"

  Padding="50" Background="Pink"

  BorderBrush="Blue" CornerRadius="5"

  BorderThickness="5" HorizontalAlignment="Center">



  <TextBlock>

            <!--ScrollViewer all the text box to allow entering of text-->

            <ScrollViewer Margin="0" x:Name="PART_ContentHost">

            </ScrollViewer>

  </TextBlock>           

</Border>



        <ControlTemplate.Triggers>

            <Trigger Property="IsEnabled" Value="False">

                <Setter TargetName="templateBorder" Property="Background" Value="{StaticResource DisabledBackgroundBrush}"/>

                <Setter TargetName="templateBorder" Property="BorderBrush" Value="{StaticResource DisabledBackgroundBrush}"/>

            </Trigger>



            <Trigger Property="IsMouseOver" Value="True">

                <Setter TargetName="templateBorder" Property="BorderThickness" Value="8"/>

            </Trigger>



            <Trigger Property="IsKeyboardFocused" Value="True">

                <Setter TargetName="templateBorder" Property="Background" Value="Red"/>

            </Trigger>





        </ControlTemplate.Triggers>

    </ControlTemplate>



</Window.Resources>



 <StackPanel>



    <TextBox Text="Click Me" FontWeight="Bold" Template="{StaticResource myTextBoxTemplate}"

            Name="myTextBox" TextChanged="myTextBox_TextChanged" Opacity="1"/>

 </StackPanel>



 </Window>

方法:

private void myTextBox_TextChanged(object sender, TextChangedEventArgs e)

    {

        myTextBox.Background = Brushes.Blue;

    }
4

1 回答 1

0

在您ControlTemplate中,您实际上并没有使用TextBox. 因此,当您从代码隐藏设置它时,它没有任何效果,因为它没有被您的ControlTemplateBorder 使用。

从而让您Border使用Background="{TemplateBinding Background}"

就像是:

<Window.Resources>
  <ControlTemplate x:Key="myTextBoxTemplate"
                    TargetType="{x:Type TextBox}">
    <Border x:Name="templateBorder"
            HorizontalAlignment="Center"
            Background="{TemplateBinding Background}"
            BorderBrush="{TemplateBinding BorderBrush}"
            BorderThickness="5"
            CornerRadius="5"
            Padding="50">
      <TextBlock>
        <ScrollViewer x:Name="PART_ContentHost"
                      Margin="0" />
      </TextBlock>
    </Border>
    <ControlTemplate.Triggers>
      <Trigger Property="IsEnabled"
                Value="False">
        <Setter TargetName="templateBorder"
                Property="Background"
                Value="{StaticResource DisabledBackgroundBrush}" />
        <Setter TargetName="templateBorder"
                Property="BorderBrush"
                Value="{StaticResource DisabledBackgroundBrush}" />
      </Trigger>
      <Trigger Property="IsMouseOver"
                Value="True">
        <Setter TargetName="templateBorder"
                Property="BorderThickness"
                Value="8" />
      </Trigger>
      <Trigger Property="IsKeyboardFocused"
                Value="True">
        <Setter TargetName="templateBorder"
                Property="Background"
                Value="Red" />
      </Trigger>
    </ControlTemplate.Triggers>
  </ControlTemplate>
</Window.Resources>
<StackPanel>
  <TextBox Name="myTextBox"
            FontWeight="Bold"
            Opacity="1"
            Template="{StaticResource myTextBoxTemplate}"
            Text="Click Me"
            TextChanged="myTextBox_TextChanged" />
  <Button Content="Dummy Button" />
</StackPanel>
于 2013-05-05T22:08:54.390 回答