Mik*_* B. 4 c# forms data-binding wpf
鉴于:WPF 4.0基于桌面的应用程序.基本输入表单,包含两个TextBox字段和提交按钮.
XAML代码:
<Label Content="Username" />
<TextBox x:Name="Form_UserName" />
<Label Content="Password" />
<TextBox x:Name="Form_Password" />
<Button x:Name="Submit"
Click="Form_Submit_Button_Click"
Content="Submit" />
Run Code Online (Sandbox Code Playgroud)
任务:当且仅当TextBox填充了两个字段时,实现启用提交按钮的逻辑.
解决此问题的经典方法是使用诸如此类的事件处理程序onLostFocus(),我们可以在每次用户从字段切换焦点时控制此字段的条件.
但由于我的项目是基于WPF的,我更喜欢使用本机方式来处理表单 - 数据绑定机制.我从这个站点和MSDN上读了一些关于表单验证的文章,但几乎所有的例子都提出使用MVVM框架,我想在没有任何框架的情况下实现它.
此外,我试图玩,IMultiValueConverter但没有收到任何工作结果.
请指点(代码)建议如何用尽可能简单的数据绑定来解决这个问题(我只是从WPF开始).
这可以使用WPF验证机制轻松完成.首先,因为您想要遵循WPF架构,我建议您使用WPF Command模型.
现在要实现您的功能,您可以添加CommandBinding到Window/UserControl或Button自身:
<Button Content="Save" Command="Save">
<Button.CommandBindings>
<CommandBinding Command="Save"
Executed="Save_Executed" CanExecute="Save_CanExecute" />
</Button.CommandBindings>
</Button>
Run Code Online (Sandbox Code Playgroud)
现在,您可以CanExecute根据验证逻辑订阅事件以启用或禁用按钮.在继续之前我建议您阅读这些内容:
Windows Presentation Foundation中的验证
满足您要求的最简单方法如下:
XAML
<Window x:Class="GridScroll.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:GridScroll"
Title="Window1" Height="300" Width="300">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="200"/>
</Grid.ColumnDefinitions>
<TextBlock Text="User Name" Grid.Column="0" Grid.Row="0"/>
<TextBox Grid.Column="1" Grid.Row="0" Text="{Binding Path=UserName,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"/>
<TextBlock Text="Password" Grid.Column="0" Grid.Row="1"/>
<TextBox Grid.Column="1" Grid.Row="1" Text="{Binding Path=Password,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"/>
<Button Content="Save" Grid.Row="2" Grid.ColumnSpan="2" Width="100" HorizontalAlignment="Right" Command="Save">
<Button.CommandBindings>
<CommandBinding Command="Save"
Executed="Save_Executed" CanExecute="Save_CanExecute"/>
</Button.CommandBindings>
</Button>
</Grid>
Run Code Online (Sandbox Code Playgroud)
Code behind
public partial class Window1 : Window,INotifyPropertyChanged
{
public Window1()
{
InitializeComponent();
DataContext = this;
}
private string userName;
public string Username
{
get
{
return userName;
}
set
{
userName = value;
OnPropertyChanged("UserName");
}
}
private string password;
public string Password
{
get
{
return password;
}
set
{
password = value;
OnPropertyChanged("Password");
}
}
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(string name)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(name));
}
}
private void Save_Executed(object sender, ExecutedRoutedEventArgs e)
{
//Your code
}
private void Save_CanExecute(object sender, CanExecuteRoutedEventArgs e)
{
e.CanExecute = !(string.IsNullOrEmpty(Username) && string.IsNullOrEmpty(Password));
}
}
Run Code Online (Sandbox Code Playgroud)
希望这可以帮助.
| 归档时间: |
|
| 查看次数: |
7509 次 |
| 最近记录: |