[英]WPF - Bind a textbox's content other Textbox
我有两个在wpf mvvm中独立工作的文本框。 但是现在我想根据复选框将一个文本框的输入绑定到另一文本框。
<Label Grid.Row="4" Grid.Column="0" Style="{StaticResource StlLblForm}" Margin="0,5">First Name:</Label>
<TextBox Grid.Row="4" MaxLength="10" Grid.Column="1" Style="{StaticResource StlTxtForm}" Text="{Binding FirstName}" Grid.ColumnSpan="2" Margin="0,1,0,11" />
<Label Grid.Row="5" Grid.Column="0" Style="{StaticResource StlLblForm}" Margin="0,5">Last Name:</Label>
<TextBox Grid.Row="5" Grid.Column="1" Style="{StaticResource StlTxtForm}" Text="{Binding LastName}" AcceptsReturn="True" Grid.ColumnSpan="2" Margin="0,1,0,11" />
<CheckBox Content="CopyFirstNameToLast" />
因此,当我选中复选框时,它会将“名字”复制为“姓氏”。 我可以在没有代码的情况下执行此操作吗? 如果是,那怎么办?
当你在下面的MVVM模式来实现您的应用程序,你应该注意到的是,根据MVVM模式引导的观点不应包含任何应用程序逻辑:
该视图负责定义用户在屏幕上看到的内容的结构,布局和外观。 理想情况下,视图是完全由XAML定义的,并且背后的代码有限,其中不包含业务逻辑。
将FirstName
复制到LastName
是应用程序逻辑的一部分,因此您应该将此任务委托给ViewModel。
再次引用MSDN:
视图模型充当视图和模型之间的中介,并负责处理视图逻辑。
这正是您要实现的目标。
因此,为了使内容整洁,您应该在ViewModel中定义FirstName
和LastName
属性,该属性将绑定到View的TextBoxes,就像在问题代码中所做的那样:
// ViewModel
public const string FirstNamePropertyName = "FirstName";
private string _firstName = null;
public string FirstName
{
get
{
return _firstName;
}
set
{
_firstName = value;
RaisePropertyChanged(FirstNamePropertyName);
}
}
public const string LastNamePropertyName = "LastName";
private string _lastName = null;
public string LastName
{
get
{
return _lastName;
}
set
{
_lastName = value;
RaisePropertyChanged(LastNamePropertyName);
}
}
<!-- XAML -->
<Label Grid.Row="4" Grid.Column="0" Style="{StaticResource StlLblForm}" Margin="0,5">First Name:</Label>
<TextBox Grid.Row="4" MaxLength="10" Grid.Column="1" Style="{StaticResource StlTxtForm}" Text="{Binding FirstName}" Grid.ColumnSpan="2" Margin="0,1,0,11" />
<Label Grid.Row="5" Grid.Column="0" Style="{StaticResource StlLblForm}" Margin="0,5">Last Name:</Label>
<TextBox Grid.Row="5" Grid.Column="1" Style="{StaticResource StlTxtForm}" Text="{Binding LastName}" AcceptsReturn="True" Grid.ColumnSpan="2" Margin="0,1,0,11" />
虽然CheckBox可以通过两种方式处理:
Checked
事件绑定到ViewModel的命令,在其中您操作FirstName
和LastName
属性,并将复选框的IsChecked
属性值作为命令参数传递给它(困难的方式) IsChecked
属性绑定到ViewModel的boolean属性,并以其setter处理逻辑(简便方法) 因此,通过以下简单方法即可:
// ViewModel
public const string CheckedPropertyName = "Checked";
private bool _checked = false;
public bool Checked
{
get
{
return _checked;
}
set
{
_checked = value;
RaisePropertyChanged(CheckedPropertyName);
// app logic is handled here
if (_checked)
{
LastName = FirstName;
}
}
}
<!-- XAML -->
<CheckBox IsChecked="{Binding Checked}" />
没有代码:
<Label>First Name:</Label>
<TextBox x:Name="txtFirstName" Width="100" MaxLength="10" Text="{Binding FirstName}" />
<Label >Last Name:</Label>
<TextBox Width="100">
<TextBox.Style>
<Style TargetType="TextBox">
<Setter Property="Text" Value="{Binding LastName}" />
<Style.Triggers>
<DataTrigger Binding="{Binding ElementName=chk, Path=IsChecked, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Value="True">
<Setter Property="Text" Value="{Binding ElementName=txtFirstName, Path=Text, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
</DataTrigger>
</Style.Triggers>
</Style>
</TextBox.Style>
</TextBox>
<CheckBox x:Name="chk" Content="CopyFirstNameToLast" />
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.