简体   繁体   English

如何编写DataTrigger以更新WPF文本框中的Tag属性的值?

[英]How to Write DataTrigger to update the value of a Tag Property in a WPF TextBox?

I need to Write DataTrigger to update the value of a Tag Property in a WPF TextBox. 我需要编写DataTrigger来更新WPF TextBox中Tag属性的值。

If the TextBox Text.Count >0 then update the Tag property to True otherwise False . 如果TextBox Text.Count >0则将Tag属性更新为True否则更新为False

XAML Source Code:
<TextBox Text="WPF"  Tag="True">
    <TextBox.Triggers>
        <DataTrigger Property="Text" Value="0">
            <Setter Property="Tag" Value="False" />
        </DataTrigger>
    </TextBox.Triggers>
</TextBox>

Your code won't work because you can't put data triggers into a control's Triggers collection. 您的代码将无法工作,因为您无法将数据触发器放入控件的Triggers集合中。 What you actually need is a trigger in the control's Style . 您实际需要的是控件的Style的触发器。

Try this instead: 尝试以下方法:

<TextBox Text="WPF" Tag="True">
    <TextBox.Style>
        <Style TargetType="{x:Type TextBox}">
            <Style.Triggers>
                <DataTrigger Value="0"
                    Binding="{Binding Text.Length, RelativeSource={RelativeSource Self}}">
                    <Setter Property="Tag" Value="False" />
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </TextBox.Style>
</TextBox>

Note that this is not foolproof: if the text box contains only whitespace for example, then it may appear to be empty but the length of the text will be greater than zero. 请注意,这并非万无一失:例如,如果文本框仅包含空格,则该文本框可能看起来为空,但文本的长度将大于零。

As the answers from user2946329 and ATM show, there are various ways of doing this in a <Style> trigger. 如user2946329和ATM的答案所示,在<Style>触发器中有多种方法可以执行此操作。

DataTrigger has not the property. DataTrigger没有该属性。 You shoud use Property trigger for this purpose like this: 为此,应使用Property trigger ,如下所示:

<TextBox Text="WPF" Tag="True">
    <TextBox.Style>
        <Style TargetType="TextBox">
            <Style.Triggers>
                <Trigger Property="Text" Value="0">
                    <Setter Property="Tag" Value="False" />
                </Trigger>
            </Style.Triggers>
        </Style>
    </TextBox.Style>
</TextBox>

Or if you want to change the Tag based on text Length you should try this: 或者,如果您要基于文本长度更改Tag ,则应尝试以下操作:

<Trigger Property="Text" Value="">

I've used StevenRands answer and adapted my own: 我使用了StevenRands的答案并改编了自己的答案:

In this example Tag will be false if Text is null or empty. 在此示例中,如果Text为null或为空,则Tag将为false。

<StackPanel>
    <TextBox Text="WPF" Name="tb">
        <TextBox.Style>
            <Style TargetType="{x:Type TextBox}">
                <Setter Property="Tag" Value="True"/>
                <Style.Triggers>
                    <DataTrigger Value="" Binding="{Binding Text, RelativeSource={RelativeSource Self}}">
                        <Setter Property="Tag" Value="False" />
                    </DataTrigger>
                    <DataTrigger Value="{x:Null}" Binding="{Binding Text, RelativeSource={RelativeSource Self}}">
                        <Setter Property="Tag" Value="False" />
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </TextBox.Style>
    </TextBox>

    <!-- YOU CAN CHECK THE TAG'S VALUE HERE-->
    <TextBlock Text="{Binding ElementName=tb, Path=Tag}"/>
</StackPanel>

And here's StevenRands better answer with some changes to make it work: 以下是StevenRands做出的一些更改使其更有效的更好答案:

<TextBox Text="WPF">
        <TextBox.Style>
            <Style TargetType="{x:Type TextBox}">
                <Setter Property="Tag" Value="True"/>
                <Style.Triggers>
                    <DataTrigger Value="0" Binding="{Binding Text.Length, RelativeSource={RelativeSource Self}}">
                        <Setter Property="Tag" Value="False" />
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </TextBox.Style>
    </TextBox>

You will have to do something similar to this: 您将必须执行以下操作:

Here I described a style and attached it to a control. 在这里,我描述了一种样式并将其附加到控件上。

 <Style TargetType="telerik:BarIndicator" x:Key="Percent">
            <Style.Resources>
                <vc:LoadPercentValueConverter x:Key="LPValueConverter"/>
            </Style.Resources>
            <Style.Triggers>
                <DataTrigger Binding="{Binding Path=myEngine.PercentLoaded, Converter={StaticResource LPValueConverter}}" Value="1" >
                    <Setter Property="Background" Value="Red"></Setter>
                </DataTrigger>
                <DataTrigger Binding="{Binding Path=myEngine.PercentLoaded, Converter={StaticResource LPValueConverter}}" Value="0" >
                    <Setter Property="Background" Value="Green"></Setter>
                </DataTrigger>
            </Style.Triggers>
        </Style>

Here is the control: 这是控件:

<telerik:BarIndicator Style="{StaticResource Percent}" Visibility="{Binding WhileLoading}" Value="{Binding Path=myEngine.PercentLoaded}" StartWidth="0.13"/>

And you have to use a value converter. 而且您必须使用值转换器。 Here is mine. 这是我的。

class LoadPercentValueConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            long percent = (long)System.Convert.ChangeType(value, typeof(long));
            if (percent > 80)
            {
                return 1;
            }

            else
            {
                return 0;
            }
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM