2016-04-14 17 views
0

DataGridPopUpとするカスタムComboBoxを作成しようとしています。私は、標準Templateを上書きしますが、私はItemsHostDataGridIsItemsHost="True")に設定していたとき、私はComboBoxとDataGrid PopUp

<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="White" 
        BorderThickness="1" 
        BorderBrush="LightGray"/> 
        <ScrollViewer Margin="1,2" SnapsToDevicePixels="True"> 
          <DataGrid IsItemsHost="True" KeyboardNavigation.DirectionalNavigation="Contained" /> 
        </ScrollViewer> 
     </Grid> 
</Popup> 

がどのように私はこれを実装することができます(予想通り)The member "IsItemsHost" is not recognized or is not accesibleを取得しますか?

進捗状況:Microsoftによると

あなたはコンボボックス用のControlTemplateを作成すると、テンプレートがScrollViewerの内ItemsPresenterが含まれている場合があります。 (ItemsPresenterはコンボボックスの各項目を表示し、ScrollViewerはコントロール内でのスクロールを有効にします)。 ItemsPresenterがScrollViewerの直接の子でない場合は、ItemsPresenterにItemsPresenterという名前を付ける必要があります。

(私は答えとして、それを追加します完全なソリューションを持っている場合)だから、この作品:

<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="White" 
       BorderThickness="1" 
       BorderBrush="LightGray"/> 
      <ScrollViewer Margin="1,2" SnapsToDevicePixels="True"> 
        <StackPanel Orientation="Vertical" > 
         <DataGrid x:Name="ItemsPresenter" ItemsSource="{TemplateBinding ItemsSource}" KeyboardNavigation.DirectionalNavigation="Contained"></DataGrid> 
         <Border Height="10" Background="Red" /> 
        </StackPanel> 
      </ScrollViewer> 
     </Grid> 
</Popup> 
+0

どこで、どのようにこのポップアップは/現在/表示され示されることになるのでしょうか? – AnjumSKhan

+0

これは、上書きされた 'ComboBox''Templates'の' Popup'部分です。私がそれを知っているなら、私は完全な 'Template'を追加することができます –

答えて

0

私は私が(私の目的は、ページングのためにコンボボックスを作成することです)スタイル怒鳴るで欲しかったものを手に入れるの管理:

 <Style x:Key="DataGridComboBoxStyle" TargetType="{x:Type ComboBox}"> 
     <Setter Property="Foreground" Value="DimGray" /> 
     <Setter Property="BorderBrush" Value="LightGray" /> 
     <Setter Property="Background" Value="White" /> 
     <Setter Property="SnapsToDevicePixels" Value="True"/> 
     <Setter Property="OverridesDefaultStyle" Value="True"/> 
     <Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Auto"/> 
     <Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto"/> 
     <Setter Property="ScrollViewer.CanContentScroll" Value="true"/> 
     <Setter Property="FontSize" Value="12" /> 
     <Setter Property="FontFamily" Value="Tahoma, Verdana" /> 
     <Setter Property="Height" Value="25" /> 
     <Setter Property="HorizontalContentAlignment" Value="Left" /> 
     <Setter Property="MaxDropDownHeight" Value="250" /> 
     <Setter Property="StaysOpenOnEdit" Value="True" /> 
     <Setter Property="Template"> 
      <Setter.Value> 
       <ControlTemplate TargetType="ComboBox"> 
        <Grid> 
         <ToggleButton Name="ToggleButton" 
             BorderBrush="{TemplateBinding BorderBrush}" 
             Background="{TemplateBinding Background}" 
             Foreground="{TemplateBinding Foreground}" 
             Style="{StaticResource ComboBoxToggleButton}" 
             Grid.Column="2" 
             Focusable="false" 
             IsChecked="{Binding Path=IsDropDownOpen,Mode=TwoWay,RelativeSource={RelativeSource TemplatedParent}}" 
             ClickMode="Press"> 
         </ToggleButton> 
         <ContentPresenter Name="ContentSite" 
              IsHitTestVisible="False" 
              Content="{Binding ElementName=ItemsPresenter, Path=SelectedItem}" 
              ContentTemplate="{TemplateBinding SelectionBoxItemTemplate}" 
              ContentTemplateSelector="{TemplateBinding ItemTemplateSelector}" 
              Margin="10,3,30,3" 
              VerticalAlignment="Center" 
              HorizontalAlignment="Center" /> 
         <TextBox x:Name="PART_EditableTextBox" 
           Style="{x:Null}" 
           SelectionBrush="Transparent" 
           Template="{StaticResource ComboBoxTextBox}" 
           Text="{Binding ElementName=ItemsPresenter, Path=SelectedItem}" 
           Foreground="{TemplateBinding Foreground}" 
           FontFamily="{TemplateBinding FontFamily}" 
           FontSize="{TemplateBinding FontSize}" 
           FontWeight="{TemplateBinding FontWeight}" 
           HorizontalAlignment="Stretch" 
           HorizontalContentAlignment="Left" 
           VerticalAlignment="Stretch" 
           VerticalContentAlignment="Center" 
           Background="Transparent" 
           Margin="3,3,23,3" 
           Focusable="True"        
           Visibility="Hidden" 
           IsReadOnly="{TemplateBinding IsReadOnly}"/> 
         <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="White" 
             BorderThickness="1" 
             BorderBrush="LightGray"/> 
           <ScrollViewer Margin="1,2" SnapsToDevicePixels="True" VerticalScrollBarVisibility="Disabled" HorizontalScrollBarVisibility="Disabled"> 
            <StackPanel Orientation="Vertical" > 
             <DataGrid x:Name="ItemsPresenter" 
                IsReadOnly="True" 
                ScrollViewer.CanContentScroll="True" 
                ScrollViewer.HorizontalScrollBarVisibility="Auto" 
                ScrollViewer.VerticalScrollBarVisibility="Auto" 
                ItemsSource="{TemplateBinding ItemsSource}" 
                BorderBrush="Transparent" 
                RowHeaderWidth="0" 
                RowHeight="20" 
                ColumnWidth="*" 
                HeadersVisibility="None" 
                GridLinesVisibility="None" 
                KeyboardNavigation.DirectionalNavigation="Contained"> 
             </DataGrid> 
             <Border BorderBrush="LightGray" BorderThickness="0, 1, 0, 0"> 
              <DockPanel LastChildFill="True" Background="White" Margin="5"> 
               <Button Style="{StaticResource DataGridPagingButtonStyle}" DockPanel.Dock="Left" > 
                <Button.Content> 
                 <UniformGrid Margin="1"> 
                  <Path Height="20" Width="20" Stroke="DarkGray" Stretch="Uniform" StrokeThickness="1" Data="M 4,1 L 1,4 L 4,7" /> 
                 </UniformGrid> 
                </Button.Content> 
               </Button> 
               <Button Style="{StaticResource DataGridPagingButtonStyle}" DockPanel.Dock="Right"> 
                <Button.Content> 
                 <UniformGrid Margin="1"> 
                  <Path Height="20" Width="20" Stroke="DarkGray" Stretch="Uniform" StrokeThickness="1" Data="M1,1 L4,4 1,7" /> 
                 </UniformGrid> 
                </Button.Content> 
               </Button> 
               <StackPanel Orientation="Horizontal" HorizontalAlignment="Center" DockPanel.Dock="Left"> 
                <TextBlock FontFamily="Tahoma, Verdana" FontSize="11" Foreground="DimGray" VerticalAlignment="Center" Padding="0, 0, 5, 2" Text="Page" /> 
                <TextBox BorderBrush="LightGray" FontFamily="Tahoma, Verdana" FontSize="11" Foreground="DimGray" Height="20" Width="20" HorizontalContentAlignment="Center" Text="1" /> 
                <TextBlock FontFamily="Tahoma, Verdana" FontSize="11" Foreground="DimGray" VerticalAlignment="Center" Padding="5, 0, 5, 2" Text="to" /> 
                <TextBlock FontFamily="Tahoma, Verdana" FontSize="11" Foreground="DimGray" VerticalAlignment="Center" Padding="0, 0, 0, 2" Text="(bind pages)" /> 
               </StackPanel> 
              </DockPanel> 
             </Border> 
            </StackPanel> 
           </ScrollViewer> 
          </Grid> 
         </Popup> 
        </Grid> 
        <ControlTemplate.Triggers> 
         <!-- Triggers --> 
        </ControlTemplate.Triggers> 
       </ControlTemplate> 
      </Setter.Value> 
     </Setter> 
    </Style> 
0

代わりのDataGridPanel(StackPanelの/グリッドなど)を使用します。

IsItemsHostプロパティはPanelのメンバーであり、attached propertyではないため、DataGridと一緒に使用することはできません。あなたの項目がDataGridに示すことにする方法があれば

Panel.IsItemsHost Property

は今、あなたはComboBoxItemsコレクションにあなたのDataGridポインティングのItemsSourceを使用する必要があります。

あなたが任意の外部のDataSourceなしComboBoxで直接ComboBoxItemを使用している場合は、コードの下に動作します:

<DataGrid ItemsSource="{Binding Items, RelativeSource={RelativeSource TemplatedParent}}"> 
    <DataGrid.Columns> 
     <DataGridTemplateColumn> 
      <DataGridTemplateColumn.CellTemplate> 
       <DataTemplate> 
        <ComboBoxItem Content="{Binding Content}"/> 
       </DataTemplate> 
      </DataGridTemplateColumn.CellTemplate> 
     </DataGridTemplateColumn> 
    </DataGrid.Columns> 
</DataGrid> 
をそして、あなたは、いくつかの外部のデータソースを使用している場合は、 {Binding someproperty}{Binding Content}を交換してください。

私はあなたのコードとまったく同じ動作サンプルを作って、うまく動作します。

+0

私はそれを知っています。標準の 'Template'は' StackPanel'を使います。問題は、 'DataGrid'の中に' ComboBoxItem'sを入れたいのです。 –

+0

あなたの助けをありがとう。私はこのアプローチを見つけました。私は解決策を求めていました。この唯一の欠点は、再利用できないということです。私はこれをスタイルの中に実装することはできません。私はあなたの答えを受け入れるだろう何かを思い付くなら、より良い解決策を探し続けるつもりです。あなたの努力のためにもう一度ありがとう。 –

+0

@Απόπατοςはスタイルで実装できないと言ったのですか?'ControlTemplate'をリソースとして宣言し、' ComboBox'でそれを使用します。 – AnjumSKhan