Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/unix/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 清除文本框_C#_Wpf_Mvvm_Textbox - Fatal编程技术网

C# 清除文本框

C# 清除文本框,c#,wpf,mvvm,textbox,C#,Wpf,Mvvm,Textbox,我正在创建一个输入页面,并尝试实现一个重置按钮。单击按钮后,UI应再次为空 我认为输入一个空字符串可以解决这个问题。在代码中,它似乎起作用,并且值确实更改为“”,但在UI中,键入的文本保持可见(因此它不会显示空的“”字符串)。我也试过使用string.Empty,如中所建议的,但这似乎也不起作用 我是不是遗漏了什么?我是编程新手,所以如果我做错了什么可怕的事情,不要笑得太厉害;) 我使用MVVM模式和Fody Weaver来处理代码中属性更改的部分 用户界面/XAML <TextBlock

我正在创建一个输入页面,并尝试实现一个重置按钮。单击按钮后,UI应再次为空

我认为输入一个空字符串可以解决这个问题。在代码中,它似乎起作用,并且值确实更改为“”,但在UI中,键入的文本保持可见(因此它不会显示空的“”字符串)。我也试过使用string.Empty,如中所建议的,但这似乎也不起作用

我是不是遗漏了什么?我是编程新手,所以如果我做错了什么可怕的事情,不要笑得太厉害;)

我使用MVVM模式和Fody Weaver来处理代码中属性更改的部分

用户界面/XAML

<TextBlock Text="Naam:"
           Grid.Column="0"
           Style="{StaticResource InputInputBlock}"
           />

<TextBox Foreground="White"
         Grid.Column="1"
         Text="{Binding Name, Mode=TwoWay}"
         Style="{StaticResource InputInputBox}"
         />

<Button Content="Reset"
            Height="50"
            Width="150"
            Grid.Column="0"
            Grid.Row="2"
            VerticalAlignment="Top"
            HorizontalAlignment="Center"
            Style="{StaticResource FlatButton}"
            Command="{Binding ResetCommand}"
            />

您可以在类中实现
INotifyPropertyChanged
接口。这对我很有用:

public class Person : INotifyPropertyChanged
    {
        private string _name;
        public string Name
        {
            get { return _name; }
            set
            {
                _name = value;
                // Call OnPropertyChanged whenever the property is updated
                OnPropertyChanged("Name");
            }
        }

        // Declare the event
        public event PropertyChangedEventHandler PropertyChanged;

        // Create the OnPropertyChanged method to raise the event
        protected void OnPropertyChanged(string name)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
        }
    }
XAML:


试试String.Empty。理想情况下,txtBoxControl.Clear()应该可以完成这项任务。-)在名称设置器中并没有通知,UI在调用命令后并没有看到视图模型中的更改。在中实现INotifyPropertyChanged并引发事件setter@PrateekShrivastava,我尝试了字符串。空的,但似乎没有任何作用。txtBoxControl.Clear()不能在MVVM模式中使用,是吗?如果是这样的话,你能详细说明一下吗?@ASh我确实忘了实现我的BaseViewModel接口。谢谢你指出!如果您将其作为解决方案发布,我们可以结束此问题。
public class Person : INotifyPropertyChanged
    {
        private string _name;
        public string Name
        {
            get { return _name; }
            set
            {
                _name = value;
                // Call OnPropertyChanged whenever the property is updated
                OnPropertyChanged("Name");
            }
        }

        // Declare the event
        public event PropertyChangedEventHandler PropertyChanged;

        // Create the OnPropertyChanged method to raise the event
        protected void OnPropertyChanged(string name)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
        }
    }
<TextBox Foreground="White"
     Grid.Column="1"
     Text="{Binding Name, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"
     Style="{StaticResource InputInputBox}"
     />
public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        DataContext = newPerson;
    }
    Person newPerson = new Person();
    private void button_Click(object sender, RoutedEventArgs e)
    {
        newPerson.Name = "";
    }
}