2013-06-06 38 views
0

有一個場景我試圖做到安靜一段時間,找不到做組合框的方式。 我有組合框,其中有一個數據模板包含一個按鈕和文本塊。該按鈕綁定到某個事件,因此當用戶在組合框內單擊它時會觸發事件。它運作良好,直到我選擇了一個組合框項目並嘗試單擊按鈕時沒有任何反應。當做選擇按鈕觸發事件時,一旦我從梳子框中選擇了按鈕是單個項目的一部分的項目。現在,當我嘗試點擊現在是梳子盒的selecteditem的按鈕時,它不會觸發任何事件。什麼都沒發生。選擇一次項目後,無法從WPF組合框項目中觸發事件?

我想要的按鈕,可點擊和火災事件,即使組合框項目是在選擇的模式。我怎樣才能做到這一點? 我希望我的問題是清楚的

代碼其次 -

<ComboBox x:Name="cbbox" Height="50" Width="200" ItemsSource="{Binding}"> 
     <ComboBox.ItemTemplate> 
      <DataTemplate> 
       <StackPanel Orientation="Horizontal"> 
        <Button Width="40" Height="30" Content="Clik" Click="Button_Click"></Button> 
        <TextBlock Text="{Binding val}"/> 
       </StackPanel> 
      </DataTemplate> 
     </ComboBox.ItemTemplate> 
    </ComboBox> 

感謝

VJ

+0

可以更新的問題,並把'ComboBox'你的XAML定義你的'DataTemplate'? – dkozl

+0

嗨,我已經添加了xaml的定義,我希望有助於理解。我希望我的問題是明確的,如果不讓我知道我會盡力更具體 –

+0

即使從下拉列表中選擇組合框項目,我也希望按鈕的單擊事件觸發。 –

回答

0

做到這一點的唯一方法是定義組合框一個新的ControlTemplate。我在下面創建了一個Style,它會爲您提供所需的功能。

<Style x:Key="ComboBoxStyle" TargetType="{x:Type ComboBox}"> 
     <Setter Property="Template"> 
      <Setter.Value> 
       <ControlTemplate TargetType="{x:Type ComboBox}"> 
        <Grid> 
         <Grid.ColumnDefinitions> 
          <ColumnDefinition/> 
          <ColumnDefinition MaxWidth="18"/> 
         </Grid.ColumnDefinitions> 
         <TextBox Name="PART_EditableTextBox" 
           Padding="5,0,0,0" 
           Background="Azure" 
           Height="{TemplateBinding Height}" 
           IsEnabled="{TemplateBinding IsEditable}"/> 
         <ToggleButton Grid.Column="1" Margin="0" 
            Height="{TemplateBinding Height}" 
            Focusable="False" 
            IsChecked="{Binding Path=IsDropDownOpen, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}" 
            ClickMode="Press"> 
          <Path Grid.Column="1" 
            HorizontalAlignment="Center" 
            VerticalAlignment="Center" 
            Data="M 0 0 L 4 4 L 8 0 Z" 
            Fill="DodgerBlue" /> 
         </ToggleButton> 
         <ContentPresenter Name="ContentSite" 
              Content="{TemplateBinding SelectionBoxItem}" 
              ContentTemplate="{TemplateBinding SelectionBoxItemTemplate}" 
              ContentTemplateSelector="{TemplateBinding ItemTemplateSelector}" 
              VerticalAlignment="Center" 
              HorizontalAlignment="Left" 
              Visibility="Visible" 
              Margin="5,0,0,0"/> 
         <Popup Name="Popup" 
           Placement="Bottom" 
           IsOpen="{TemplateBinding IsDropDownOpen}" 
           AllowsTransparency="True" 
           Focusable="False" 
           PopupAnimation="Slide"> 
          <Grid 
            Name="DropDown" 
            SnapsToDevicePixels="True"     
            MinWidth="{TemplateBinding ActualWidth}" 
            MaxHeight="{TemplateBinding MaxDropDownHeight}"> 
           <Border 
            x:Name="DropDownBorder" 
            Background="Azure" 
            BorderThickness="1" 
            BorderBrush="Azure"/> 
           <ScrollViewer Margin="4,6,4,6" SnapsToDevicePixels="True"> 
            <StackPanel IsItemsHost="True" KeyboardNavigation.DirectionalNavigation="Contained" /> 
           </ScrollViewer> 
          </Grid> 
         </Popup> 
        </Grid> 
        <ControlTemplate.Triggers> 
         <Trigger Property="HasItems" Value="false"> 
          <Setter TargetName="DropDownBorder" Property="MinHeight" Value="95"/> 
         </Trigger> 
         <Trigger Property="IsEnabled" Value="false"> 
          <Setter Property="Foreground" Value="Gray"/> 
         </Trigger> 
         <Trigger Property="IsGrouping" Value="true"> 
          <Setter Property="ScrollViewer.CanContentScroll" Value="false"/> 
         </Trigger> 
         <Trigger SourceName="Popup" Property="Popup.AllowsTransparency" Value="true"> 
          <Setter TargetName="DropDownBorder" Property="CornerRadius" Value="4"/> 
          <Setter TargetName="DropDownBorder" Property="Margin" Value="0,2,0,0"/> 
         </Trigger> 
         <Trigger Property="IsEditable" Value="true"> 
          <Setter Property="IsTabStop" Value="false"/> 
          <Setter TargetName="PART_EditableTextBox" Property="Visibility" Value="Visible"/> 
          <Setter TargetName="ContentSite" Property="Visibility" Value="Hidden"/> 
         </Trigger> 
        </ControlTemplate.Triggers> 
       </ControlTemplate> 
      </Setter.Value> 
     </Setter> 
    </Style> 

您需要修改顏色,利潤率等,以滿足您的需求,但它應該給你一個很好的起點。

+0

感謝您回覆我的帖子,但它不起作用。我曾嘗試使用相同的模板,但它沒有奏效。 –

+0

你可以發佈你的更新的組合框的xaml,我會看看? –

+0

我得到了解決方案,我只需要將組合框控件模板中的ContentPresenter的IsHitTestVisible =「True」設置爲TRUE並且它工作,默認情況下IsHitTestVisible =「False」設置爲false,因此它忽略任何輸入事件,因此一旦您讓它成真,它可以讓事件發生。 –

相關問題