我在xaml中設置了ComboBox
並設置了ItemsSource
綁定。當我運行該項目時,ComboBox
中沒有任何顯示。如果我用snoop檢查它,則ComboBox
的ItemsSource
爲空。wpf ComboBox上的ItemsSource被重置?
以前有人遇到過這個嗎?
我查了綁定錯誤,這是錯誤則顯示
System.Windows.Data Error: 39 : BindingExpression path error: 'WasteTypeData' property not found on 'object' ''JobItems' (HashCode=28494546)'. BindingExpression:Path=WasteTypeData; DataItem='JobItems' (HashCode=28494546); target element is 'ComboBox' (Name='CboWasteTypes'); target property is 'ItemsSource' (type 'IEnumerable')
WasteTypeData
是ObservableCollection<WasteTypes>
公共財產。
這就是我已經設置爲ComboBox
的綁定,如果我調試應用程序WasteTypeData
是按預期填充WasteTypes
的列表。
我不明白爲什麼它尋找WasteTypeData
對象JobItems
。對象JobItems
上找不到WasteTypeData
屬性。
JobItemsData
是ObservableCollection<JobItems>
的公共財產。
我的xaml有一個ListBox
及其ItemsSource
綁定設置爲JobItemsData
。
該ListBox
有一個DataTemplate
與幾個TextBox
es和一個ComboBox
。所有的TextBox
ES都正確顯示其數據。
這裏的XAML,如果這將有助於闡明這是怎麼回事任何光線:
<UserControl
x:Class="WorkItems.View.ViewJobItems"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:JobItemsViewModel="clr-namespace:WorkItems.ViewModel"
Height="300" Width="500">
<ListBox
x:Name="LstJobItems"
ItemsSource="{Binding JobItemsData}"
VerticalAlignment="Stretch"
HorizontalAlignment="Stretch">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<StackPanel
Grid.Column="0"
Margin="5">
<StackPanel
Orientation="Horizontal"
Margin="0,5,0,0">
<Label
Content="Customer Details"
FontWeight="Bold"
FontSize="24"></Label>
</StackPanel>
<StackPanel
Orientation="Horizontal">
<Line
StrokeThickness="3"></Line>
</StackPanel>
<StackPanel
Orientation="Horizontal"
Margin="0,5,0,0">
<Label
Content="Customer: "
FontWeight="Bold"
Width="110" />
<TextBox
Text="{Binding Customer, Mode=OneWay}"
Width="200" />
</StackPanel>
<StackPanel
Orientation="Horizontal"
Margin="0,5,0,0">
<Label
Content="Address: "
FontWeight="Bold"
Width="110" />
<TextBox
Text="{Binding Address1, Mode=OneWay}"
Width="200" />
</StackPanel>
<StackPanel
Grid.Column="1"
Margin="5">
<StackPanel
Orientation="Horizontal"
Margin="0,5,0,0">
<Label
Content="Job Details"
FontWeight="Bold"
FontSize="24"></Label>
</StackPanel>
<StackPanel
Orientation="Horizontal">
<Line
StrokeThickness="3"></Line>
</StackPanel>
<StackPanel
Orientation="Horizontal"
Margin="0,5,0,0">
<Label
Content="Date: "
FontWeight="Bold"
Width="110" />
<TextBox
Text="{Binding JobDate, Mode=OneWay}"
Width="200" />
</StackPanel>
<StackPanel
Orientation="Horizontal"
Margin="0,5,0,0">
<Label
Content="Waste Type: "
FontWeight="Bold"
Width="110" />
<ComboBox
x:Name="CboWasteTypes"
IsEditable="False"
ItemsSource="{Binding Path=WasteTypeData}"
DisplayMemberPath="WasteType"
SelectedValuePath="WasteTypeID"
SelectedValue="{Binding WasteTypeID}"
Width="200" />
</StackPanel>
<StackPanel
Orientation="Horizontal"
Margin="0,5,0,0">
<Label
Content="Status: "
FontWeight="Bold"
Width="110" />
<TextBox
Text="{Binding Status, Mode=OneWay}"
Width="200" />
</StackPanel>
</StackPanel>
</StackPanel>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</UserControl>
感謝 保羅
是的,我試過那個帕特里克,但它似乎無法看到可觀察集合'WasteTypeData',即使它是JobItemsViewModel中的公共屬性。 xmlns:local =「clr-namespace:WorkItems.ViewModel」> JobItemsViewModel.cs位於名爲ViewModel的文件夾中。 未找到類型'local:WasteTypeData'。確認您沒有遺漏裝配參考,並且所有參考裝配都已建成 謝謝 Paul – Paul 2010-01-24 12:44:55