1
我想在Silverlight中實現此功能。自定義Silverlight組合框的控制模板
的組合框在線過濾器,在彈出的
http://gregandora.wordpress.com/2012/01/25/filtering-items-in-a-wpf-combobox/
不幸的是它是WPF和XAML是不兼容的。轉換它或瞭解如何更改組合框的控件模板非常困難。
有什麼想法?
我想在Silverlight中實現此功能。自定義Silverlight組合框的控制模板
的組合框在線過濾器,在彈出的
http://gregandora.wordpress.com/2012/01/25/filtering-items-in-a-wpf-combobox/
不幸的是它是WPF和XAML是不兼容的。轉換它或瞭解如何更改組合框的控件模板非常困難。
有什麼想法?
這裏是解決方案的演示:https://dl.dropbox.com/u/8424800/StackOverflowSl.html(見組合框篩選)
我把默認的Silverlight組合框模板,並增加了一個「FilterTextBox」的彈出部分。因爲它超出了StackOverflow的限制,所以我無法發佈整個xaml。全部來源是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);
}
}