如何在C#WPF中动态设置标签内容(来自代码) [英] How to set label content dynamically(from code behind) in C# WPF

查看:136
本文介绍了如何在C#WPF中动态设置标签内容(来自代码)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在动态创建textBox并为水印文本应用样式。它的工作正常,但是,文本显示为水印标签(仅限数字),我的风格硬编码,我想在后面的代码中设置,

以下是我的风格。我想从代码隐藏中传递它。

谢谢。



我尝试了什么:



I am creating textBox dynamically and applying style for watermark text. its working fine but, The text displayed as watermark label(Only Numeric) that is hard coded in my style that I want to set in code behind,
below are my style. I want to pass it from codebehind.
thanks.

What I have tried:

<Style x:Key="onlyNumericTextboxStyle" TargetType="{x:Type TextBox}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type TextBox}">
                        <Grid>
                            <Border Background="{TemplateBinding Background}" BorderBrush="#FF97A0A5" BorderThickness="1" SnapsToDevicePixels="True">
                                <ScrollViewer x:Name="PART_ContentHost" Margin="0,0,0,0" VerticalAlignment="Center" />
                            </Border>
                            <Label x:Uid="UsernameText" x:Name="WaterMarkLabel" Content="Only Numeric"
                               Visibility="Collapsed" Foreground="#1a1a1a" FontSize="10" Opacity="0.5"
                                   FontStyle="Italic"/>
                        </Grid>
                        <ControlTemplate.Triggers>
                            <MultiTrigger>
                                <MultiTrigger.Conditions>
                                    <Condition Property="Text" Value=""/>
                                </MultiTrigger.Conditions>
                                <Setter Property="Visibility" TargetName="WaterMarkLabel" Value="Visible"/>
                            </MultiTrigger>
                            <Trigger Property="IsEnabled" Value="False">
                                <Setter Property="Foreground" Value="DimGray"/>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

推荐答案

这是您正在尝试做的工作版本:

Here is a working version of what you are trying to do:
<Window x:Class="MainWindow"

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

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



        Title="Watermark Textbox demo" Height="350" Width="525">

    <Window.Resources>

        <SolidColorBrush x:Key="TextBox.Static.Border" Color="#FFABAdB3"/>
        <SolidColorBrush x:Key="TextBox.MouseOver.Border" Color="#FF7EB4EA"/>
        <SolidColorBrush x:Key="TextBox.Focus.Border" Color="#FF569DE5"/>

        <Style x:Key="CommandTextBoxStyle" TargetType="{x:Type TextBox}">
            <Setter Property="Background"

                    Value="{DynamicResource {x:Static SystemColors.WindowBrushKey}}"/>
            <Setter Property="BorderBrush"

                    Value="{StaticResource TextBox.Static.Border}"/>
            <Setter Property="Foreground"

                    Value="{DynamicResource {x:Static
                                                 SystemColors.ControlTextBrushKey}}"/>
            <Setter Property="BorderThickness" Value="1"/>
            <Setter Property="KeyboardNavigation.TabNavigation" Value="None"/>
            <Setter Property="HorizontalContentAlignment" Value="Left"/>
            <Setter Property="FocusVisualStyle" Value="{x:Null}"/>
            <Setter Property="AllowDrop" Value="true"/>
            <Setter Property="ScrollViewer.PanningMode" Value="VerticalFirst"/>
            <Setter Property="Stylus.IsFlicksEnabled" Value="False"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type TextBox}">
                        <Border x:Name="border"

                                BorderBrush="{TemplateBinding BorderBrush}"

                                BorderThickness="{TemplateBinding BorderThickness}"

                                Background="{TemplateBinding Background}"

                                SnapsToDevicePixels="True">
                            <Grid>
                                <ScrollViewer x:Name="PART_ContentHost"

                                              Focusable="false"

                                              HorizontalScrollBarVisibility="Hidden"

                                              VerticalScrollBarVisibility="Hidden"/>

                                <TextBlock IsHitTestVisible="False"

                                           Margin="{TemplateBinding Padding}"

                                           Text="Watermark text goes here"

                                           TextTrimming="CharacterEllipsis"

                                           FontStyle="Italic" Foreground="LightGray"

                                           DataContext="{Binding RelativeSource=
                                               {RelativeSource Mode=FindAncestor,
                                                               AncestorType=TextBox}}">
                                    <TextBlock.Style>
                                        <Style TargetType="{x:Type TextBlock}">
                                            <Setter Property="Visibility" 

                                                    Value="Collapsed"/>
                                            <Style.Triggers>
                                                <MultiDataTrigger>
                                                    <MultiDataTrigger.Conditions>
                                                        <Condition Binding="{Binding 
                                                                   Text}" Value=""/>
                                                        <Condition Binding="{Binding 
                                                                   IsKeyboardFocused}"

                                                                   Value="False"/>
                                                    </MultiDataTrigger.Conditions>
                                                    <MultiDataTrigger.Setters>
                                                        <Setter Property="Visibility" 

                                                                Value="Visible"/>
                                                    </MultiDataTrigger.Setters>
                                                </MultiDataTrigger>
                                            </Style.Triggers>
                                        </Style>
                                    </TextBlock.Style>
                                </TextBlock>

                            </Grid>
                        </Border>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
            <Style.Triggers>
                <MultiTrigger>
                    <MultiTrigger.Conditions>
                        <Condition Property="IsInactiveSelectionHighlightEnabled" 

                                   Value="true"/>
                        <Condition Property="IsSelectionActive" Value="false"/>
                    </MultiTrigger.Conditions>
                    <Setter Property="SelectionBrush"

                            Value="{DynamicResource {x:Static 
                                  SystemColors.InactiveSelectionHighlightBrushKey}}"/>
                </MultiTrigger>
            </Style.Triggers>
        </Style>

    </Window.Resources>

    <Grid HorizontalAlignment="Center">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto"/>
            <ColumnDefinition Width="Auto"/>
        </Grid.ColumnDefinitions>
        <TextBox Width="200" VerticalAlignment="Center" Padding="4"

                 Style="{DynamicResource CommandTextBoxStyle}"/>
        <Button Content="Search"

                Margin="10 0" Padding="10 5"

                Grid.Column="1" VerticalAlignment="Center"/>
    </Grid>

</Window>


If its in ControlTemplate it would be a messy thing to do... I would not recomend that. You would have to find that specific control in the applied template and that would cost you a lot.

Use bindings or behaviours instead...



And second... Don’t use Labels, use TextBlocks.
If its in ControlTemplate it would be a messy thing to do... I would not recomend that. You would have to find that specific control in the applied template and that would cost you a lot.
Use bindings or behaviours instead...

And second... Don't use Labels, use TextBlocks.


这篇关于如何在C#WPF中动态设置标签内容(来自代码)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆