0
我很难解决这个错误,我收到我的绑定对象的子属性。数据绑定错误40与子属性在4.0 WPF
输出日志:
System.Windows.Data Error: 40 :
BindingExpression path error:
'ModuleTitle' property not found on 'object' ''MainViewModel' (HashCode=20054924)'.
BindingExpression:Path=ModuleTitle;
DataItem='MainViewModel' (HashCode=20054924);
target element is 'Module' (Name='');
target property is 'Content' (type 'Object')
System.Windows.Data Error: 40 :
BindingExpression path error:
'HexColorValue' property not found on 'object' ''MainViewModel' (HashCode=20054924)'.
BindingExpression:Path=HexColorValue;
DataItem='MainViewModel' (HashCode=20054924);
target element is 'Module' (Name='');
target property is 'Background' (type 'Brush')
System.Windows.Data Error: 40 :
BindingExpression path error:
'ImageSource' property not found on 'object' ''MainViewModel' (HashCode=20054924)'.
BindingExpression:Path=ImageSource;
DataItem='MainViewModel' (HashCode=20054924);
target element is 'Module' (Name='');
target property is 'TileGlyph' (type 'ImageSource')
我的视图:
<UserControl definition ...>
<UserControl.DataContext>
<ViewModels:MainViewModel/>
</UserControl.DataContext>
...
<dxnav:TileBar x:Name="MainNavigation_TileBar" ScrollViewer.HorizontalScrollBarVisibility="Auto" ItemSpacing="3" ItemsSource="{Binding NavModules}">
<dxnav:TileBar.ItemContainerStyle>
<Style TargetType="dxnav:TileBarItem">
<Setter Property="Content" Value="{Binding ModuleTitle}"/>
<Setter Property="Width" Value="166"/>
<Setter Property="Background" Value="{Binding HexColorValue}"/>
<Setter Property="TileGlyph" Value="{Binding ImageSource}"/>
</Style>
</dxnav:TileBar.ItemContainerStyle>
...
视图模型:
public partial class MainViewModel
{
public ObservableCollection<Module> NavModules { get { return Module.AvailableNavModules; } }
public MainViewModel()
{
}
}
模块类(属性被设置私人在构造函数):
public class Module : TileBarItem
{
public string ModuleTitle { get; private set; }
public string ImageSource { get; private set; }
public string HexColorValue { get; private set; }
public string ModuleGroup { get; private set; }
public string ModuleView { get; private set; }
public static readonly ObservableCollection<Module> AvailableNavModules = new ObservableCollection<Module>()
{
new Module("Search", "/Resources/Icons/Modules/Search.png", ModuleColor.AQUA, _RECORD_GROUP, "SearchPageView"),
new Module("Record Keeping", "/Resources/Icons/Modules/RecordKeeping.png", ModuleColor.BLACK, _RECORD_GROUP, "RecordKeepingView"),
new Module("Scheduling", "/Resources/Icons/Modules/Scheduling.png", ModuleColor.ORANGE, _SCHEDULING_GROUP, "SchedulingView"),
new Module("Management", "/Resources/Icons/Modules/Management.png", ModuleColor.BLUE, _MANAGEMENT_GROUP, "ManagementView")
};
所有的属性都使用简单的{ get; set; }
实现来设置,就像我在项目中的其他位置所做的那样。这只是不适合这种特定的用途。当我加载程序时,它正确地加载了对象NavModules
,但没有一个子属性被设置为显示。
此前,我忘记了将{ get; set; }
添加到ObservableCollection<Module> NavModules
,但现在我无法找到它的子属性。
对此提出建议?
为什么视图模型类模块从视图类TileBarItem派生?这似乎是错误的。这就是说,你确定绑定错误真的来自你所展示的XAML吗? ItemsContainerStyle中的'DataItem'应该是'Module'类型,而不是MainViewModel。 – Clemens 2015-03-02 15:03:32
当它没有从'TileBarItem'派生时,我收到一个错误,指出'TileBar'只接受'TileBarItem'。 我将'ItemsContainerStyle'的数据上下文设置为'Module',看看是否有效。 – Mordil 2015-03-02 17:09:51
这听起来像你直接将项目添加到TileBar,而不是将它们添加到NavModules集合。我承认我不知道TileBar控件,但是当它是派生的ItemsControl时,绑定的ItemsSource集合将不必包含TileBarItem(或派生)对象。 – Clemens 2015-03-02 17:27:25