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