2013-02-18 54 views
3

的文本框的一部分,我們有一個樣式像組合框:單擊組合框

<Style x:Key="OurComboBox" TargetType="ComboBox"> 
    <!-- omitted style Properties --> 
     <Setter Property="Template"> 
      <Setter.Value> 
       <ControlTemplate TargetType="ComboBox"> 
        <Grid> 
         <ToggleButton Name="ToggleButton" 
             Grid.Column="2" 
             ClickMode="Press" 
             Focusable="false" 
             IsChecked="{Binding Path=IsDropDownOpen, 
                  Mode=TwoWay, 
                  RelativeSource={RelativeSource TemplatedParent}}" 
             Style="{StaticResource ComboBoxToggleButton}" /> 
         <ContentPresenter Name="ContentSite" 
              Margin="3,3,23,3" 
              HorizontalAlignment="Left" 
              VerticalAlignment="Center" 
              Content="{TemplateBinding SelectionBoxItem}" 
              ContentTemplate="{TemplateBinding SelectionBoxItemTemplate}" 
              ContentTemplateSelector="{TemplateBinding ItemTemplateSelector}" 
              IsHitTestVisible="False" /> 
         <TextBox x:Name="PART_EditableTextBox" 
           Margin="5,3,23,1" 
           HorizontalAlignment="Left" 
           VerticalAlignment="Center" 
           Background="Transparent" 
           Focusable="False" 
           FontFamily="Arial Narrow" 
           FontWeight="DemiBold" 
           Foreground="#FF404040" 
           IsReadOnly="True" 
           PreviewMouseDown="" 
           Style="{x:Null}" 
           Template="{StaticResource ComboBoxTextBox}" 
           Text="{TemplateBinding Text}" 
           Visibility="Hidden" /> 

      <!-- omitted PopUp and ControlTemplate.Triggers --> 

並在此基礎上,我們還有更具體的風格

<Style x:Key="comboBoxSpecialPage" 
      BasedOn="{StaticResource OurComboBox}" 
      TargetType="ComboBox"> 
     <Style.Triggers> 
      <Trigger Property="SelectedIndex" Value="-1"> 
       <Setter Property="Text" Value="Select value" /> 
      </Trigger> 
     </Style.Triggers> 
    </Style> 

導致文本「選擇價值「如果在組合框中沒有選擇任何東西,例如在應用程序的開始。

但是,當我直接點擊文本框的文本,沒有任何反應。

所以問題是: 如何實現打開PopUp,就像當組合框(沒有文本的部分)的其餘部分被點擊一樣?

CNC中 如果我被遺漏的有趣的部分,請讓我知道,我將它們添加即可。

+0

你真的想實現這樣的行爲?關於可用性,這是一個奇怪的解決方案,因爲您有一個用戶操作和兩個所需的行爲。如果我使用'ComboBox',我希望選擇我點擊的項目。我不希望'TextBox'切換到編輯模式。 – DHN 2013-02-18 10:45:40

+0

我想讓ComboBox表現爲一個ComboBox。我不希望TextBox部分是可編輯的,它應該顯示一些文本。如果單擊文本框,它應該彈出組合框。現在,它沒有。如果點擊未被文本覆蓋的區域,它會彈出。我怎樣才能讓它彈出點擊文​​本? – 2013-02-18 11:34:29

+0

啊,我很抱歉,現在我正在解決你的問題。 – DHN 2013-02-18 12:25:24

回答

2

也許IsHitTestVisible屬性是你在找什麼,更多的信息在這裏: Textbox tag and IsHitTestVisible property

+0

很抱歉回答遲到。嘗試與該財產,但迄今沒有成功。 – 2013-02-19 10:10:52

+0

好吧,現在它正在工作!必須設置IsHitTestVisible =「False」和「IsEnabled =」False「'。非常感謝你! – 2013-02-19 10:20:23

0

組合框有一個名爲DropDownStyle屬性。 將其設置爲DropDownList並且文本區域不再可編輯,即您必須從列表中進行選擇。

+0

它是一個System.Windows.Controls.ComboBox。認爲是沒有這樣的財產。 – 2013-02-19 10:10:13

+0

對不起我的壞。我正在考慮System.Windows.Forms.ComboBox。 – James 2013-02-19 14:39:33

相關問題