2013-02-21 3 views
0

나는 아래 보이는 간단한 XAML 있습니다수정 스타일과 multibinding 조건부

<Window.Resources> 
    <Converters:ButtonVisibilityConverter x:Key="m_ButtonConverter"/> 
</Window.Resources> 

<Grid> 
    <ComboBox Height="23" HorizontalAlignment="Left" Margin="71,18,0,0" Name="comboBox1" VerticalAlignment="Top" Width="229" SelectionChanged="comboBox1_SelectionChanged">    
    </ComboBox> 
    <TextBox Height="23" HorizontalAlignment="Left" Margin="71,76,0,0" Name="textBox1" VerticalAlignment="Top" Width="229" Style="{StaticResource requiredFieldValidationStyle}"/> 
    <TextBox Height="23" HorizontalAlignment="Left" Margin="71,125,0,0" Name="textBox2" VerticalAlignment="Top" Width="229" Style="{StaticResource requiredFieldValidationStyle}"/>   
    <Button Content="Submit" Height="23" HorizontalAlignment="Left" Margin="225,175,0,0" Name="button1" VerticalAlignment="Top" Width="75"> 
     <Button.IsEnabled> 
      <MultiBinding Converter="{StaticResource m_ButtonConverter}"> 
       <Binding ElementName="textBox1" Path="Text" /> 
       <Binding ElementName="textBox2" Path="Text" /> 
       <Binding ElementName="comboBox1" Path="Text" /> 
      </MultiBinding> 
     </Button.IsEnabled> 
    </Button> 
    <Label Content="Box 1" Height="28" HorizontalAlignment="Left" Margin="12,74,0,0" Name="label1" VerticalAlignment="Top" /> 
    <Label Content="Box 2" Height="28" HorizontalAlignment="Left" Margin="12,123,0,0" Name="label2" VerticalAlignment="Top" /> 
</Grid> 

으로 TextBox1 및 TextBox2를 지금 모두 필수 필드입니다, 그리고 두 상자가 텍스트를하지 않는 버튼을 사용하지 않습니다.

다음을 원합니다. 콤보 박스에서 짝수를 선택하면 textbox2 항목을 선택적으로 설정하려고합니다. 즉, 버튼의 IsEnabled에서 멀티 바인딩을 제거하고 스타일을 제거합니다. 그러나, 홀수가 선택되면, 나는 그들을 돌려주고 싶다.

누군가 도와 드릴 수 있습니까?

답변

0

콤보 박스의 선택 변경 이벤트에서 xaml.cs 파일을 조건부로 확인하여이 작업을 수행 할 수있었습니다.

private void comboBox1_SelectionChanged(object sender, SelectionChangedEventArgs e) 
    { 
     var selVal = (int)comboBox1.SelectedValue; 

     if ((selVal % 2) == 0) 
     { 
      // remove the style 
      textBox2.Style = null; 

      // remove from the button's IsEnabled multibinding     
      _vfs.NumberValidationFlag = false; 
      BindingOperations.ClearBinding(button1, Button.IsEnabledProperty); 
      BindingOperations.SetBinding(button1, Button.IsEnabledProperty, GetBindingForButton());     
     } 
     else 
     { 
      // add back the style 
      Style myStyle = (Style)Application.Current.Resources["requiredFieldValidationStyle"]; 
      textBox2.Style = myStyle; 

      // add back to the button's IsEnabled multibinding     
      _vfs.NumberValidationFlag = true; 
      BindingOperations.ClearBinding(button1, Button.IsEnabledProperty); 
      BindingOperations.SetBinding(button1, Button.IsEnabledProperty, GetBindingForButton()); 
     } 
    } 
+1

이 방법이 효과가 있지만 자동 단위 테스트를 만드는 데 얼마나 많은 노력을 했습니까? 또한 UI에 임베드 한 비즈니스 논리 (특정 상황의 선택적 데이터)입니다. 어쩌면이 응용 프로그램에서 작은 하나이며 MVVM의 복잡성을 필요로하지 않지만이 솔루션의 단점을 고려하는이 ​​솔루션을 찾을 수있는 다른 사람을 원합니다. –

+0

동의합니다. MVVM으로 변환하지 않고 더 나은 솔루션을 제안 하시겠습니까? – naspras

0

나는 선택 항목에 따라 MultiBinding을 변경하는 것이 문제가 될 것이라고 생각합니다. 내 제안은 ViewModel을 사용하고 IsButtonEnabled에 대한 종속성 속성을 만들고 ViewModel에 선택적 항목 및 유효성 검사를위한 논리를 배치하는 것입니다. 그러면 IsButtonEnabled DP에 바인딩 할 수 있습니다.

+0

감사합니다. 하지만 ViewModel을 만들지 않고 이런 식 으로든 또는 어쨌든 할려고합니다. 좀 도와 줄 수있어? – naspras