WPF TextBox 在验证期间使用触发器设置文本

发布于 2024-09-25 20:36:07 字数 2172 浏览 3 评论 0原文

我有一个要求,当用户输入错误的输入时,我必须将 TextBox 的值恢复为旧值。我正在使用 MVVM 框架,所以我不想编写任何隐藏代码。

TextBox 的文本和标签是来自 ViewModel 变量的数据绑定。所以我的 TextBox 的 Tag 字段将始终具有旧值。我想使用标签字段值来恢复我的文本值。

  <Style TargetType="{x:Type TextBox}">
        <Setter Property="Validation.ErrorTemplate">
            <Setter.Value>
                <ControlTemplate>
                    <DockPanel LastChildFill="True">
                        <TextBlock DockPanel.Dock="Right" 
                    Foreground="Orange"
                    FontSize="12pt">

                </TextBlock>
                        <Border BorderBrush="Red" BorderThickness="1">
                            <AdornedElementPlaceholder />
                        </Border>
                    </DockPanel>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
        <Style.Triggers>
            <Trigger Property="Validation.HasError" Value="true" >
                <Setter Property="ToolTip" 
                        Value="{Binding Path=Tag,RelativeSource={RelativeSource Self}}">
                </Setter>
                <Setter Property="Text"
                            Value="{Binding Path=Tag,RelativeSource={RelativeSource Self}}">
                </Setter>
            </Trigger>
        </Style.Triggers>
    </Style>


  <TextBox Width="68" Tag="{Binding SampleText}" Height="23" HorizontalAlignment="Left" Margin="39,37,0,0" VerticalAlignment="Top" >
        <TextBox.Text>
            <Binding Path="SampleText"  NotifyOnValidationError="True" ValidatesOnDataErrors="True" ValidatesOnExceptions="True">
                <Binding.ValidationRules>
                    <val:SampleTextValidator></val:SampleTextValidator>
                </Binding.ValidationRules>
            </Binding>
        </TextBox.Text>        
 </TextBox>


现在,当发生错误时,文本框会突出显示为红色。我编写了一个触发器来将值恢复为原始值(存储在标记字段)。 Tt 不工作。但工具提示部分正在工作。我完全困惑了。请帮助我哪里做错了!!!如果可能的话用示例代码纠正我!!!!

I am having a requirement where in I have to revert the values of a TextBox to old value when the user enters a wrong input. I am using MVVM framework so I dont want to write any codebehind.

The Text and Tag of TextBox is databound from ViewModel variable. So my Tag field of TextBox will always have old value. I want to use the Tag field value to revert my Text value.

  <Style TargetType="{x:Type TextBox}">
        <Setter Property="Validation.ErrorTemplate">
            <Setter.Value>
                <ControlTemplate>
                    <DockPanel LastChildFill="True">
                        <TextBlock DockPanel.Dock="Right" 
                    Foreground="Orange"
                    FontSize="12pt">

                </TextBlock>
                        <Border BorderBrush="Red" BorderThickness="1">
                            <AdornedElementPlaceholder />
                        </Border>
                    </DockPanel>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
        <Style.Triggers>
            <Trigger Property="Validation.HasError" Value="true" >
                <Setter Property="ToolTip" 
                        Value="{Binding Path=Tag,RelativeSource={RelativeSource Self}}">
                </Setter>
                <Setter Property="Text"
                            Value="{Binding Path=Tag,RelativeSource={RelativeSource Self}}">
                </Setter>
            </Trigger>
        </Style.Triggers>
    </Style>


  <TextBox Width="68" Tag="{Binding SampleText}" Height="23" HorizontalAlignment="Left" Margin="39,37,0,0" VerticalAlignment="Top" >
        <TextBox.Text>
            <Binding Path="SampleText"  NotifyOnValidationError="True" ValidatesOnDataErrors="True" ValidatesOnExceptions="True">
                <Binding.ValidationRules>
                    <val:SampleTextValidator></val:SampleTextValidator>
                </Binding.ValidationRules>
            </Binding>
        </TextBox.Text>        
 </TextBox>

Now When an error happens, the TextBox is highlighted red.I have written a Trigger to revert the value back to original value (value stored in Tag field). Tt is not working. But Tooltip part is working. I am confused fully. Please help where am I doing wrong!!!. Correct me with a sample code if possible!!!!

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

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

发布评论

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

评论(1

心房的律动 2024-10-02 20:36:07

我的第一个猜测是,当您使文本输入无效(例如删除所有值)时,您会导致标记绑定到相同的值,因此,它将反映一个空字符串。

您需要的是一个单独的属性,用于将您的标签绑定到原始值。

private string _oldValue;
public string OldValue
{
    get {...}
    set {... NotifyPropertyChanged()...}
}

private string _sampleText;
public string SampleText
{
    get { return _sampleText; }
    set {
            OldValue = _sampleText;
            _sampleText = value;
            NotifyPropertyChanged(...);
        }
}

<TextBox Width="68" Tag="{Binding OldValue}" ... >

不要忘记实现 INotifyPropertyChanged。

My first guess is that when you made your text input invalid (eg. delete all values), you cause the tag to bind to the same value, hence, it will reflect an empty string.

What you need is a separate property for your original value to bind your tag to.

private string _oldValue;
public string OldValue
{
    get {...}
    set {... NotifyPropertyChanged()...}
}

private string _sampleText;
public string SampleText
{
    get { return _sampleText; }
    set {
            OldValue = _sampleText;
            _sampleText = value;
            NotifyPropertyChanged(...);
        }
}

<TextBox Width="68" Tag="{Binding OldValue}" ... >

Don't forget to implement INotifyPropertyChanged.

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