我正在尝试创建ComboBox
作为DataGrid
的自定义PopUp
。我覆盖了标准Template
,但当我将ItemsHost
设置为DataGrid
(IsItemsHost="True"
)时,我得到The member "IsItemsHost" is not recognized or is not accesible
(正如预期的那样)
<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>
我该如何实现?
进度:
根据Microsoft:
为ComboBox创建ControlTemplate时,您的模板可能在ScrollViewer中包含ItemsPresenter。 (ItemsPresenter显示ComboBox中的每个项目; 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 :(得分:0)
使用DataGrid
(StackPanel / Grid等)代替Panel
。
由于IsItemsHost
属性是Panel的成员,并且它不是attached property
,因此您无法将其与DataGrid
一起使用。
现在,如果您希望自己的项目显示在DataGrid
中,那么您必须使用ItemsSource
DataGrid
指向Items
的{{1}}集合。 ComboBox
。
如果您在ComboBoxItem
中直接使用ComboBox
而没有任何外部DataSource
,则下面的代码将有效:
<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 Content}
替换为{Binding someproperty}
。
我完全按照您的代码制作了一个工作样本,并且工作正常。
答案 1 :(得分:0)
我设法得到了我想要的风格(我的目的是为分页创建一个ComboBox):
<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>