[英]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.