【发布时间】:2019-09-06 11:48:10
【问题描述】:
我想在 TextChange 事件上执行一个方法,对于特定文本,我想做一些事情并使用 MVVM 关闭窗口
例如,在我的这部分代码中,我想关闭我的窗口:
if (text.Equals("12345"))
{
//Exit from window
}
我知道如何使用命令从按钮执行此操作,正如我在下一个示例的代码中所做的那样。
但是如何将窗口传递给绑定到文本框文本的运行属性?
或者还有另一种关闭表单的方法?
我想在 ViewModel 上执行此操作,而不是在后面的代码上将我的代码编写为 MVVM 模式,我可以这样做
我的 XAML 代码:
<Window x:Class="PulserTesterMultipleHeads.UserControls.TestWindows"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:PulserTesterMultipleHeads.UserControls"
mc:Ignorable="d"
Name="MainTestWindow"
Title="TestWindows" Height="450" Width="800">
<Grid>
<StackPanel>
<TextBox Text="{Binding Text,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"></TextBox>
<Button Command="{Binding EndTestExit}"
CommandParameter="{Binding ElementName=MainTestWindow}">Exit</Button>
</StackPanel>
</Grid>
</Window>
XAML 背后的代码:
using PulserTesterMultipleHeads.Classes;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
namespace PulserTesterMultipleHeads.UserControls
{
/// <summary>
/// Interaction logic for TestWindows.xaml
/// </summary>
public partial class TestWindows : Window
{
public TestWindows()
{
InitializeComponent();
DataContext = new TestWindowsMV();
}
}
}
查看模型代码:
using PulserTester.ViewModel.Base;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
namespace PulserTesterMultipleHeads.Classes
{
public class TestWindowsMV : INotifyPropertyChanged
{
private string text;
public string Text {
get {
return text;
} set {
text = value;
if (text.Equals("12345"))
{
//Exit from window
}
}
}
/// <summary>
/// for the example
/// </summary>
private ICommand _EndTestExit;
public ICommand EndTestExit
{
get
{
if (_EndTestExit == null)
{
_EndTestExit = new GenericRelayCommand<Window>((window) => EndTestExitAction(window));
}
return _EndTestExit;
}
}
private void EndTestExitAction(Window window)
{
window.Close();
}
public event PropertyChangedEventHandler PropertyChanged;
}
}
****************************** 编辑为答案 ****************** ********** ModelView 的变化:
public string Text {
get {
return text;
} set {
text = value;
if (text.Equals("12345"))
{
CloseAction();
}
}
}
后面代码的变化:
public TestWindows()
{
InitializeComponent();
DataContext = new TestWindowsMV();
if (((TestWindowsMV)DataContext).CloseAction == null)
((TestWindowsMV)DataContext).CloseAction = new Action(this.Close);
}
【问题讨论】:
-
作为解决方法,您可以默认关闭按钮不可见/禁用,并且仅当文本框中的文本满足您的条件时才启用它。不要忘记解雇
propertychanged(目前你的Text属性没有它) -
window参数是否可变?为什么不将窗口设置为视图模型中的属性,然后从设置器中的条件调用
EndTestExitAction(Window window)? -
@PavelAnikhouski 我希望这个隐藏的按钮在正确的字符串到来时自动拍摄.. 有可能吗?
-
没有什么反对使用 TextBox.TextChanged 事件来关闭您的窗口。另一方面,MVVM 中的所有内容都反对在 ViewModel 中引用您的 VIEW。 ViewModel 应该分开,它应该在没有 View 的情况下工作。如果您对当前的 ViewModel 进行单元测试,它将失败。
-
@NawedNabiZada MVVM 并不意味着将我的逻辑与我的观点分开?我在我的 ViewModel 中登录并在窗口中查看