WPF 自定义控件数据绑定

发布于 2024-10-26 00:05:20 字数 1556 浏览 0 评论 0原文

我是 WPF 中自定义控件开发的新手,但我尝试开发一个单独的控件以在我正在开发的应用程序中使用。该控件是一个自动完成文本框。在此控件中,我有一个 DependencyProprety ,其中包含可能的条目列表,以便人们在输入文本时可以进行选择

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();
            }
        }

此视图模型具有带有值的属性Articles,但当我时,控件的ItemsSource属性为空用户输入一些文本后尝试在列表中搜索。 创建控件时是否遗漏了任何特殊步骤,因此使用 mvvm 模式。

我希望以一种可以理解的方式解释这个问题。欢迎任何帮助/提示。

I'm new to the development of custom controls in WPF, but I tried to develop a single one to use in a application that I'm developing. This control is an autocomplete textbox. In this control, I have a DependencyProprety that has a list of possible entries so a person can choose from while entering the text

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");
            }
        }

I use this control in a usercontrol and associate this control to a property in the viewmodel

<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>

I have a viewmodel that I assign on the usercontrol load to the datacontext of the usercontrol load

protected virtual void Window_Loaded(object sender, RoutedEventArgs e)
        {
            if (!DesignerProperties.GetIsInDesignMode(this))
            {
                this.DataContext = viewModel;
                SetLabels();
            }
        }

This viewmodel has the property Articles with values but the ItemsSource property of the control is null when I try to search in the list after the user enter some text.
Is there any special step that I missed when I create the control so use the mvvm pattern.

I hope that the explain the problem in a understandable way. Any help/hints would be welcome.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

盛夏尉蓝 2024-11-02 00:05:20

这里有两个问题:

首先,您的依赖属性将此属性的“默认”值定义为 null。您可以通过更改元数据来指定新集合来更改这一点:

public static readonly DependencyProperty ItemsSourceProperty = DependencyProperty.Register("ItemsSource",typeof (IList<object>),typeof (AutoCompleteTextBox),
     new PropertyMetadata(new List<object>));

其次,使用依赖项属性时,setter 不能包含任何逻辑。您应该将属性设置为:

   public IList<object> ItemsSource
    {
        get { return (IList<object>) GetValue(ItemsSourceProperty); }
        set { SetValue(ItemsSourceProperty, value); }
    }

这是因为绑定系统实际上不会调用 setter - 仅当您使用代码时。但是,由于该类是 DependencyObject 并且这是一个 DP,因此您不需要引发属性更改事件。

There are two issues here:

First, you're dependency property is defining the "default" value for this property to be null. You can change that by changing the metadata to specify a new collection:

public static readonly DependencyProperty ItemsSourceProperty = DependencyProperty.Register("ItemsSource",typeof (IList<object>),typeof (AutoCompleteTextBox),
     new PropertyMetadata(new List<object>));

Secondly, when using dependency properties, the setter can't contain any logic. You should keep your property set as:

   public IList<object> ItemsSource
    {
        get { return (IList<object>) GetValue(ItemsSourceProperty); }
        set { SetValue(ItemsSourceProperty, value); }
    }

This is because the setter doesn't actually get called by the binding system - only when you use code. However, since the class is a DependencyObject and this is a DP, you don't need to raise property changed events.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文