문제

사용자 이름 텍스트 상자와 비밀번호 상자가 포함 된 로그인 양식이 있습니다.

두 필드에 값이 포함 된 경우에만 OK 버튼을 활성화하려고합니다.

모든 문자열이 널 또는 비어있는 경우 모든 문자열을 점검하는 변환기가 있습니다.

변환 메소드의 첫 번째 줄에 중단 점을 배치했으며 MenuItem 텍스트를 변경할 때 초기, 애프터 워드를 초기화합니다.

다음 예는 잘 작동합니다. 문제는 텍스트를 변경할 때 멀티 핀딩이 트리거되지 않는다는 것입니다. 양식을 초기화 할 때만 묶여 있습니다.

<!--The following is placed in the OK button-->
<Button.IsEnabled>
    <MultiBinding Converter="{StaticResource TrueForAllConverter}">
        <Binding ElementName="tbUserName" Path="Text"/>
        <Binding ElementName="tbPassword" Path="Password"/>
    </MultiBinding>
</Button.IsEnabled>

문제는 원격 바인딩 소스가 변경 될 때 알림을받지 못한다고 생각합니다 (예 : 설정할 옵션이 없습니다. UpdateTargetTrigger="PropertyChanged".

어떤 아이디어?

도움이 되었습니까?

해결책

나는 당신이 명령 바인딩을 조사하는 것이 좋습니다. 명령은 일부 조건에 따라 로그인 버튼을 자동으로 활성화하거나 비활성화 할 수 있습니다 (예 : 사용자 이름과 암호는 비어 있지 않음).

public static RoutedCommand LoginCommand = new RoutedCommand();

private void CanLoginExecute(object sender, CanExecuteRoutedEventArgs e)
{
    e.CanExecute = !string.IsNullOrEmpty(_userInfo.UserName) && !string.IsNullOrEmpty(_userInfo.Password);
    e.Handled = true;
}

private void LoginExecute(object sender, ExecutedRoutedEventArgs e)
{
    MessageBox.Show("Loging in...");
    // Do you login here.
    e.Handled = true;
}

XAML 명령 바인딩은 다음과 같이 보입니다

<TextBox Text="{Binding UserName, UpdateSourceTrigger=PropertyChanged}" />
<TextBox Text="{Binding Password, UpdateSourceTrigger=PropertyChanged}" />
<Button Command="local:LoginWindow.LoginCommand" >Login</Button>

XAML에 명령을 등록하려면

<Window.CommandBindings>
    <CommandBinding Command="local:LoginWindow.LoginCommand" CanExecute="CanLoginExecute" Executed="LoginExecute" />
</Window.CommandBindings>

또는 코드 뒤에

public LoginWindow()
{
    InitializeComponent();

    CommandBinding cb = new CommandBinding(LoginCommand, CanLoginExecute, LoginExecute);
    this.CommandBindings.Add(cb);
}

더 유선 여기.

다른 팁

Private Sub tb_Changed(sender As Object, e As RoutedEventArgs) _
        Handles tbUsername.TextChanged, _
                tbPassword.PasswordChanged
    btnOk.IsEnabled = tbUsername.Text.Length > 0 _
              AndAlso tbPassword.Password.Length > 0
End Sub

설정해보십시오 UpdateSourceTrigger 에게 PropertyChanged 그리고 Mode 에게 TwoWay. 이로 인해 입력 할 때 속성이 업데이트됩니다. 그러나 이것이 컨버터에서 작동하는지 확실하지 않습니다.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top