【问题标题】:wpf bind to element and to propertywpf 绑定到元素和属性
【发布时间】: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


    【解决方案1】:

    我会添加一个布尔值来保存视图模型中的复选框状态。然后在视图模型中,您可以捕获用户名更改,检查复选框状态并相应地更新密码。

    【讨论】:

      猜你喜欢
      • 2015-08-30
      • 2017-07-20
      • 2018-05-14
      • 2011-01-27
      • 2013-11-05
      • 2010-09-30
      • 1970-01-01
      • 2017-10-10
      • 2020-07-07
      相关资源
      最近更新 更多