2010-01-17 33 views
0

我有一個文本框和一個複選框,我想根據複選框是否被選中來設置文本框中的三個屬性。WPF - 轉換爲多個屬性可能嗎?

我可以將屬性綁定到複選框,但我需要知道在轉換器中綁定了哪些屬性。

例如,未選中如果我想的文本框的屬性是AcceptsReturn="False" TextWrapping="NoWrap" Height="25".

然後檢查:AcceptsReturn="True" TextWrapping="Wrap" Height="100".

請問這個需要3個轉換器或我可以告訴轉換器「,如果檢查==真& & boundfrom ==高度,返回100"

感謝, 鋼鈑


接受的解決方案

<TextBox Name="txtAnswer" Margin="5" VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Hidden" > 
       <TextBox.Style> 
        <Style> 
         <Style.Triggers> 
          <DataTrigger Binding="{Binding ElementName=cbMultiLine, Path=IsChecked}" Value="True"> 
           <Setter Property="TextBox.TextWrapping" Value="Wrap" /> 
           <Setter Property="TextBox.Height" Value="100" /> 
           <Setter Property="TextBox.AcceptsReturn" Value="True" /> 
          </DataTrigger> 
         </Style.Triggers> 
        </Style> 
       </TextBox.Style> 
      </TextBox> 

回答

2

我會認真考慮使用數據觸發。它的一個用戶關心,所以我會盡量避免使用你的視圖模型。你只需要幾行xaml就可以做到這一點。

+0

有效的點,這是我用來代替。非常感謝。 – 4imble 2010-01-18 10:54:58

1
<CheckBox x:Name="myCheckBox" /> 
<TextBox 
    AcceptsReturn="{Binding ElementName=myCheckBox, Path=IsChecked}" 
    TextWrapping="{Binding ElementName=myCheckBox, Path=IsChecked, Converter={StaticResource boolToTextWrappingConverter}}" 
    Height="{Binding ElementName=myCheckBox, Path=IsChecked, Converter={StaticResource boolToHeightConverter}}" 
/> 

這將其降低到2個轉換器。您也可以按照您在文章中建議的方式編寫一個boolToTextWrappingOrHeight轉換器,並通過CommandParameter=heightCommandParameter=textwrapping並查看轉換器中的參數,但我不是那種方法的粉絲。第三種選擇是在您的視圖模型中創建IsChecked,TextWrappingHeight屬性,綁定到這些屬性並將轉換邏輯放置在屬性中。

3

這應該通過使用綁定的ConverterParameter財產與一個轉換器工作:

Converter="{StaticResource MyIsCheckedConverter}" ConverterParameter="height" 

轉換器看起來像:

public class IsCheckedConverter : IValueConverter 
{ 
    public object Convert(
    object value, Type targetType, 
    object parameter, System.Globalization.CultureInfo culture) 
    { 
     object returnValue; 

     if (!((bool?)value).HasValue) 
      //do something if null (but I don't see why it would be) 

     switch ((string) parameter) 
     { 
      case "height": 
        returnValue = ((bool?)value).Value ? 100 : 25; 
        break; 
      case "TextWrapping": 
        ..... 
        ..... 
     } 

     return returnValue; 
    } 

    public object ConvertBack(
    object value, Type targetType, 
    object parameter, System.Globalization.CultureInfo culture) 
    { 
     //Likewise for convert back but I think in your case you wouldn't need to convert back 
    } 
} 
+0

謝謝,我不知道ConverterParameter。明天我會嘗試這個第一件事。 – 4imble 2010-01-18 01:18:17

+0

謝謝你,這正是我所要求的。但我選擇了數據觸發器。 +1 – 4imble 2010-01-18 10:55:50