2009-09-30 4 views
2

사용자 이름 텍스트 상자와 암호 상자가 포함 된 로그인 양식이 있습니다.MultiBinding을 사용하여 요소 바인딩

두 필드에 값이있는 경우에만 확인 버튼을 사용하고 싶습니다.

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

Convert 메서드의 첫 번째 줄에 중단 점을 배치했으며 이후에 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"를 설정할 수있는 옵션이 더 있습니다

하나를 : 단지 양식을 초기화 할 때 구속되지입니다. 아이디어?

+0

tbUserName 및 tbPassword 컨트롤에 대해 XAML을 추가하고 TrueForAllConverter 리소스를 선언 할 수 있습니까? –

+0

제리가 올바른 길로 가고 있다고 생각합니다. UpdateSourceTrigger 특성을 TextBoxes의 바인딩에 다시 추가해야 포커스가 컨트롤을 벗어날 때만이 아니라 MultiBinding이 변경 될 때 알림을받습니다. –

+0

내가 싫어하고 그것이 작동하지 않는 것을 볼 수 없다면 나는 응답하지 않을 것이다. TrueForAllConverter 관련 Convert 메서드 위에 중단 점을 배치 했으므로 MenuItem이 초기화 될 때만 중단되므로 변환기에 무엇이 있는지 신경 쓰지 않아도됩니다. 걱정하지 말고 처음에는 시도해야합니다. 변환기가 무엇인지 정말 궁금하다면 게시자에게 게시합니다. – Shimmy

답변

2

명령 바인딩을 살펴 보시기 바랍니다. 명령은 일부 조건 (즉, 사용자 이름과 암호가 비어 있지 않음)에 따라 로그인 버튼을 자동으로 활성화 또는 비활성화 할 수 있습니다.

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 명령

public LoginWindow() 
{ 
    InitializeComponent(); 

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

더 readigin here 뒤에

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

또는 코드에서 XAML

에서 명령을 등록하려면이

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

처럼 보일 것이다 바인딩.

+0

+1 ... * PasswordBox *의 * Password * 속성이 종속성 속성이 아니기 때문에 명령 바인딩이 그 이유를 설명하는 이유를 언급하면 ​​사람들이 검색을 조금만 저장할 수 있습니다. 그것이 변경되었음을 알리지 않습니다. 이 특별한 문제에 대한 [여기 좋은 블로그 게시물입니다] (http://blog.functionalfun.net/2008/06/wpf-passwordbox-and-data-binding.html). – slugster

0

UpdateSourceTriggerPropertyChanged하고 ModeTwoWay에 설정하십시오. 이것은 당신이 입력 할 때 속성을 업데이트하게됩니다.이 있지만, 당신의 컨버터 작동하는지 확실하지.

+0

나는 위의 시도하고 작동하지 않습니다. – Shimmy

0
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 
관련 문제