- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想做一个可搜索的ComboBox
,搜索框为 TextBox
出现在 ItemsPanel
上方当ComboBox
下拉菜单展开。我想我需要制作一个自定义控件来实现搜索功能,但首先我只是想获得 TextBox
使用普通 ComboBox
显示.这是我当前的尝试,当我尝试展开下拉列表时会产生异常:
<Style x:Key="FilteredComboBox" TargetType="ComboBox">
<Setter Property="ItemsPanel">
<Setter.Value>
<ItemsPanelTemplate>
<StackPanel>
<TextBox/>
<StackPanel IsItemsHost="True"
Orientation="Horizontal"
VerticalAlignment="Center"
HorizontalAlignment="Center"/>
</StackPanel>
</ItemsPanelTemplate>
</Setter.Value>
</Setter>
</Style>
Cannot explicitly modify Children collection of Panel used as ItemsPanel for ItemsControl. ItemsControl generates child elements for Panel.
最佳答案
虽然我对此的回答都是正确的,但我认为他们中的任何一个都没有真正回答我的问题,足以被标记为解决方案。我最终做的是从 ComboBox 派生一个自定义控件(右键单击设计器中的 ComboBox 并选择 Edit Template -> Edit a Copy,这会为我复制的 ComboBox 模板生成一大堆 XAML 代码到我制作的自定义控件项目的 Generic.xaml 文件)。然后我编辑了生成的 XAML 代码的 Popup 部分,在 ItemsPresenter 上方添加了一个 WatermarkTextBox(来自扩展的 WPF 工具包),如下所示:
<Popup x:Name="PART_Popup" AllowsTransparency="true" Grid.ColumnSpan="2" IsOpen="{Binding IsDropDownOpen, RelativeSource={RelativeSource TemplatedParent}}" Margin="1" PopupAnimation="{DynamicResource {x:Static SystemParameters.ComboBoxPopupAnimationKey}}" Placement="Bottom">
<Themes:SystemDropShadowChrome x:Name="Shdw" Color="Transparent" MaxHeight="{TemplateBinding MaxDropDownHeight}" MinWidth="{Binding ActualWidth, ElementName=MainGrid}">
<Border x:Name="DropDownBorder" BorderBrush="{DynamicResource {x:Static SystemColors.WindowFrameBrushKey}}" BorderThickness="1" Background="{DynamicResource {x:Static SystemColors.WindowBrushKey}}">
<ScrollViewer x:Name="DropDownScrollViewer">
<Grid RenderOptions.ClearTypeHint="Enabled">
<Canvas HorizontalAlignment="Left" Height="0" VerticalAlignment="Top" Width="0">
<Rectangle x:Name="OpaqueRect" Fill="{Binding Background, ElementName=DropDownBorder}" Height="{Binding ActualHeight, ElementName=DropDownBorder}" Width="{Binding ActualWidth, ElementName=DropDownBorder}"/>
</Canvas>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition/>
</Grid.RowDefinitions>
<xtk:WatermarkTextBox Grid.Row="0"
Visibility="{Binding IsDropDownOpen, RelativeSource={RelativeSource TemplatedParent}, Converter={StaticResource booleanToVisibilityConverter}}"
Watermark="Type here to filter..."
Text="{Binding SearchFilter, RelativeSource={RelativeSource TemplatedParent}, UpdateSourceTrigger=PropertyChanged}">
</xtk:WatermarkTextBox>
<ItemsPresenter x:Name="ItemsPresenter" Grid.Row="1" KeyboardNavigation.DirectionalNavigation="Contained" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
</Grid>
</Grid>
</ScrollViewer>
</Border>
</Themes:SystemDropShadowChrome>
</Popup>
public class SearchableComboBox : ComboBox
{
public const string SearchFilterPropertyName = "SearchFilter";
public readonly static DependencyProperty SearchFilterProperty;
public string SearchFilter
{
get { return (string)GetValue(SearchFilterProperty); }
set { SetValue(SearchFilterProperty, value); }
}
static SearchableComboBox()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(SearchableComboBox), new FrameworkPropertyMetadata(typeof(SearchableComboBox)));
SearchFilterProperty = DependencyProperty.Register(SearchFilterPropertyName, typeof(string), typeof(SearchableComboBox),
new PropertyMetadata(string.Empty, new PropertyChangedCallback(SearchFilter_PropertyChanged)));
}
private static void SearchFilter_PropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
((SearchableComboBox)d).RefreshFilter();
}
private void RefreshFilter()
{
if (this.ItemsSource != null)
{
ICollectionView view = CollectionViewSource.GetDefaultView(this.ItemsSource);
view.Refresh();
}
}
private bool FilterPredicate(object value)
{
if (value == null)
return false;
if (string.IsNullOrEmpty(SearchFilter))
return true;
return value.ToString().Contains(SearchFilter, StringComparison.CurrentCultureIgnoreCase);
}
protected override void OnItemsSourceChanged(IEnumerable oldValue, IEnumerable newValue)
{
if (newValue != null)
{
ICollectionView view = CollectionViewSource.GetDefaultView(newValue);
view.Filter += this.FilterPredicate;
}
if (oldValue != null)
{
ICollectionView view = CollectionViewSource.GetDefaultView(oldValue);
view.Filter -= this.FilterPredicate;
}
base.OnItemsSourceChanged(oldValue, newValue);
}
关于wpf - 在 ComboBox 的 ItemsPanel 上方添加其他控件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15096507/
我正在尝试将 ListBox 中的 ItemsPanel 换成 WrapPanel,但样式上的 ItemsPanelTemplate 似乎没有效果。找到并应用了样式,因为边框和背景颜色发生了变化,但使
我有一个带有 ItemsPanel 的列表框 我想使用隐藏代码中的 TranslateTransform 沿 X
我正在尝试将项目(图像)添加到列表框,最好使用 WrapPanel 类型布局。很简单。 我遇到的问题是,当我定义 ListBox.ItemsPanel 模板以使用包装面板时,加载图像所花费的时间变得难
我想做一个可搜索的ComboBox ,搜索框为 TextBox出现在 ItemsPanel 上方当ComboBox下拉菜单展开。我想我需要制作一个自定义控件来实现搜索功能,但首先我只是想获得 Text
我正在寻找一种方法来制作我的 TreeView在 Canvas 中显示项目控制板。
我正在使用列表框和包装面板来显示数据。 例如:
这是对 this 的后续问题, 已回答。 使用代码: 您可以创建一个列表,其中的项目填充 ListBox在从左到右的列中,并且特定列中的最后一个项目永
我有一个自定义控件 ListBox,我想根据其 ItemsPanel 的大小调整其大小。对于项目面板,我有一个自定义控件 WrapPanel 可以相应地排列项目。正如您在屏幕截图中所见,ListBox
我在 ItemsControl ItemsPanelTemplate 中定义了一个网格,其中一个 RowDefinitions 定义了一个 x:Name(因此我可以为行大小设置动画)。
是否有可能做这样的事情:
我有 Windows Phone 7 ListBox绑定(bind)到整数列表。我使用的是默认的 MVVM Light 模板,所以有一个 ViewModel包含数据和简单 RelayCommand 的
我有一个 ItemsControl,我已经构建了我在其中显示的项目 (views:DisplayGroupView),它们将水平扩展到显示所有内容而不是垂直显示(仅使用可用高度) 我已经将 Items
我有一个 ItemsControl,我将 ItemsPanel 设置为 Canvas。 Canvas 需要能够根据我放入的内容动态调整大小,并且我需要能够在内容超出控件边界时滚动。问题是我无法让内容滚
对于动态绑定(bind)到 ItemsControl 的列表,我使用 Grid 作为 ItemsPanel。下面的代码正在运行 - 还有一个问题:我找不到动态初始化网格的 ColumnDefiniti
使用带有 WrapPanel 设置为 ItemsPanel 的 ItemsControl,我试图实现此图像中所示的内容: XAML 看起来像这样(经过修改以使其更简单):
我正在尝试使用 Silverlight 3 在 ItemsControl DataTemplate 中设置 Canvas 属性。根据 this post ,这样做的唯一方法是使用 ContentPre
有人知道来自 Windows Phone 8 SDK(不是 WP 7.5 的 Windows Phone 工具包)的 Longlistselector 控件是否具有 ItemsPanel 属性或等效属
我正在使用 ItemsControl,其中 ItemsPanel 设置为 Canvas(有关更多背景信息,请参阅 this 问题)。ItemsControl 正在按我想要的方式执行,并且通过将子元素放
我是一名优秀的程序员,十分优秀!