1

我想在 Silverlight 中实现这一点。

弹出框中带有内联过滤器的组合框

http://gregandora.wordpress.com/2012/01/25/filtering-items-in-a-wpf-combobox/

不幸的是,它适用于 WPF,并且 XAML 不兼容。很难转换或理解如何更改组合框的控件模板。

任何想法?

4

1 回答 1

1

这是解决方案的演示:https ://dl.dropbox.com/u/8424800/StackOverflowSl.html (请参阅组合框过滤器)

我采用了默认的 Silverlight ComboBox 模板,并在 Popup 部分添加了一个“FilterTextBox”。我无法发布整个 xaml,因为它超出了 StackOverflow 的限制。完整的源代码在此处作为 GitHub Gist。我已经离开了重要的部分。接下来,需要连接 TextBox 上的事件处理程序。

<Style TargetType="ComboBox">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="ComboBox">
                <Grid>                       
                    <Popup x:Name="Popup">
                        <Border x:Name="PopupBorder"
                                Height="Auto"
                                HorizontalAlignment="Stretch"
                                BorderBrush="{TemplateBinding BorderBrush}"
                                BorderThickness="{TemplateBinding BorderThickness}"
                                CornerRadius="3">
                            <Border.Background>
                                <LinearGradientBrush StartPoint="0.5,0" EndPoint="0.5,1">
                                    <GradientStop Offset="0" Color="#FFFFFFFF" />
                                    <GradientStop Offset="1" Color="#FFFEFEFE" />
                                </LinearGradientBrush>
                            </Border.Background>
                            <Grid>
                                <TextBox x:Name="FilterTextBox"
                                            Height="22"
                                            VerticalAlignment="Top" />
                                <ScrollViewer x:Name="ScrollViewer"
                                                Margin="0,25,0,0"
                                                BorderThickness="0"
                                                Padding="1">
                                    <ItemsPresenter />
                                </ScrollViewer>
                            </Grid>
                        </Border>
                    </Popup>
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

连接文本框

public Q12513294()
{
    // Required to initialize variables
    InitializeComponent();

    InitializeMyCombo(
        Enumerable.Range(1, 99).Select(x => "Beer " + x.ToString() + " on the wall"),
        (object item, string filter) => (item as String).Contains(filter)
    );
}

private void InitializeMyCombo(IEnumerable items, Func<object, string, bool> filter)
{
    MyComboBox.Loaded += (s, e) =>
    {
        // PagedCollectionView implements a filterable collection
        PagedCollectionView list = new PagedCollectionView(items);
        MyComboBox.ItemsSource = list;

        // Set the filter based on the contents of the textbox
        TextBox filterTextBox = MyComboBox.GetTemplateChild<TextBox>("FilterTextBox");
        list.Filter = new Predicate<object>(
            item => filter(item, filterTextBox.Text)
            );

        // Refresh the filter each time
        filterTextBox.TextChanged += (s2, e2) =>
        {
            list.Refresh();
            filterTextBox.Focus();
        };
    };

}

public static class Helper
{
    public static T GetTemplateChild<T>(this DependencyObject parent, string partName)
    {
        return (T)(VisualTreeHelper.GetChild(parent, 0) as Panel).FindName(partName);
    }
}
于 2012-09-21T02:03:54.997 回答