0
我是WPF中自定义控件开发的新手,但我尝试开发一个在我正在开发的应用程序中使用的控件。此控件是一个自动完成文本框。在这种控制中,我有一个DependencyProprety
有可能的条目列表,这样一个人可以从在输入文本WPF自定义控件数据绑定
public static readonly DependencyProperty ItemsSourceProperty = DependencyProperty.Register("ItemsSource",typeof (IList<object>),typeof (AutoCompleteTextBox),new PropertyMetadata(null));
public IList<object> ItemsSource
{
get { return (IList<object>) GetValue(ItemsSourceProperty); }
set
{
SetValue(ItemsSourceProperty, value);
RaiseOnPropertyChanged("ItemsSource");
}
}
我使用该控件在用户控件和该控件的属性在视图模型
关联选择<CustomControls:AutoCompleteTextBox Height="23" Width="200"
VerticalAlignment="Center" Text="{Binding Path=ArticleName, Mode=TwoWay,
UpdateSourceTrigger=PropertyChanged}" ItemsSource="{Binding Path=Articles,
Mode=OneWay, UpdateSourceTrigger=PropertyChanged}">
</CustomControls:AutoCompleteTextBox>
我有我的用户控件负荷分配到用户控件的加载
protected virtual void Window_Loaded(object sender, RoutedEventArgs e)
{
if (!DesignerProperties.GetIsInDesignMode(this))
{
this.DataContext = viewModel;
SetLabels();
}
}
此视图模型具有的DataContext的一个视图模型属性Articles
与值但该控件的ItemsSource
属性为空,当我尝试在用户输入一些文本后在列表中搜索。 当我创建控件时,是否有任何特殊的步骤让我使用mvvm模式。
我希望以一种可以理解的方式解释问题。任何帮助/提示将受到欢迎。
感谢您的回复,我纠正了两个问题。 – 2011-03-23 17:34:41
现在viewmodel中的属性被调用,但当我调用'wordMatches = ItemsSource.Where(x => x.ToString().ToLower()。StartsWith(Text.ToLower(),StringComparison.InvariantCulture)) .Select x => new KeyValuePair(x.ToString(),x))。ToList();'ItemsSource仍然为null –
2011-03-23 17:36:21