2010-10-01 49 views
2

我有一個ListBox與一個ItemTemplate呈現一個兩列的網格。第一列是一個TextBlock,第二列是一個ComboBox。是否可以根據Silverlight 4中的可綁定屬性動態選擇要渲染的控件?

這個想法是向用戶呈現問題列表和用戶可以從中選擇答案的組合。該工程確定與此XAML:

 <ListBox x:Name="QAListBox" ScrollViewer.VerticalScrollBarVisibility="Auto" SelectedIndex="-1" 
      ItemsSource="{Binding Questions}" IsTabStop="True" TabIndex="5" 
      ScrollViewer.HorizontalScrollBarVisibility="Auto" Margin="10" BorderThickness="0"> 
      <ListBox.ItemTemplate> 
       <DataTemplate> 
        <Grid d:DesignWidth="931" d:DesignHeight="61" d:IsLocked="True" Margin="0"> 
         <Grid.ColumnDefinitions> 
          <ColumnDefinition Width=".80*" MinWidth="800"/> 
          <ColumnDefinition Width=".20*" MinWidth="200"/> 
         </Grid.ColumnDefinitions> 
         <TextBlock Text="{Binding Path=QuestionText}" Padding="10" FontSize="21.333" FontWeight="Bold" Margin="0" Grid.Column="0" d:IsLocked="True" /> 
         <ComboBox ItemsSource="{Binding Path=AnswerAlternative}" 
          SelectedValue="{Binding Path=QuestionsAndAnswers}" SelectedValuePath="AnswerAlternativeId" 
          FontSize="21.333" FontWeight="Bold" Grid.Column="1" Margin="60,0,0,0" d:IsLocked="True" SelectionChanged="ComboBox_SelectionChanged"> 
          <ComboBox.ItemTemplate> 
           <DataTemplate> 
            <TextBox Text="{Binding Path=AnswerText, Mode=TwoWay}" BorderThickness="0"> 
             <TextBox.Background> 
              <SolidColorBrush /> 
             </TextBox.Background> 
            </TextBox> 
           </DataTemplate> 
          </ComboBox.ItemTemplate> 
         </ComboBox> 
        </Grid> 
       </DataTemplate> 
      </ListBox.ItemTemplate> 
     </ListBox> 

之所以把一個TextBox中的DataTemplate內(在TextBlock中代替),是我在讓用戶除了輸入自由文本來選擇第一次嘗試從下拉菜單中選擇。但是,這是一種工作,TextBox在組合框內。這不是我想要的。

是否有可能有一個「普通」的文本框呈現而不是基於一些可綁定屬性的組合框?

因此,如果一個屬性InputType == FreeText視圖使用TextBox呈現,並且如果該屬性是Inputtype == Combo,它將如上所述呈現?

t。

回答

1

一個簡單的解決方案,您的具體問題是包括和可見性屬性使用值轉換器: -

public class EqualityToValueConverter<T> : IValueConverter 
{ 
    public T FalseValue { get; set; } 
    public T TrueValue { get; set; } 

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
    { 
     if (value == null) 
      return FalseValue; 
     else 
      return value.ToString().Equals(parameter) ? TrueValue : FalseValue; 
    } 

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
    { 
     return value != null && value.Equals(TrueValue) ? parameter : null; 
    } 
} 

public class EqualityToVisibilityConverter : EqualityToValueConverter<Visibility> { } 

然後XAML中可以看起來像: -

<ListBox x:Name="QAListBox" ScrollViewer.VerticalScrollBarVisibility="Auto" SelectedIndex="-1"  
     ItemsSource="{Binding Questions}" IsTabStop="True" TabIndex="5"  
     ScrollViewer.HorizontalScrollBarVisibility="Auto" Margin="10" BorderThickness="0"> 
     <ListBox.Resources> 
      <local:EqualityToVisibilityConverter x:Key="converter" 
      TrueValue="Visible" FalseValue="Collapsed" /> 
     </ListBox.Resources>  
     <ListBox.ItemTemplate>  
      <DataTemplate>  
       <Grid d:DesignWidth="931" d:DesignHeight="61" d:IsLocked="True" Margin="0">  
        <Grid.ColumnDefinitions>  
         <ColumnDefinition Width=".80*" MinWidth="800"/>  
         <ColumnDefinition Width=".20*" MinWidth="200"/>  
        </Grid.ColumnDefinitions>  
        <TextBlock Text="{Binding Path=QuestionText}" Padding="10" FontSize="21.333" FontWeight="Bold" Margin="0" Grid.Column="0" d:IsLocked="True" />  
        <ComboBox ItemsSource="{Binding Path=AnswerAlternative}"  
         SelectedValue="{Binding Path=QuestionsAndAnswers}" SelectedValuePath="AnswerAlternativeId"  
         FontSize="21.333" FontWeight="Bold" Grid.Column="1" Margin="60,0,0,0" d:IsLocked="True" SelectionChanged="ComboBox_SelectionChanged" 
         Visibility={Binding InputType, Converter={StaticResource converter}, ConverterParameter=Combo}">  
        <TextBox Text="{Binding Path=AnswerText, Mode=TwoWay}" Grid.Column="1" Margin="60,0,0,0" 
         Visibility={Binding InputType, Converter={StaticResource converter}, ConverterParameter=FreeText}">    
       </Grid>  
      </DataTemplate>  
     </ListBox.ItemTemplate>  
    </ListBox> 

更復雜解決方案將使用ListBox的子類和覆蓋或PrepareContainerForItemOverride來允許分配各種ItemTemplates。不過,我認爲這會對這個問題有所幫助。

相關問題