I'm not sure what you are trying to achieve here, the error should be that the view is not instantiated. You could try to instantiate the view as needed.
I put the code List<string> party = new List<string>();
outside the method and try to add code like below .
void cmb_KeyUp(object sender, KeyEventArgs e)
{
var tb = cmb.Template.FindName("PART_EditableTextBox", cmb) as TextBox;
var val = tb.Text;
var empty = string.IsNullOrEmpty(tb.Text);
var keysToIgnore = new Key[] { Key.Down, Key.Up, Key.Enter, Key.Left, Key.Right };
if (keysToIgnore.Contains(e.Key))
{
return;
}
if (empty)
{
view.Filter = null;
}
else
{
view = CollectionViewSource.GetDefaultView(party);
view.Filter = (i) =>
{
var str = i.ToString();
return str.ToLowerInvariant().Contains(tb.Text.ToLowerInvariant());
};
}
cmb.IsDropDownOpen = true;
tb.Text = val;
//tb.CaretIndex = tb.Text.Length;
}
Update:
<Style
TargetType="{x:Type DataGrid}">
<Setter Property="ColumnWidth" Value="auto"></Setter>
<Setter Property="FontSize" Value="13"></Setter>
</Style>
You could modify your dg_AutoGeneratingColumn method as needed.
void dg_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
{
string headerName = e.Column.Header.ToString();
e.Column = new DataGridTextColumn()
{
Header = e.PropertyName,
SortMemberPath = e.PropertyName, //To allow for sorting on a column
Binding = new Binding("[" + e.PropertyName + "]")
};
if (headerName == "Party")
{
e.Column.Width = 298;
}
//if (headerName == "Bill No.")
//{
//e.Column.Width = 148;
//}
if (headerName == "Bill Date")
{
e.Column.Width = 148;
}
//if (headerName == "Amount")
//{
//e.Column.Width = 148;
//}
if (headerName == "Due Date")
{
e.Column.Width = 248;
}
if (headerName == "Remarks")
{
e.Column.Width = 248;
}
if (headerName == "Payment Released on")
{
e.Column.Width = 288;
}
}
Update 2 : You can add additional properties to Combobox to filter.
<ComboBox IsTextSearchEnabled="True" IsReadOnly="False" IsEditable="True" Margin="10,0" x:Name="cmb" Width="380"
Height="35" HorizontalAlignment="Left" VerticalAlignment="Top" SelectionChanged="cmb_SelectionChanged" KeyUp="cmb_KeyUp"
local:ComboBox.IsFilterOnAutocompleteEnabled="True" />
class ComboBox : DependencyObject
{
#region IsFilterOnAutoCompleteEnabled attached property
public static readonly DependencyProperty IsFilterOnAutocompleteEnabledProperty =
DependencyProperty.RegisterAttached(
"IsFilterOnAutocompleteEnabled",
typeof(bool),
typeof(ComboBox),
new PropertyMetadata(default(bool), ComboBox.OnIsFilterOnAutocompleteEnabledChanged));
public static void SetIsFilterOnAutocompleteEnabled(DependencyObject attachingElement, bool value) =>
attachingElement.SetValue(ComboBox.IsFilterOnAutocompleteEnabledProperty, value);
public static bool GetIsFilterOnAutocompleteEnabled(DependencyObject attachingElement) =>
(bool)attachingElement.GetValue(ComboBox.IsFilterOnAutocompleteEnabledProperty);
#endregion
private static Dictionary<TextBox, System.Windows.Controls.ComboBox> TextBoxComboBoxMap { get; }
private static Dictionary<TextBox, int> TextBoxSelectionStartMap { get; }
private static Dictionary<System.Windows.Controls.ComboBox, TextBox> ComboBoxTextBoxMap { get; }
private static bool IsNavigationKeyPressed { get; set; }
static ComboBox()
{
ComboBox.TextBoxComboBoxMap = new Dictionary<TextBox, System.Windows.Controls.ComboBox>();
ComboBox.TextBoxSelectionStartMap = new Dictionary<TextBox, int>();
ComboBox.ComboBoxTextBoxMap = new Dictionary<System.Windows.Controls.ComboBox, TextBox>();
}
private static void OnIsFilterOnAutocompleteEnabledChanged(
DependencyObject attachingElement,
DependencyPropertyChangedEventArgs e)
{
if (!(attachingElement is System.Windows.Controls.ComboBox comboBox
&& comboBox.IsEditable))
{
return;
}
if (!(bool)e.NewValue)
{
ComboBox.DisableAutocompleteFilter(comboBox);
return;
}
if (!comboBox.IsLoaded)
{
comboBox.Loaded += ComboBox.EnableAutocompleteFilterOnComboBoxLoaded;
return;
}
ComboBox.EnableAutocompleteFilter(comboBox);
}
private static async void FilterOnTextInput(object sender, TextChangedEventArgs e)
{
await Application.Current.Dispatcher.InvokeAsync(
() =>
{
if (ComboBox.IsNavigationKeyPressed)
{
return;
}
var textBox = sender as TextBox;
int textBoxSelectionStart = textBox.SelectionStart;
ComboBox.TextBoxSelectionStartMap[textBox] = textBoxSelectionStart;
string changedTextOnAutocomplete = textBox.Text.Substring(0, textBoxSelectionStart);
if (ComboBox.TextBoxComboBoxMap.TryGetValue(
textBox,
out System.Windows.Controls.ComboBox comboBox))
{
comboBox.Items.Filter = item => item.ToString().StartsWith(
changedTextOnAutocomplete,
StringComparison.OrdinalIgnoreCase);
}
},
DispatcherPriority.Background);
}
private static async void HandleKeyDownWhileFiltering(object sender, KeyEventArgs e)
{
var comboBox = sender as System.Windows.Controls.ComboBox;
if (!ComboBox.ComboBoxTextBoxMap.TryGetValue(comboBox, out TextBox textBox))
{
return;
}
switch (e.Key)
{
case Key.Down
when comboBox.Items.CurrentPosition < comboBox.Items.Count - 1
&& comboBox.Items.MoveCurrentToNext():
case Key.Up
when comboBox.Items.CurrentPosition > 0
&& comboBox.Items.MoveCurrentToPrevious():
{
ComboBox.IsNavigationKeyPressed = true;
await Application.Current.Dispatcher.InvokeAsync(
() =>
{
ComboBox.SelectCurrentItem(textBox, comboBox);
ComboBox.IsNavigationKeyPressed = false;
},
DispatcherPriority.ContextIdle);
break;
}
}
}
private static void SelectCurrentItem(TextBox textBox, System.Windows.Controls.ComboBox comboBox)
{
comboBox.SelectedItem = comboBox.Items.CurrentItem;
if (ComboBox.TextBoxSelectionStartMap.TryGetValue(textBox, out int selectionStart))
{
textBox.SelectionStart = selectionStart;
}
}
private static void EnableAutocompleteFilterOnComboBoxLoaded(object sender, RoutedEventArgs e)
{
var comboBox = sender as System.Windows.Controls.ComboBox;
ComboBox.EnableAutocompleteFilter(comboBox);
}
private static void EnableAutocompleteFilter(System.Windows.Controls.ComboBox comboBox)
{
if (comboBox.TryFindVisualChildElement(out TextBox editTextBox))
{
ComboBox.TextBoxComboBoxMap.Add(editTextBox, comboBox);
ComboBox.ComboBoxTextBoxMap.Add(comboBox, editTextBox);
editTextBox.TextChanged += ComboBox.FilterOnTextInput;
comboBox.AddHandler(UIElement.PreviewKeyDownEvent, new KeyEventHandler(HandleKeyDownWhileFiltering), true);
}
}
private static void DisableAutocompleteFilter(System.Windows.Controls.ComboBox comboBox)
{
if (comboBox.TryFindVisualChildElement(out TextBox editTextBox))
{
ComboBox.TextBoxComboBoxMap.Remove(editTextBox);
editTextBox.TextChanged -= ComboBox.FilterOnTextInput;
}
}
}
public static class Extensions
{
public static bool TryFindVisualChildElement<TChild>(this DependencyObject parent, out TChild resultElement)
where TChild : DependencyObject
{
resultElement = null;
if (parent is Popup popup)
{
parent = popup.Child;
if (parent == null)
{
return false;
}
}
for (var childIndex = 0; childIndex < VisualTreeHelper.GetChildrenCount(parent); childIndex++)
{
DependencyObject childElement = VisualTreeHelper.GetChild(parent, childIndex);
if (childElement is TChild child)
{
resultElement = child;
return true;
}
if (childElement.TryFindVisualChildElement(out resultElement))
{
return true;
}
}
return false;
}
}
The result:
If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.