[英]Clear multiple textbox value in wpf
I want to clear all text box value when i pressed a button.I used this code that is fine work in winform but when i am trying to use this same code in wpf then error occured at this.Controls position.Here is the code.Please give me a solution. 我想在按下按钮时清除所有文本框值。我在winform中使用了此代码,这很好用,但是当我尝试在wpf中使用相同的代码时,在this.Controls位置出现了错误。这是代码。请给我一个解决方案。
foreach (Control c in this.Controls)
if (c is TextBox)
(c as TextBox).Clear();
I recommend looking into the MVVM pattern for WPF to solve your question. 我建议研究WPF的MVVM模式来解决您的问题。
By binding a textbox and button in your view (XAML) to a view model (class) you can clear the textbox values directly in the button command. 通过将视图(XAML)中的文本框和按钮绑定到视图模型(类),可以直接在button命令中清除文本框值。 There are many good MVVM frameworks like: Cinch and MVVM light to get you started.
有很多不错的MVVM框架,例如: Cinch和MVVM light可以帮助您入门。
Here is a sample that uses Cinch, but what's important is: 这是使用Cinch的示例,但重要的是:
1. TextBox in row 0 uses TwoWay binding to Text1 1.第0行中的TextBox使用TwoWay绑定到Text1
2. TextBox in row 1 uses TwoWay binding to Text2 2.第1行中的TextBox使用TwoWay绑定到Text2
3. Button in row 2 uses Command binding to Clearcommand that sets Text1 and Text2 to string.Empty 3.第2行中的按钮使用Command绑定到Clearcommand,从而将Text1和Text2设置为string.Empty
Here is the view: 这是视图:
<Window x:Class="TextboxClear.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:meffed="clr-namespace:MEFedMVVM.ViewModelLocator;assembly=MEFedMVVM.WPF"
meffed:ViewModelLocator.ViewModel="MainWindowViewModel"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<TextBox Grid.Row="0" Text="{Binding Path=Text1, Mode=TwoWay}"/>
<TextBox Grid.Row="1" Text="{Binding Path=Text2, Mode=TwoWay}"/>
<Button Grid.Row="2" Content="Clear" Command="{Binding Path=ClearCommand}"/>
</Grid>
</Window>
Here is the view model: 这是视图模型:
using System;
using System.ComponentModel.Composition;
using Cinch;
using MEFedMVVM.ViewModelLocator;
namespace TextboxClear.ViewModels
{
[ExportViewModel("MainWindowViewModel")]
[PartCreationPolicy(CreationPolicy.Shared)]
public class MainWindowViewModel : ViewModelBase
{
[ImportingConstructor]
public MainWindowViewModel()
{
ClearCommand = new SimpleCommand<Object, Object>(CanExecuteClearCommand, ExecuteClearCommand);
}
private string _text1 = string.Empty;
public string Text1
{
get
{
return _text1;
}
set
{
_text1 = value;
NotifyPropertyChanged("Text1");
}
}
private string _text2 = string.Empty;
public string Text2
{
get
{
return _text2;
}
set
{
_text2 = value;
NotifyPropertyChanged("Text2");
}
}
public SimpleCommand<Object, Object> ClearCommand { get; private set; }
private void ExecuteClearCommand(Object args)
{
Text1 = string.Empty;
Text2 = string.Empty;
}
private bool CanExecuteClearCommand(Object args)
{
return true;
}
}
}
Use VisualTreeHelper.GetChild(). 使用VisualTreeHelper.GetChild()。 For example, if your textboxes are inside a StackPanel called StackPanelNew, use
例如,如果您的文本框位于称为StackPanelNew的StackPanel中,请使用
for (int i = 0;i < VisualTreeHelper.GetChildrenCount(this.StackPanelNew);i++) {
TextBox txt = VisualTreeHelper.GetChild(this.StackPanelNew, i) as TextBox;
if (txt != null)
{
//do stuff
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.