简体   繁体   English

WPF 绑定到用户控件的属性

[英]WPF Binding to a property of an usercontrol

I have an usercontrol, which have a property Results .我有一个用户控件,它有一个属性Results This usercontrol should show this ObservableCollection .此用户控件应显示此ObservableCollection I think the XAML-Code of the usercontrol doesn't matter.我认为用户控件的 XAML 代码无关紧要。 The Code-Behind look like that:代码隐藏看起来像这样:

Public Property Results() As ObservableCollection(Of ResultModel)
    Get
        Return GetValue(ResultsProperty)
    End Get

    Set(ByVal value As ObservableCollection(Of ResultModel))
        SetValue(ResultsProperty, value)
    End Set
End Property

Public Shared ReadOnly ResultsProperty As DependencyProperty = _
                       DependencyProperty.Register("Results", _
                       GetType(ObservableCollection(Of ResultModel)), GetType(ResourcesGridData), _
                       New FrameworkPropertyMetadata(Nothing))

In my MainView.xaml I have the following XAML-Code to show the usercontrol:在我的 MainView.xaml 我有以下 XAML 代码来显示用户控件:

<controls:ResourcesGridData Results="{Binding Path=ResultsToShow}" />

I want to bind the ResultsToShow property of the MainViewModel to the property of the usercontrol.我想将 MainViewModel 的ResultsToShow属性绑定到用户控件的属性。

But now I get the following error:但现在我收到以下错误:

System.Windows.Data.BindingExpression ist kein Wert des Typs ASSESS.Data.ResultModel und kann in dieser generischen Auflistung nicht verwendet werden. System.Windows.Data.BindingExpression ist kein Wert des Typs ASSESS.Data.ResultModel und kann in diesergenerischen Auflistung nicht verwendet werden。

I try to translate it:我试着翻译它:

System.Windows.Data.BindingExpression is not a value of the type ASSESS.Data.ResultModel and can not be used in this generic list. System.Windows.Data.BindingExpression 不是 ASSESS.Data.ResultModel 类型的值,不能在此通用列表中使用。

Okay, it's very simple.好的,这很简单。 In my case, I don't need any properties in the usercontrol code-behind.就我而言,我不需要用户控件代码隐藏中的任何属性。

In the MainView I simply write this:在 MainView 我简单地写了这个:

<controls:ResourcesGridData />

and in the usercontrol:在用户控件中:

ItemsSource="{Binding ResultsToShow}"

I directly bind to the Property of the DataContext of the MainView.我直接绑定到 MainView 的 DataContext 的属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM