2016-12-02 59 views
0

我有两个文本框在wpf mvvm中独立工作。但是现在我想在一个复选框的基础上将一个文本框的输入绑定到其他文本框。WPF - 绑定文本框的内容其他文本框

<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" /> 

因此,当我选中复选框时,它将名字复制到姓氏。我可以做到这一点,没有代码背后。如果是,那么如何?

+0

你不必onproperty改变notificatio n – ntohl

+0

将名字绑定到XAML中的名字很简单,请参阅http://stackoverflow.com/questions/9586956/how-to-bind-a-controls-property-to-another-controls-property获取更多信息。但是,复选框使其更加复杂,您可能必须编写一个值转换器才能完成此项工作。 –

+0

也许这样? HTTP://计算器。com/questions/23225751/wpf-binding-to-two-properties –

回答

2

无代码:

<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" /> 
+0

正确的答案。 – AnjumSKhan

+0

就像一个魅力。非常感谢。这就是我正在寻找的:) –

2

当你在下面的MVVM模式来实现您的应用程序,你应该注意到,根据MVVM pattern guide认为应该包含任何应用程序逻辑:

的看法是负责定义的结构,布局,和 显示用户在屏幕上看到的内容。理想情况下,纯粹使用XAML定义的观点是 ,有限的代码隐藏不包含业务逻辑 。

复制你的FirstNameLastName应用程序逻辑的一部分,所以你应该将这个任务交给你的视图模型。

再次引用MSDN:

视图模型充当视图和模型之间的中介,并且 负责处理视图的逻辑。

这正是你想要实现的。

所以要保持整洁,你应该定义你的视图模型FirstNameLastName特性,这将势必查看的文本框,就像你做的代码在你的问题:

// 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" /> 

虽然复选框可以在两种处理方法:

  • 绑定其Checked事件的视图模型的命令,你 操纵你FirstNameLastName性质哟ü传递给它的复选框的IsChecked属性值的命令参数(硬路)
  • IsChecked属性绑定到视图模型的布尔属性和 处理在其制定者的逻辑(简单的方法)

因此,要做到这一点,简单的方法:

// 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}" /> 
+0

感谢您的帮助。但是我使用Bindablebase,它给出了RaisePropertyChanged没有定义的错误。 –

+0

然后您应该使用'BindableBase.SetProperty':https://msdn.microsoft.com/en-us/library/dn736263(v=pandp.50).aspx – Alex

+0

我已经使用setProperty –