启用文本框的TextChanged事件在WPF XAML由/关闭按钮 [英] Enable/disable Button on TextChanged event of TextBox in WPF by XAML

查看:206
本文介绍了启用文本框的TextChanged事件在WPF XAML由/关闭按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有TextBox和Button控件在WPF:

There are TextBox and Button controls in WPF:

<TextBox Name="BackUpTextBox" />
<Button  Name="BackUpSave" Content="Save" />

如果BackUpTextBox的文本更改

按钮BackUpSave必须启用。
我知道如何用C#code,以做到这一点BackUpTextBox TextChanged事件。
但是,有没有一种方法,使/ XAML所禁止的符号按钮?

Button BackUpSave must be enabled if a text of BackUpTextBox is changed. I know how to do that by BackUpTextBox TextChanged event by C# code. But is there a way to enable/disable button by XAML notation?

推荐答案

您可以做到这一点通过的EventTrigger 。总结这两个控件在的StackPanel 框TextChanged 事件路由高达其父的StackPanel

You can achieve that via EventTrigger. Wrap both controls in StackPanel so that TextChanged events routed upto its parent StackPanel.

而在这情况下,由 IsEnabled 设置为

And in that case enable Button by setting IsEnabled to True.

<StackPanel>
   <TextBox x:Name="BackUpTextBox"/>
   <Button x:Name="BackUpSave" Content="Save" IsEnabled="False"/>
   <StackPanel.Triggers>
      <EventTrigger RoutedEvent="TextBox.TextChanged">
         <BeginStoryboard>
           <Storyboard>
            <BooleanAnimationUsingKeyFrames Storyboard.TargetProperty="IsEnabled"
                                            Storyboard.TargetName="BackUpSave">
                <DiscreteBooleanKeyFrame KeyTime="0:0:0" Value="True"/>
            </BooleanAnimationUsingKeyFrames>
           </Storyboard>
        </BeginStoryboard>
     </EventTrigger>
     <EventTrigger RoutedEvent="Button.Click">
       <BeginStoryboard>
         <Storyboard>
          <BooleanAnimationUsingKeyFrames Storyboard.TargetProperty="IsEnabled"
                                          Storyboard.TargetName="BackUpSave">
                 <DiscreteBooleanKeyFrame KeyTime="0:0:0" Value="False"/>
          </BooleanAnimationUsingKeyFrames>
         </Storyboard>
       </BeginStoryboard>
     </EventTrigger>
     <EventTrigger RoutedEvent="Loaded">
       <BeginStoryboard>
         <Storyboard>
          <BooleanAnimationUsingKeyFrames Storyboard.TargetProperty="IsEnabled"
                                          Storyboard.TargetName="BackUpSave">
                 <DiscreteBooleanKeyFrame KeyTime="0:0:0" Value="False"/>
          </BooleanAnimationUsingKeyFrames>
         </Storyboard>
       </BeginStoryboard>
     </EventTrigger>
  </StackPanel.Triggers>
</StackPanel>


更新

如果你想在点击按钮的禁用按钮再次的,添加其他事件处理程序 Button.Click 并设置从禁用。我已经更新了XAML code表示。

In case you want to disable button on button click again, add another event handler for Button.Click and set it to disable from that. I have updated the xaml code for that.

更新2

解决方案看起来这样繁琐的,我有很大的怀疑它是否值得
  相比于初始,框TextChanged只设置IsEnabled属性
  和Click事件。是我错过了什么是XAML风格比较
  preferable可好?

Solution looks such cumbersome that I have big doubt if it worth compared to just setting IsEnabled property in Initial, TextChanged and Click events. Am I missed something that XAML style more preferable these days?

是的,我有同意,的 XAML唯一的解决办法是,有时它可以轻松地使用code 的后面来完成繁琐。具体到你的情况下,它可能已被简单地通过挂钩到某些事件,更重要的是的没有违反任何MVVM规则,因为你正在做的视图具体的事情在code后面的。所以,我看不出有什么害处从code这样做背后TI。

Yes, i have to agree with that XAML only solutions are sometimes cumbersome which can easily be done using code behind. Specifically in your case it could have been done simply by hooking onto some events and more importantly that's not violating any MVVM rules since you are doing view specific things in code behind. So, i see no harm in doing ti from code behind.

XAML唯一的解决办法我一般会preFER的在我想松散存储我的XAML一些文本文件,并想用加载在运行时XamlReader.Load()的情况下

XAML only solutions i would generally prefer in cases where i want to store my XAML loosely in some text file and want to load that at runtime using XamlReader.Load().

正如我提到的动画有超过当地的价值观制定者依赖属性较高的precedence。所以,一旦框TextChanged 事件得到升高值被设置为true,任何进一步的变化,从code,它背后的财产将无法正常工作。

And as i mentioned animation has higher precedence over local values setter for Dependency property. So, once TextChanged event gets raised value gets set to true and any further change to that property from code behind won't work.

但无论如何你可以通过动画设置它改变,从code的后面(将张贴在这里只是为了回答的完整性,并可能帮助其他人在这个帖子谁绊倒)。这是你如何从code背后做到这一点:

But anyhow you can change that from code behind by setting it via animation (will post here just for completeness of answer and might help others who stumble upon this post). This is how you do it from code behind:

private void Button_BackUpSave_Click(object sender, RoutedEventArgs e)
{
   EnableDisableBackUpSaveButton(false);
}

private void EnableDisableBackUpSaveButton(bool value)
{
   BooleanAnimationUsingKeyFrames animation = 
                              new BooleanAnimationUsingKeyFrames();
   DiscreteBooleanKeyFrame keyFrame = new DiscreteBooleanKeyFrame(value, 
                           KeyTime.FromTimeSpan(TimeSpan.FromMilliseconds(0)));
   animation.KeyFrames.Add(keyFrame);
   BackUpSave.BeginAnimation(Button.IsEnabledProperty, animation);
}

现在,在XAML你只能有单XAML触发框TextChanged

Now, in XAML you can only have single XAML trigger on TextChanged.

这篇关于启用文本框的TextChanged事件在WPF XAML由/关闭按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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