Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 我的文本框控件模板有问题_C#_Wpf_Textbox_Controltemplate - Fatal编程技术网

C# 我的文本框控件模板有问题

C# 我的文本框控件模板有问题,c#,wpf,textbox,controltemplate,C#,Wpf,Textbox,Controltemplate,我正在开发一个简单的文本框模板,它只有两个边框,一个带有渐变背景。现在,我的具体问题是,我希望能够将文本框的前景色设置为我想要的任何颜色,并使其正常工作。然而,我似乎无法让禁用的前景和启用的前景颜色一起工作。例如,如果我将前景设置为红色,当我禁用文本框时,前景不会更改为禁用的颜色。我尝试在IsEnabled=true触发器中绑定前台,但似乎不起作用。无论文本框是否启用,前景始终保持红色 你能看看下面的模板并告诉我我做错了什么吗?另外,请指出我可能犯的任何其他错误,因为我是新创建模板 非常感谢

我正在开发一个简单的文本框模板,它只有两个边框,一个带有渐变背景。现在,我的具体问题是,我希望能够将文本框的前景色设置为我想要的任何颜色,并使其正常工作。然而,我似乎无法让禁用的前景和启用的前景颜色一起工作。例如,如果我将前景设置为红色,当我禁用文本框时,前景不会更改为禁用的颜色。我尝试在IsEnabled=true触发器中绑定前台,但似乎不起作用。无论文本框是否启用,前景始终保持红色

你能看看下面的模板并告诉我我做错了什么吗?另外,请指出我可能犯的任何其他错误,因为我是新创建模板

非常感谢

  <SolidColorBrush x:Key="DisabledForegroundBrush" Color="#888" />
  <SolidColorBrush x:Key="DisabledBackgroundBrush" Color="#EEE" />
  <SolidColorBrush x:Key="WindowBackgroundBrush" Color="#FFF" />
  <SolidColorBrush x:Key="SelectedBackgroundBrush" Color="#DDD" />


<Style x:Key="TextBoxControlTemplate1" TargetType="{x:Type TextBox}">
  <Setter Property="KeyboardNavigation.TabNavigation" Value="None"/>
  <Setter Property="AllowDrop" Value="true"/>
  <Setter Property="Background" Value="#00000000"/>
  <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
  <Setter Property="VerticalContentAlignment" Value="Stretch"/>
  <Setter Property="FontFamily" Value="Segoe UI"/>
  <Setter Property="FontSize" Value="12"/>
  <Setter Property="Padding" Value="8,5,3,3"/>
  <Setter Property="BorderThickness" Value="0"/>
  <Setter Property="Template">
    <Setter.Value>
      <ControlTemplate TargetType="{x:Type TextBox}">
        <Grid>
          <Border BorderBrush="#FF000000" BorderThickness="2,2,2,2" CornerRadius="5,5,5,5" Padding="0,0,0,0" Width="Auto" Height="Auto" Background="#FF000000"/>
          <Border x:Name="Border" BorderBrush="#FFFFFFFF" BorderThickness="1,1,1,1" CornerRadius="5,5,5,5" Padding="0,0,0,0" Width="Auto" Height="Auto" Margin="2,2,2,2">
            <Border.Background>
              <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                <GradientStop Color="#FF000000" Offset="0"/>
                <GradientStop Color="#FF4D4D4D" Offset="1"/>
              </LinearGradientBrush>
            </Border.Background>
          </Border>
          <ScrollViewer Margin="0" x:Name="PART_ContentHost"/>
        </Grid>
        <ControlTemplate.Triggers>
          <Trigger Property="IsEnabled" Value="False">
            <Setter Property="Background" Value="{DynamicResource DisabledBackgroundBrush}" TargetName="Border"/>
            <Setter Property="BorderBrush" Value="{DynamicResource DisabledBackgroundBrush}" TargetName="Border"/>
            <Setter Property="Foreground" Value="{DynamicResource DisabledForegroundBrush}"/>
          </Trigger>
          <Trigger Property="IsEnabled" Value="True">
            <Setter Property="Foreground" Value="{Binding Path=Foreground, RelativeSource={RelativeSource AncestorType={x:Type TextBox}}}"/>
          </Trigger>
        </ControlTemplate.Triggers>
      </ControlTemplate>
    </Setter.Value>
  </Setter>
</Style>


<TextBox Text="TEST" TextWrapping="Wrap" Canvas.Top="293.761" Canvas.Left="112" Style="{DynamicResource TextBoxControlTemplate1}" Height="28.724" Width="232.25" IsTabStop="False" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" HorizontalContentAlignment="Right" VerticalContentAlignment="Center" IsEnabled="True" Foreground="#FFFF0000"/>
有几个想法可以尝试:

摆脱你的一个触发器。有两个相反的触发点可能不是一个好主意。我将直接在边界声明上设置默认背景、边界笔刷和前景,并删除Enabled=True触发器。然后,调试只需正确设置Enabled=False触发器

为Enabled=False触发器将TargetName属性添加到setter

这是一个longshot,但请使用UIElement.IsEnabled,而不仅仅是IsEnabled,如下所示:


希望我在这里说的话能有所帮助

可能的原因是,在使用样式时,禁用的BackgroundBrush不可见。请尝试将样式添加到ControlTemplate的资源中:

<ControlTemplate TargetType="{x:Type TextBox}">
    <ControlTemplate.Resources>
        <SolidColorBrush x:Key="DisabledBackgroundBrush" Color="#EEE" />
        ...
    </ControlTemplate.Resources>
    ...
顺便说一下,控件模板不尊重属性的值。 例如,您可能应该使用以下内容

<ScrollViewer Margin="{TemplateBinding Padding}" x:Name="PART_ContentHost"/>

在您的控件模板中。

这里有一些不同的问题,它们都对您不利。第一个是在控件实例上设置一个特定的前景值,对于控件本身的属性,该值的优先级高于从样式中的触发器设置的值。这不同于在ControlTemplate内的元素上设置的属性,如Border。您使用触发器设置边框属性的方式说明了这一点。通常,您还希望使用TemplateBindings将控件实例上设置的值作为默认值(如后台)拉入,这些值当前被忽略

要在已设置样式的控件上的属性的两个值之间切换,就像您希望对前台所做的那样,可以在样式中使用Setter和触发器来提供默认值和备用值。这仍然会被实例上设置的值覆盖。如果要禁止实例重写触发器,请将其设置为类似于边界触发器,在该触发器中,您将在ControlTemplate内的元素上设置值

我建议的最后一个更改是切换到StaticResource,以便将画笔添加到您的样式中。在这种情况下,它可能不会有什么不同,但在某些情况下,默认样式可能会被拉入一个上下文中,而该上下文没有从声明它的文件中引用周围的资源。使用Static将保证无论在哪里使用,它都会包含这些资源。您可能不会遇到这种情况,但在设置这样的样式/模板时,这是一个好习惯

以下是您的代码和这些改进:

<SolidColorBrush x:Key="DisabledForegroundBrush" Color="#888" />
<SolidColorBrush x:Key="DisabledBackgroundBrush" Color="#EEE" />

<Style x:Key="TextBoxControlTemplate1" TargetType="{x:Type TextBox}">
    <Setter Property="KeyboardNavigation.TabNavigation" Value="None"/>
    <Setter Property="AllowDrop" Value="true"/>
    <Setter Property="Background">
        <Setter.Value>
            <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                <GradientStop Color="#FF000000" Offset="0"/>
                <GradientStop Color="#FF4D4D4D" Offset="1"/>
            </LinearGradientBrush>
        </Setter.Value>
    </Setter>
    <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
    <Setter Property="VerticalContentAlignment" Value="Stretch"/>
    <Setter Property="FontFamily" Value="Segoe UI"/>
    <Setter Property="FontSize" Value="12"/>
    <Setter Property="Padding" Value="8,5,3,3"/>
    <Setter Property="BorderThickness" Value="2"/>
    <Setter Property="BorderBrush" Value="#FF000000"/>
    <Setter Property="Foreground" Value="Red" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type TextBox}">
                <Grid>
                    <!--Take advantage of containment when possible to let the layout engine help you!-->
                    <Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" CornerRadius="5" Padding="0" Background="#FF000000"/>
                    <Border x:Name="Border" BorderBrush="#FFFFFFFF" BorderThickness="1" CornerRadius="5" Padding="0" Margin="{TemplateBinding BorderThickness}"
                            Background="{TemplateBinding Background}"/>
                    <ScrollViewer Margin="0" x:Name="PART_ContentHost"/>
                </Grid>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsEnabled" Value="False">
                        <Setter Property="Background" Value="{StaticResource DisabledBackgroundBrush}" TargetName="Border"/>
                        <Setter Property="BorderBrush" Value="{StaticResource DisabledBackgroundBrush}" TargetName="Border"/>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
    <Style.Triggers>
        <Trigger Property="IsEnabled" Value="False">
            <Setter Property="Foreground" Value="{StaticResource DisabledForegroundBrush}"/>
        </Trigger>
    </Style.Triggers>
</Style>
<TextBox Text="TEST" TextWrapping="Wrap" Canvas.Top="293.761" Canvas.Left="112" Style="{DynamicResource TextBoxControlTemplate1}" 
         Height="28.724" Width="232.25" IsTabStop="False" HorizontalContentAlignment="Right" VerticalContentAlignment="Center" />