【发布时间】:2012-05-22 22:43:47
【问题描述】:
我有以下形式:
我在 MVVM 中实现了它,XAML 看起来像这样:
<!-- Username -->
<Label Grid.Row="2" Style="{StaticResource TypicalLabelStyle}">Username:</Label>
<TextBox Grid.Row="2" Grid.Column="1" Style="{StaticResource TypicalTextBoxStyle}" Name="UsernameTextBox"
Text="{Binding SourceConnection.Username, UpdateSourceTrigger=PropertyChanged}"
/>
<CheckBox Grid.Row="2" Grid.Column="2" Margin="5" VerticalAlignment="Center" Name="CopyPasswordCheckBox">Copy Password</CheckBox>
<!-- Password -->
<Label Grid.Row="3" Style="{StaticResource TypicalLabelStyle}">Password:</Label>
<TextBox Grid.Row="3" Grid.Column="1" >
<TextBox.Style>
<Style>
<Style.Triggers>
<DataTrigger Binding="{Binding ElementName=CopyPasswordCheckBox, Path=IsChecked}" Value="True">
<Setter Property="TextBox.Text" Value="{Binding ElementName=UsernameTextBox, Path=Text}" />
</DataTrigger>
<DataTrigger Binding="{Binding ElementName=CopyPasswordCheckBox, Path=IsChecked}" Value="False">
<Setter Property="TextBox.Text" Value="{Binding SourceConnection.Password}" />
</DataTrigger>
</Style.Triggers>
</Style>
</TextBox.Style>
</TextBox>
问题出在密码文本框中: 如果选中“复制密码”复选框,则绑定 SourceConnection.Password 为空。 在没有检查的情况下,我得到正确的值绑定。
(复制密码意味着将用户名文本框中的文本复制到密码文本框中)。 我不想在代码中保留“复制密码”的属性,然后询问是否...”。
【问题讨论】:
标签: wpf data-binding bind