2011-08-26 68 views
2

我有一個組合框,用int表示年代。這些年我將它們添加到一個ObservableCollection,但我的問題是,當我加載項目默認情況下,組合框的空白。我想爲它設置一個默認名稱,比如「年」,但我不希望解決方案將isEditable設置爲true,或者在開始處插入字符串。我想要一個純xaml解決方案,如果它是可行的。將默認名稱設置爲WPF中的組合框

這是我當前的XAML文件:

<RSControls:SmoothScrollComboBox Grid.Column="1" x:Name="compilationYearCombo" Margin="7,2.04,0,2.04"                      
          SelectedValue="{Binding Path=SelectedYear}" 
          SelectedValuePath="" 
          ItemsSource="{Binding Years}" 
          DisplayMemberPath="" SelectionChanged="compilationYearCombo_SelectionChanged" IsSynchronizedWithCurrentItem="True" Grid.ColumnSpan="2" IsEditable="False" SelectedIndex="0" IsReadOnly="False" Text="Years"> 

         </RSControls:SmoothScrollComboBox> 

我嘗試添加一個<TextBlock Text="Years" />,但只有改變了所有的元素組合爲「年」。

我apreciatte細節explenation如何,我只是一個WPF的初學者。

謝謝。

+0

請問這是[SO問題](http://stackoverflow.com/questions/1426050/how-to-display-default-text-select-team-in-combo-box-on-pa geload-in-wpf)的幫助? –

+0

確實重複 – V4Vendetta

+0

我試過這個解決方案,但我不知道那是什麼CultureInfo參數是... – Kobe

回答

2

您可以將知名度轉化器添加到您的TextBlock

<TextBlock 
      Visibility="{Binding SelectedItem, ElementName=compilationYearCombo, Converter={StaticResource NullToVisibilityConverter}}" 
      IsHitTestVisible="False" 
      Text="Years" /> 

與該轉換器:

public class NullToVisibilityConverter : IValueConverter 
{ 
    #region Implementation of IValueConverter 

    public object Convert(object value, Type targetType, object parameter, CultureInfo culture) 
    { 
     return value == null ? Visibility.Visible : Visibility.Collapsed; 
    } 

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) 
    { 
     throw new NotImplementedException(); 
    } 

    #endregion 
} 
+0

我試過了,但我總是得到它不知道NullToVisibilityConverter – Kobe

+0

你'當然,首先需要將NullToVisibilityConverter添加到資源中 – UrbanEsc

0

要顯示的默認文本組合框中

' - - 選擇價值'
<ComboBox Height="23" HorizontalAlignment="Left" Margin="180,18,0,0" Name="cmbExportData" VerticalAlignment="Top" Width="148" ItemsSource="{Binding}" Text="-- Select Value --" AllowDrop="False" IsEditable="True" IsManipulationEnabled="False" IsReadOnly="True" />