2009-08-07 20 views
3

WPFのツリービュー内でポップアップを使用すると、ポップアップ内のコントロールが使用できなくなる問題が発生しています。たとえば、次のコードを使用すると、ポップアップ内のToggleButtonは一度のみトグルされ、トグルバックできなくなります。この問題を回避する方法はありますか?WPFのツリービューとポップアップコントロールの問題

<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> 
    <Grid> 
     <TreeView> 
      <TreeViewItem> 
       <TreeViewItem.Header> 
        <StackPanel> 
         <ToggleButton> 
          Button outside popup 
         </ToggleButton> 
         <Popup IsOpen="True"> 
          <ToggleButton> 
           Button inside popup 
          </ToggleButton> 
         </Popup> 
        </StackPanel> 
       </TreeViewItem.Header> 
      </TreeViewItem> 
     </TreeView> 
    </Grid> 
</Page> 

答えて

3

問題は、あなたがツリービューアイテム内のポップアップを埋め込むことで、何とかそれは、その組み込み機能に干渉です。とにかく、ポップアップはそんなに使われるつもりはないと思う。ポップアップのPlacementTargetを指定できるため、どこにでも宣言して、必要な場所で開くことができます。次のマークアップでは、これをあなたの例で示しています。

<StackPanel> 
    <Popup IsOpen="True" PlacementTarget="{Binding ElementName=buttonPanel}" 
     Placement="Bottom"> 
    <ToggleButton> 
     Button inside popup 
    </ToggleButton> 
    </Popup> 
    <TreeView> 
    <TreeViewItem> 
     <TreeViewItem.Header> 
     <StackPanel Name="buttonPanel"> 
      <ToggleButton> 
      Button outside popup 
      </ToggleButton> 
     </StackPanel> 
     </TreeViewItem.Header> 
    </TreeViewItem> 
    </TreeView> 
</StackPanel> 

ご覧のとおり、ポップアップはどこにでも宣言できます。 PlacementTargetを使用して、必要な場所に配置することができます。この変更により、ToggleButtonは期待どおりに動作します。また、falseにツリービューでFocusableの設定

+0

私は、単純な場合には、ここで提示し、このソリューションが動作することを同意するが、私の実際のsenarioのために私が定義するHierarchicalDataTemplateでユーザーコントロールを使用していますTreeViewItemヘッダーとこのユーザーコントロールにはポップアップがあり、ここで紹介した単純なシナリオと同じ動作をしています。このため、PlacementTagetを使用して、TreeViewの外でPopupを宣言することは、私のためには機能しません。 – dlannoye

+0

そして、それをTreeViewItem.Headerに配置する必要がありますか? UserControl.Resourcesやそれ以外の場所に置くことはできませんか? – Charlie

+0

これはうまくいくかもしれませんが、それは理想から遠いです。 TreeViewItem内でホストしたいコントロールはComboBoxと似ていますが、ComboBoxを使って作成することはできず、ポップアップを使ってコンボボックスのドロップダウン部分のように使用しています。このため、私は他のTreeViewItemコンテンツを持つ単一のユーザーコントロールでポップアップを保持したいと思います。私はそれがポップアップを使用していることがわかり、それは問題なしでTreeViewItemで使用することができるので、この問題を回避する方法があるはずです。 – dlannoye

1

使用してTreeViewコントロールの問題を発見チャーリーのおかげで...

が動作しているようです。しかし、他の問題を引き起こす可能性があります。私は問題を伝えることができるものから、

<TreeView Focusable="False"> 

は焦点を取っているかを制御であるので、あなたのツリービューではなく、ボタンのマウスクリックの焦点を取っているように見えます。

+1

提案していただきありがとうございますが、TreeViewをフォーカスできないように設定すると、キーボードを使用してナビゲートすることができなくなります。 – dlannoye

1

WPFチームと議論したところ、これはWPFの単なる問題であることが判明しました。

私の場合、各TreeViewItemに使用されるドロップダイアログを作成しようとしていました。私は必要な見た目を得るためにさまざまなコントロールを使って遊びました。ComboBoxのPopup部分がTreeViewの内部で正常に機能することがわかりました。

このため、私はTreeViewの内部でComboBoxを使い、常に選択されている項目が1つしかなく、その項目がドロップダイアログになるようにしました。この後、私はちょうど私が必要とする外観に合うようにComboBoxをスタイルする必要がありました。下のコードは、私がしたことの一般的な考え方を示しています。

<TreeView> 
    <TreeViewItem> 
     <ComboBox HorizontalAlignment="Left" VerticalAlignment="Top"> 
      <ComboBox.Resources> 
       <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Transparent"/> 
      </ComboBox.Resources> 
      <ComboBoxItem IsSelected="True"> 
       <StackPanel Orientation="Vertical"> 
        <Label>Some stuff on the combobox popup</Label> 
        <RadioButton>Radio Button 1</RadioButton> 
        <RadioButton>Radio Button 2</RadioButton> 
        <CheckBox>Check Box</CheckBox> 
        <Button HorizontalAlignment="Right">Ok</Button> 
       </StackPanel> 
      </ComboBoxItem> 
     </ComboBox> 
    </TreeViewItem> 
</TreeView> 
0

さて私は、ツリービューでデータテンプレートと、複数のユーザーコントロールを使用して、階層の項目を表示し、ユーザーコントロールの一つがその中にポップアップでトグルボタンで同じ問題を抱えていました。

これは完全にフリーズし、まったく応答しません。この問題はFocusプロパティに関連していますが、Focusableをfalseに設定しても機能しない場合があります。

上記のソリューションを見ると、ツリービューアイテムの中でコンボボックスを使用している最後のものが気に入っています。しかしそれはもう一つの問題につながる。ユーザーがコンボボックスの(単一の)項目を選択すると、それはコンボボックスにselectedValueとして表示され、ポップアップでトグルのように動作したいだけです。

また、コンボボックスの三角形を省略記号に変更したいと考えました。だから私はコントロールテンプレートを変更してこの解決策を試みました。ここで

は私のソリューションです:

... ...

<Style x:Key="ComboBoxStyle1" TargetType="{x:Type ComboBox}"> 
     <Setter Property="FocusVisualStyle" Value="{StaticResource ComboBoxFocusVisual}"/> 
     <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.WindowTextBrushKey}}"/> 
     <Setter Property="Background" Value="{StaticResource ButtonNormalBackground}"/> 
     <Setter Property="BorderBrush" Value="{StaticResource ButtonNormalBorder}"/> 
     <Setter Property="BorderThickness" Value="1"/> 
     <Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Auto"/> 
     <Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto"/> 
     <Setter Property="Padding" Value="4,3"/> 
     <Setter Property="Template"> 
      <Setter.Value> 
       <ControlTemplate TargetType="{x:Type ComboBox}"> 
        <Grid x:Name="MainGrid" SnapsToDevicePixels="true"> 
         <Grid.ColumnDefinitions> 
          <ColumnDefinition Width="*"/> 
         </Grid.ColumnDefinitions> 
         <Popup x:Name="PART_Popup" Margin="1" 
           AllowsTransparency="true" 
           IsOpen="{Binding IsDropDownOpen, RelativeSource={RelativeSource TemplatedParent}}" 
           Placement="Bottom" 
           PopupAnimation="Fade"> 
          <Microsoft_Windows_Themes:SystemDropShadowChrome x:Name="Shdw" MaxHeight="{TemplateBinding MaxDropDownHeight}" MinWidth="{Binding ActualWidth, ElementName=MainGrid}" Color="Transparent"> 
           <Border x:Name="DropDownBorder" Background="{DynamicResource {x:Static SystemColors.WindowBrushKey}}" BorderBrush="{DynamicResource {x:Static SystemColors.WindowFrameBrushKey}}" BorderThickness="2" CornerRadius="0,4,4,4"> 
            <ScrollViewer CanContentScroll="true"> 
             <ItemsPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" KeyboardNavigation.DirectionalNavigation="Contained"/> 
            </ScrollViewer> 
           </Border> 
          </Microsoft_Windows_Themes:SystemDropShadowChrome> 
         </Popup> 
         <ToggleButton Style="{StaticResource ComboBoxReadonlyToggleButton}" 
             Background="{TemplateBinding Background}" 
             BorderBrush="{TemplateBinding BorderBrush}" 
             IsChecked="{Binding IsDropDownOpen, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}"/> 
        </Grid> 
        <ControlTemplate.Triggers> 
         <Trigger Property="HasDropShadow" SourceName="PART_Popup" Value="true"> 
          <Setter Property="Margin" TargetName="Shdw" Value="0,0,5,5"/> 
          <Setter Property="Color" TargetName="Shdw" Value="#71000000"/> 
         </Trigger> 
         <Trigger Property="HasItems" Value="false"> 
          <Setter Property="Height" TargetName="DropDownBorder" Value="95"/> 
         </Trigger> 
         <Trigger Property="IsEnabled" Value="false"> 
          <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/> 
          <Setter Property="Background" Value="#FFF4F4F4"/> 
         </Trigger> 
         <Trigger Property="IsGrouping" Value="true"> 
          <Setter Property="ScrollViewer.CanContentScroll" Value="false"/> 
         </Trigger> 
        </ControlTemplate.Triggers> 
       </ControlTemplate> 
      </Setter.Value> 
     </Setter> 
     <Style.Triggers> 
      <Trigger Property="IsEditable" Value="true"> 
       <Setter Property="BorderBrush" Value="{StaticResource TextBoxBorder}"/> 
       <Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.WindowBrushKey}}"/> 
       <Setter Property="IsTabStop" Value="false"/> 
       <Setter Property="Padding" Value="3"/> 
      </Trigger> 
     </Style.Triggers> 
    </Style> 
    <Style x:Key="ComboBoxItemStyle1" TargetType="{x:Type ComboBoxItem}"> 
     <Setter Property="HorizontalContentAlignment" Value="{Binding HorizontalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/> 
     <Setter Property="VerticalContentAlignment" Value="{Binding VerticalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/> 
     <Setter Property="Padding" Value="3,0,3,0"/> 
     <Setter Property="Background" Value="Transparent"/> 
     <Setter Property="Template"> 
      <Setter.Value> 
       <ControlTemplate TargetType="{x:Type ComboBoxItem}"> 
        <Border x:Name="Bd" SnapsToDevicePixels="true" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Padding="{TemplateBinding Padding}"> 
         <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/> 
        </Border> 
        <ControlTemplate.Triggers> 
         <Trigger Property="IsHighlighted" Value="true"> 
          <Setter Property="Background" TargetName="Bd" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/> 
          <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.HighlightTextBrushKey}}"/> 
         </Trigger> 
         <Trigger Property="IsEnabled" Value="false"> 
          <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/> 
         </Trigger> 
        </ControlTemplate.Triggers> 
       </ControlTemplate> 
      </Setter.Value> 
     </Setter> 
    </Style> 
</Window.Resources> 
<TreeView > 
    <TreeViewItem IsExpanded="True"> 
     <ComboBox HorizontalAlignment="Left" VerticalAlignment="Top" 
        IsEditable="False" 
        Style="{StaticResource ComboBoxStyle1}"> 
      <ComboBox.Resources> 
       <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Transparent"/> 
      </ComboBox.Resources> 
      <ComboBoxItem IsSelected="False"> 
       <StackPanel Orientation="Vertical"> 
        <Label>Some stuff on the combobox popup</Label> 
        <RadioButton>Radio Button 1</RadioButton> 
        <RadioButton>Radio Button 2</RadioButton> 
        <CheckBox>Check Box</CheckBox> 
        <Button HorizontalAlignment="Right">Ok</Button> 
       </StackPanel> 
      </ComboBoxItem> 
     </ComboBox> 
    </TreeViewItem> 
</TreeView>