2013-02-21 45 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的1和TextBox都是必填字段,現在,該按鈕不啓用,除非這兩個框有文字。

我想要做以下事情:當在組合框中選擇偶數時,我想讓textbox2條目成爲可選項。這意味着,我從多重綁定(按鈕的IsEnabled)中刪除它,並刪除樣式。但是,當選擇奇數時,我希望他們回來。

有人可以幫忙嗎?

回答

0

我能夠通過有條件地檢查組合框的selectionchanged事件上的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

雖然這可行,但您創建自動化單元測試的難度有多大。此外,這是嵌入到用戶界面中的業務邏輯(某些情況下爲可選數據)。也許這是一個小應用程序,不需要MVVM的複雜性,但我希望其他人可能會發現此解決方案考慮此解決方案的缺點。 – 2013-02-26 22:12:23

+0

我同意。你建議一個更好的解決方案,而不將其轉換爲MVVM? – naspras 2013-05-22 20:17:30

0

我認爲根據可選條目更改MultiBinding會有問題。我的建議是使用ViewModel併爲IsButtonEnabled創建一個Dependency屬性,並將ViewModel中的可選條目和驗證的邏輯。然後你可以綁定到IsButtonEnabled DP。

+0

謝謝。但是我打算這樣或者無論如何不創建ViewModel。你能幫忙嗎? – naspras 2013-02-21 18:02:09