I have a custom control LookupPanelView
which consists of a TextBox
and a ListBox
. It has an attached property ItemsSource
which the ListBox binds to so the bound data can be set from outside the control.
public partial class LookupPanelView : UserControl
{
public static readonly DependencyProperty ItemsSourceProperty =
DependencyProperty.Register("ItemsSource", typeof(IEnumerable), typeof(LookupPanelView));
public IEnumerable ItemsSource
{
get => (IEnumerable)GetValue(ItemsSourceProperty);
set => SetValue(ItemsSourceProperty, value);
}
public LookupPanelView()
{
InitializeComponent();
}
}
The control's ItemsSource
is bound to a property in my main ViewModel which decides what data to display.
public class MainViewModel : ViewModelBase
{
public ObservableCollection<DomainObject> LookupPanelItems { get; private set; }
public MainViewModel()
{
LookupPanelItems = // Fetch the data to display in the control.
}
}
<Window x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
UseLayoutRounding="True">
<Grid>
<lookupPanelView:LookupPanelView Grid.Column="0" ItemsSource="{Binding LookupPanelItems}"/>
</Grid>
I would like to extend the custom control to have search functionality where you type in the TextBox
and it selects a matching item from the ListBox
. This logic should be contained in the control since it should be aware of how to search it's own items. I think I need to give the control it's own ViewModel to hold the logic but then how do I access the attached property ItemsSource
in the ViewModel to search the items? I would like to avoid using code-behind as much as possible for maintainability and testability.