2012-08-10 39 views
1

當我在WPF中創建組合框時,我希望它有一個名稱就像「你的選擇」,就像它是一個普通的按鈕,當我點擊它時,我只想要項目下拉菜單,而不是組合框上的名稱。我希望你明白我的問題?有沒有辦法解決這個問題?在WPF中的組合框名稱

在XAML我用這個組合框:

<ComboBox Height="23" HorizontalAlignment="Left" Margin="110,226,0,0" Name="cmbChangeRoute" VerticalAlignment="Top" Width="156" SelectionChanged="cmbChangeRoute_SelectionChanged" /> 

我在C#代碼添加項目是這樣的:

string[] strChangeRoute = new string[] { "Your choice", "10 deg", "20 deg", "30 deg" }; 
foreach (string s in strChangeRoute) 
    cmbChangeRoute.Items.Add(s); 
cmbChangeRoute.SelectedIndex = 0; 
+0

爲什麼你想讓組合框的名稱(=「你的選擇」)在用戶點擊時消失?如果用戶點擊comboxbox(使名稱消失),然後直接單擊它在外面呢? – franssu 2012-08-10 08:55:50

+0

你是否試圖在你的組合框上疊加一個標籤控件? – NoviceProgrammer 2012-08-10 08:58:35

+0

不,我不想讓它消失,我只是不想要它兩次!正如我現在的代碼一樣,我在「按鈕」和下拉菜單中都可以看到「您的選擇」。 – 2012-08-10 09:01:41

回答

2

我會去一個TextBlock在組合框誰的知名度將被綁定到ComboBox的selectedItem(通過轉換器)。

<Grid> 
    <ComboBox x:Name="myComboBox" /> 
    <TextBlock Text="Your choice.." 
       IsHitTestVisible="False" 
       Visibility="{Binding ElementName=myComboBox, Path=SelectedItem, 
        Converter={StaticResource yourChoiceLabelVisibilityConverter}}"/> 
</Grid> 

public class YourChoiceLabelVisibilityConverter : IValueConverter 
{ 
    public object Convert(object value, Type targetType, object parameter, 
          System.Globalization.CultureInfo culture) 
    { 
     if (value == null) 
     { 
      return Visibility.Visible; 
     } 

     return Visibility.Hidden; 
    } 

OR,更好的解決方案:純XAML中,使用觸發器:

<ContentControl x:Name="myContentControl" Content="{Binding}"> 
     <ContentControl.ContentTemplate> 
      <DataTemplate> 
       <Grid> 
        <ComboBox x:Name="myComboBox" ItemsSource="{Binding}"/> 
        <TextBlock x:Name="myTextBlock" 
           Text="Your choice.." 
           IsHitTestVisible="False" 
           Visibility="Hidden"/> 
       </Grid> 
       <DataTemplate.Triggers> 
        <Trigger SourceName="myComboBox" Property="SelectedItem" 
          Value="{x:Null}"> 
         <Setter TargetName="myTextBlock" Property="Visibility 
           Value="Visible"/> 
        </Trigger> 
       </DataTemplate.Triggers> 
      </DataTemplate> 
     </ContentControl.ContentTemplate> 
    </ContentControl> 

在這種情況下,不要忘記從代碼隱藏設置的內容控制的的DataContext:

myContentControl.DataContext = Enum.GetValues(typeof([YOUR ENUM])); 
+0

這也可以使用觸發器,這是最好的使用方式(在我看來) – 2012-08-10 09:42:21

+0

謝謝Piotr Ptak,我添加了一個解決方案,使用觸發器來回答我的問題。 – franssu 2012-08-10 11:54:16

2

試試這個,我剛剛測試過

<ComboBox Height="23" HorizontalAlignment="Left" Margin="110,226,0,0" Name="cmbChangeRoute" VerticalAlignment="Top" Width="156" IsManipulationEnabled="False" IsEditable="True" Text="Your Choice..." SelectionChanged="cmbChangeRoute_SelectionChanged"> 
+0

好的,但是我把名字叫做「你的選擇」嗎? – 2012-08-10 09:43:12

+0

我會從strChangeRoute中刪除「您的選擇」 – 2012-08-10 09:45:46

+0

嗯,沒關係,但是我得到一個空的白線作爲第一項!?有沒有辦法消除這種情況? – 2012-08-10 09:52:29

2

您是否嘗試過使用綁定?

在XAML中你碰到這樣的

<ComboBox ... SelectedItem="{Binding ChosenValue,Mode=TwoWay}" ... /> 

然後,在你的構造(代碼隱藏)只需添加行

this.DataContext = this; 

讓你的綁定實際上看在代碼 - 在後面查找依賴屬性ChosenValue。這樣,每當組合框中的值發生變化時,您的道具的值將更新以保存當前選擇的項目。

達到你想要什麼,只是設置道具的價值,以「您的選擇」在構造函數中

public ClassName() 
{ 
    InitializeComponent(); 
    this.DataContext = this; 
    ChosenValue = "Your Choice"; 
} 

類似的,只是它的值設置爲相同的字符串在其他地方你想要的。當保存或什麼,只是檢查

if(!ChosenValue.Equals("Your Choice") 
{ 
    //do logic 
} 
else 
{ 
    //the user has not selected anything 
} 

希望這有助於!

+0

僅供參考:當您用四個空格縮進代碼時,不需要添加反引號。反引號僅用於內聯代碼塊。 – quetzalcoatl 2012-08-10 09:52:00

+0

我明白了。感謝那!這是我的第一個答案,所以我不完全確定它是如何工作的。 – Dorian 2012-08-10 10:59:22

2

你想要做的事實不是配置ComboBox,而是在它上面添加一個裝飾器/裝飾器,當Combo關閉時會顯示一個文本,並且在組合關閉時會自動隱藏。它有時被稱爲「水印」。

我不會進一步解釋它,因爲它毫無意義。這裏有一篇不錯的文章:http://pwlodek.blogspot.com/2009/11/watermark-effect-for-wpfs-textbox.html還有所有爲組合框加水印所需的代碼片段。

0

你可以參照這一條, How to display default text "--Select Team --" in combo box on pageload in WPF?

我建議從這個論壇下面的解決方案,

您可以使用的IValueConverter做到這一點無需任何代碼後面。

<Grid> 
    <ComboBox 
     x:Name="comboBox1" 
     ItemsSource="{Binding MyItemSource}" /> 
    <TextBlock 
     Visibility="{Binding SelectedItem, ElementName=comboBox1, Converter={StaticResource NullToVisibilityConverter}}" 
     IsHitTestVisible="False" 
     Text="... Select Team ..." /> 
</Grid> 

在這裏你有轉換器類,你可以重新使用。

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 
} 

最後,您需要在資源部分聲明您的轉換器。

<Converters:NullToVisibilityConverter x:Key="NullToVisibilityConverter" /> 

其中轉換器是放置轉換器類的地方。一個例子是:

xmlns:Converters="clr-namespace:MyProject.Resources.Converters" 

這種方法非常好的一點是沒有重複的代碼在你的代碼背後。