2013-06-06 5 views
0

내가 조용히 언젠가 달성하려고하는 시나리오가 있는데, 콤보 상자로 할 수있는 방법을 찾을 수 없습니다. 버튼과 텍스트 블록을 포함하는 Datatemplate이있는 콤보 상자가 있습니다. 버튼은 일부 이벤트에 바인딩되므로 사용자가 콤보 상자 안에서 클릭하면 이벤트가 발생합니다. 콤보 상자 항목을 선택하고 아무 것도 클릭하지 않고 단추를 클릭하려고하면 선택을 완료 할 때까지 제대로 작동합니다. 선택 버튼을 누르면 이벤트가 발생하지만 버튼을 클릭하면 개별 항목의 일부인 콤 상자에서 항목을 선택합니다. 이제 comb box의 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 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

ComboBox 용 업데이트 된 xaml을 게시 할 수 있습니까? –

+0

나는 단지 IsHitTestVisible = "True"를 콘트롤 박스의 controltemplate에있는 TRUE로 설정해야하고, 기본적으로 IsHitTestVisible = "False"가 false로 설정되어 모든 입력 이벤트를 무시할 수있는 솔루션을 얻었다. 이벤트를 실행시키는 것이 사실로 만듭니다. –