为文本框控件实现控件模板和数据触发器

发布于 2024-11-30 16:08:51 字数 1095 浏览 0 评论 0原文

我是 WPF 新手....我尝试为文本框实现 controltemplate 和 datatriggers ..当通过数据触发器输入的值不是“18”时,我想更改文本框的背景颜色..(我想要文本框的控制属性按原样工作)我怎样才能实现它...... 编写的Xaml代码如下:

    <TextBox x:Uid="txtagevals" x:Name="txtAge" Height="25" Width="80" Background="Wheat"  BorderThickness="1" BorderBrush="Black">
        <TextBox.Template>
            <ControlTemplate x:Uid ="txtagevals" TargetType="{x:Type TextBox}">
                <Border Background="{TemplateBinding Background}" 
                    BorderBrush="Black" BorderThickness="{TemplateBinding BorderThickness}" CornerRadius="5">
                      <ScrollViewer x:Name="PART_ContentHost"/>
                </Border>
            <ControlTemplate.Triggers>
                <DataTrigger Binding="{Binding Age}" Value="18">
                    <Setter TargetName="" Property="Text" Value="Green" />
                </DataTrigger>
            </ControlTemplate.Triggers>
        </ControlTemplate>
    </TextBox.Template>
</TextBox>

有人可以帮助我吗...谢谢..

I'm new to WPF....I have tried implementing controltemplate and datatriggers both for textbox..I want to change the background color of the textbox when the value entered in that is not "18" through datatriggers..(i want the controlproperty for the textbox to work as it is)how can i achieve it....
the Xaml code written is as follows:

    <TextBox x:Uid="txtagevals" x:Name="txtAge" Height="25" Width="80" Background="Wheat"  BorderThickness="1" BorderBrush="Black">
        <TextBox.Template>
            <ControlTemplate x:Uid ="txtagevals" TargetType="{x:Type TextBox}">
                <Border Background="{TemplateBinding Background}" 
                    BorderBrush="Black" BorderThickness="{TemplateBinding BorderThickness}" CornerRadius="5">
                      <ScrollViewer x:Name="PART_ContentHost"/>
                </Border>
            <ControlTemplate.Triggers>
                <DataTrigger Binding="{Binding Age}" Value="18">
                    <Setter TargetName="" Property="Text" Value="Green" />
                </DataTrigger>
            </ControlTemplate.Triggers>
        </ControlTemplate>
    </TextBox.Template>
</TextBox>

can anybody please help me...Thank you..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

记忆之渊 2024-12-07 16:08:51

尝试下面的代码来验证文本框中的文本:

<TextBox x:Uid="txtagevals" x:Name="txtAge" Height="25" Width="80">
            <TextBox.Style>
                <Style TargetType="{x:Type TextBox}">
                    <Setter Property="Background" Value="Red"/>
                    <Style.Triggers>
                        <Trigger Property="Text" Value="18">
                            <Setter Property="Background" Value="Green"/>
                        </Trigger>
                        <Trigger Property="Text" Value="">
                            <Setter Property="Background" Value="White"/>
                        </Trigger>
                    </Style.Triggers>
                </Style>
            </TextBox.Style>
        </TextBox>

在您的代码中,您可以这样做

 <TextBox x:Uid="txtagevals" x:Name="txtAge" Height="25" Width="80" BorderThickness="1" BorderBrush="Black">
                <TextBox.Style>
                    <Style TargetType="{x:Type TextBox}">
                        <Setter Property="Background" Value="Red"/>
                    </Style>
                </TextBox.Style>
                <TextBox.Template>
                    <ControlTemplate x:Uid ="txtagevals" TargetType="{x:Type TextBox}">
                        <Border Background="{TemplateBinding Background}"  
                    BorderBrush="Black" BorderThickness="{TemplateBinding BorderThickness}" CornerRadius="5">
                            <ScrollViewer x:Name="PART_ContentHost"/>
                        </Border>
                        <ControlTemplate.Triggers>
                            <Trigger Property="Text" Value="18">
                                <Setter Property="Background" Value="Green"/>
                            </Trigger>
                            <Trigger Property="Text" Value="">
                                <Setter Property="Background" Value="White"/>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </TextBox.Template>
            </TextBox>

您可以像下面这样绑定gridview,它对我来说工作正常:

<ListView Name="lstTest">
                <ListView.View>
                    <GridView>
                        <GridViewColumn Header="" Width="100">
                            <GridViewColumn.CellTemplate>
                                <DataTemplate>
                                    <TextBox x:Uid="txtagevals" x:Name="txtAge" Height="25" Width="80" BorderThickness="1" BorderBrush="Black" Text="{Binding Path=Age}">
                                        <TextBox.Style>
                                            <Style TargetType="{x:Type TextBox}">
                                                <Setter Property="Background" Value="Red"/>
                                            </Style>
                                        </TextBox.Style>
                                        <TextBox.Template>
                                            <ControlTemplate x:Uid ="txtagevals" TargetType="{x:Type TextBox}">
                                                <Border Background="{TemplateBinding Background}"  
                    BorderBrush="Black" BorderThickness="{TemplateBinding BorderThickness}" CornerRadius="5">
                                                    <ScrollViewer x:Name="PART_ContentHost"/>
                                                </Border>
                                                <ControlTemplate.Triggers>
                                                    <Trigger Property="Text" Value="18">
                                                        <Setter Property="Background" Value="Green"/>
                                                    </Trigger>
                                                    <Trigger Property="Text" Value="">
                                                        <Setter Property="Background" Value="White"/>
                                                    </Trigger>
                                                </ControlTemplate.Triggers>
                                            </ControlTemplate>
                                        </TextBox.Template>
                                    </TextBox>
                                </DataTemplate>

                            </GridViewColumn.CellTemplate>
                        </GridViewColumn>
                    </GridView>
                </ListView.View>
            </ListView>

Try below code to validate text in textbox :

<TextBox x:Uid="txtagevals" x:Name="txtAge" Height="25" Width="80">
            <TextBox.Style>
                <Style TargetType="{x:Type TextBox}">
                    <Setter Property="Background" Value="Red"/>
                    <Style.Triggers>
                        <Trigger Property="Text" Value="18">
                            <Setter Property="Background" Value="Green"/>
                        </Trigger>
                        <Trigger Property="Text" Value="">
                            <Setter Property="Background" Value="White"/>
                        </Trigger>
                    </Style.Triggers>
                </Style>
            </TextBox.Style>
        </TextBox>

In your code, you can do like this

 <TextBox x:Uid="txtagevals" x:Name="txtAge" Height="25" Width="80" BorderThickness="1" BorderBrush="Black">
                <TextBox.Style>
                    <Style TargetType="{x:Type TextBox}">
                        <Setter Property="Background" Value="Red"/>
                    </Style>
                </TextBox.Style>
                <TextBox.Template>
                    <ControlTemplate x:Uid ="txtagevals" TargetType="{x:Type TextBox}">
                        <Border Background="{TemplateBinding Background}"  
                    BorderBrush="Black" BorderThickness="{TemplateBinding BorderThickness}" CornerRadius="5">
                            <ScrollViewer x:Name="PART_ContentHost"/>
                        </Border>
                        <ControlTemplate.Triggers>
                            <Trigger Property="Text" Value="18">
                                <Setter Property="Background" Value="Green"/>
                            </Trigger>
                            <Trigger Property="Text" Value="">
                                <Setter Property="Background" Value="White"/>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </TextBox.Template>
            </TextBox>

You can bind gridview like below, it's working fine for me :

<ListView Name="lstTest">
                <ListView.View>
                    <GridView>
                        <GridViewColumn Header="" Width="100">
                            <GridViewColumn.CellTemplate>
                                <DataTemplate>
                                    <TextBox x:Uid="txtagevals" x:Name="txtAge" Height="25" Width="80" BorderThickness="1" BorderBrush="Black" Text="{Binding Path=Age}">
                                        <TextBox.Style>
                                            <Style TargetType="{x:Type TextBox}">
                                                <Setter Property="Background" Value="Red"/>
                                            </Style>
                                        </TextBox.Style>
                                        <TextBox.Template>
                                            <ControlTemplate x:Uid ="txtagevals" TargetType="{x:Type TextBox}">
                                                <Border Background="{TemplateBinding Background}"  
                    BorderBrush="Black" BorderThickness="{TemplateBinding BorderThickness}" CornerRadius="5">
                                                    <ScrollViewer x:Name="PART_ContentHost"/>
                                                </Border>
                                                <ControlTemplate.Triggers>
                                                    <Trigger Property="Text" Value="18">
                                                        <Setter Property="Background" Value="Green"/>
                                                    </Trigger>
                                                    <Trigger Property="Text" Value="">
                                                        <Setter Property="Background" Value="White"/>
                                                    </Trigger>
                                                </ControlTemplate.Triggers>
                                            </ControlTemplate>
                                        </TextBox.Template>
                                    </TextBox>
                                </DataTemplate>

                            </GridViewColumn.CellTemplate>
                        </GridViewColumn>
                    </GridView>
                </ListView.View>
            </ListView>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文