【问题标题】:Customize Control Template of Silverlight Combobox自定义 Silverlight Combobox 的控件模板
【发布时间】:2012-09-20 13:07:47
【问题描述】:

我想在 Silverlight 中实现它。

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

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

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

有什么想法吗?

【问题讨论】:

    标签: silverlight autocomplete controltemplate


    【解决方案1】:

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

    我采用了默认的 Silverlight ComboBox 模板,并在 Popup 部分添加了一个“FilterTextBox”。我无法发布整个 xaml,因为它超出了 StackOverflow 的限制。完整来源是here as a 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);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-05-18
      • 2011-01-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多