.net 4.0 WPF中带有子属性的数据绑定错误40
我很难用绑定对象的子属性来排除这个错误 输出日志:.net 4.0 WPF中带有子属性的数据绑定错误40,.net,wpf,c#-4.0,data-binding,devexpress,.net,Wpf,C# 4.0,Data Binding,Devexpress,我很难用绑定对象的子属性来排除这个错误 输出日志: System.Windows.Data Error: 40 : BindingExpression path error: 'ModuleTitle' property not found on 'object' ''MainViewModel' (HashCode=20054924)'. BindingExpression:Path=ModuleTitle; DataItem='MainViewModel' (HashCode=2005
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部分类MainViewModel
{
公共ObservableCollection NavModules{get{return Module.AvailableNavModules;}}
公共主视图模型()
{
}
}
模块类(属性在构造函数中私自设置):
公共类模块:TileBarItem
{
公共字符串ModuleTitle{get;private set;}
公共字符串ImageSource{get;private set;}
公共字符串HexColorValue{get;private set;}
公共字符串模块组{get;private set;}
公共字符串ModuleView{get;private set;}
public static readonly observetecollection AvailableNavModules=new observetecollection()
{
新模块(“Search”、“/Resources/Icons/Modules/Search.png”、ModuleColor.AQUA、_RECORD_GROUP、“SearchPageView”),
新模块(“Record Keeping”、“/Resources/Icons/Modules/RecordKeeping.png”、ModuleColor.BLACK、_Record_GROUP、“RecordKeepingView”),
新模块(“Scheduling”、“/Resources/Icons/Modules/Scheduling.png”、ModuleColor.ORANGE、_Scheduling_GROUP、“SchedulingView”),
新模块(“管理”、“/Resources/Icons/Modules/Management.png”、ModuleColor.BLUE、“管理”组、“管理视图”)
};
所有属性都是使用简单的{get;set;}
实现设置的,就像我在项目中的其他地方所做的那样。它只是不适用于此特定用途。当我加载程序时,它会正确加载对象NavModules
,但没有为显示设置任何子属性
以前,我忘记将{get;set;}
添加到ObservableCollection NavModules
,但现在我无法让它找到子属性
建议?问题是我有
模块
派生自TileBarItem
,因此路径没有按预期工作
新类别声明:
public class Module
{
public string ModuleTitle { get; private set; }
public string ImageSource { get; private set; }
....
}
为什么视图模型类模块是从视图类TileBarItem派生的?这似乎是错误的。也就是说,您确定绑定错误确实来自您正在显示的XAML吗?ItemsContainerStyle中的
DataItem
应该是Module
类型,而不是MainViewModel。当它不是从TileBarItem
派生时收到一个错误,说明TileBar
仅接受TileBarItem
。我将把ItemsContainerStyle
的数据上下文设置为Module
,看看是否有效。这听起来像是直接将项目添加到TileBar,而不是将它们添加到NavModules集合。我承认我不知道TileBar控件,但当它是派生项控件时,绑定的ItemsSource集合不必包含TileBarItem(或派生)对象。感谢指向TileBarItem
deriving的指针。我删除了它,一切正常。
public class Module
{
public string ModuleTitle { get; private set; }
public string ImageSource { get; private set; }
....
}