2016-07-18 4 views
0

저는 XAML, WPF (첫 번째 프로젝트 ;-))에 신참입니다. 내 목표는 App.xaml 내에서 선언 된 맞춤형 콤보 박스를 적용하는 것입니다. 그러나 MainWindow.xaml 내에서 콤보 상자를 사용하면 콤보 상자 안에 고정되어있는 토글 단추에 클릭 처리기를 연결하는 방법을 알지 못합니다. App.xaml 내부의 ToggleButton에 Click 속성을 추가하면 App.cs.htm 내에 처리기를 구현 한 경우에만 이벤트를 캡처하는 데 성공했습니다.토글 버튼 (콤보 박스 내부) 클릭 핸들러 연결

지금까지 시도했다 :

  • 는, (나는 ...이 시점에서 어떻게 XAML 작품의 좋은 직관이없는) MainWindow.xaml에서 콤보 요소 내에 ToggleButton을 참조하는 결과는 ToggleButton에 원하지 않는 시각적 효과를 유발합니다.

    <ComboBox x:Name="someComboBox" 
          Height="23" 
          Margin="122,35,1.8,0" 
          VerticalAlignment="Top"> 
    
         <ToggleButton Click="toggleHandler_Click" /> 
    
  • 는 MainWindow.cs에서 ToggleButton을 반입하고

    ToggleButton tBtn = ((ComboBox)someComboBox).FindName("MyToggleButton") as ToggleButton; // null 
    
        ToggleButton tBtn = LogicalTreeHelper.FindLogicalNode(someComboBox, "MyToggleButton"); // null 
    
  • 여기

가 App.xaml 내부 맞춤 콤보이다 널로 결과를 아래의 방법으로 구하더라도 핸들러 첨부 내가 채택한 언어

 <Style TargetType="ComboBox"> 
      <Setter Property="Validation.ErrorTemplate" Value="{x:Null}" /> 
      <Setter Property="VerticalAlignment" Value="Center" /> 
      <Setter Property="OverridesDefaultStyle" Value="true" /> 
      <Setter Property="IsEditable" Value="True"/> 
      <Setter Property="SnapsToDevicePixels" Value="true" /> 
      <Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Auto" /> 
      <Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto" /> 
      <Setter Property="ScrollViewer.CanContentScroll" Value="true" /> 
      <Setter Property="Margin" Value="2" /> 
      <Setter Property="MinHeight" Value="20" /> 
      <Setter Property="Template"> 
       <Setter.Value> 
        <ControlTemplate TargetType="ComboBox"> 
         <Grid> 
          <VisualStateManager.VisualStateGroups> 
           <VisualStateGroup x:Name="CommonStates"> 
            <VisualState x:Name="Normal"/> 
            <VisualState x:Name="MouseOver"/> 
            <VisualState x:Name="Disabled"/> 
           </VisualStateGroup> 
          </VisualStateManager.VisualStateGroups> 
          <ToggleButton x:Name="MyToggleButton" 
              Grid.Column="2" 
              ClickMode="Press" 
              Focusable="false" 
              IsChecked="{Binding IsDropDownOpen, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}" 
              Template="{StaticResource ComboBoxToggleButton}"/> 

          <ContentPresenter Margin="3,3,23,3" 
               Content="{TemplateBinding SelectionBoxItem}" 
               ContentTemplate="{TemplateBinding SelectionBoxItemTemplate}" 
               ContentTemplateSelector="{TemplateBinding ItemTemplateSelector}" 
               HorizontalAlignment="Left" 
               IsHitTestVisible="False" 
               x:Name="ContentSite" 
               VerticalAlignment="Center" /> 

          <TextBox Style="{x:Null}" 
            x:Name="PART_EditableTextBox" 
            Margin="3,3,23,3" 
            Background="Transparent" 
            Focusable="True" 
            HorizontalAlignment="Left" 
            IsReadOnly="{TemplateBinding IsReadOnly}" 
            Template="{StaticResource ComboBoxTextBox}" 
            VerticalAlignment="Center" 
            Visibility="Hidden" /> 

          <Popup AllowsTransparency="True" Focusable="False" IsOpen="{TemplateBinding IsDropDownOpen}" x:Name="Popup" Placement="Bottom" PopupAnimation="Fade"> 
           <Grid MaxHeight="{TemplateBinding MaxDropDownHeight}" MinWidth="{TemplateBinding ActualWidth}" x:Name="DropDown" SnapsToDevicePixels="True"> 
            <Border x:Name="DropDownBorder" Background="White" BorderBrush="{StaticResource BorderDarkMainBrush}" BorderThickness="1" CornerRadius="0" /> 
            <ScrollViewer Margin="2" SnapsToDevicePixels="True"> 
             <StackPanel KeyboardNavigation.DirectionalNavigation="Contained" IsItemsHost="True" TextBlock.Foreground="Black" /> 
            </ScrollViewer> 
           </Grid> 
          </Popup> 
         </Grid> 
         <ControlTemplate.Triggers> 
          <Trigger Property="HasItems" Value="false"> 
           <Setter Property="MinHeight" TargetName="DropDownBorder" Value="95" /> 
          </Trigger> 
          <Trigger Property="IsGrouping" Value="true"> 
           <Setter Property="ScrollViewer.CanContentScroll" Value="false" /> 
          </Trigger> 
          <Trigger Property="IsEditable" Value="true"> 
           <Setter Property="IsTabStop" Value="false" /> 
           <Setter Property="Visibility" TargetName="PART_EditableTextBox" Value="Visible" /> 
           <Setter Property="Visibility" TargetName="ContentSite" Value="Hidden" /> 
          </Trigger> 
         </ControlTemplate.Triggers> 

        </ControlTemplate> 
       </Setter.Value> 
      </Setter> 
      <Style.Triggers> 
       <Trigger Property="IsFocused" Value="True"> 
        <Setter Property="Background" Value="Transparent" /> 
       </Trigger> 
      </Style.Triggers> 
     </Style> 
+0

앱을 실행하고 'ComboBox'안에있는 버튼을 클릭하면 오류가 발생합니까? – XAMlMAX

+0

아니요, 오류가 없습니다. 이벤트를 캡처하는 가장 좋은 방법은 무엇인지 모릅니다. App.xaml 내부의 ToggleButton에 Click 속성을 추가하면 App.cs에서 click 이벤트를 처리 할 때 문제가 없습니다. 여전히 MainWindow의 컨텍스트에서 이벤트를 처리하려고합니다. – crishushu

답변

0

첫 번째 XAML 블록에는 ToggleButton ComboBox의 하위 항목으로 따라서 모든 항목을이 방법으로 반복 할 수 있습니다.

foreach(ToggleButton btn in someComboBox.Items) 
{ 
    btn.Click += event_handler; 
} 

하지만 정확히 필요한 것을 이해하지 못합니다! :-)

+0

당신의 제안을 시도하고 'btn'에 'InvalidCastException'이 있습니다. 또한 'someComboBox'객체의 멤버를 탐색 할 때 ToggleButton을 발견 할 수 없습니다. – crishushu