2010-01-21 32 views
1

我在xaml中設置了ComboBox並設置了ItemsSource綁定。當我運行該項目時,ComboBox中沒有任何顯示。如果我用snoop檢查它,則ComboBoxItemsSource爲空。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')

WasteTypeDataObservableCollection<WasteTypes>公共財產。

這就是我已經設置爲ComboBox的綁定,如果我調試應用程序WasteTypeData是按預期填充WasteTypes的列表。

我不明白爲什麼它尋找WasteTypeData對象JobItems。對象JobItems上找不到WasteTypeData屬性。

JobItemsDataObservableCollection<JobItems>的公共財產。

我的xaml有一個ListBox及其ItemsSource綁定設置爲JobItemsData

ListBox有一個DataTemplate與幾個TextBox es和一個ComboBox。所有的TextBoxES都正確顯示其數據。

這裏的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> 

感謝 保羅

回答

1

檢查任何綁定錯誤輸出窗口。您可能拼寫錯誤或未正確設置DataContext。

1

我認爲它失敗了,因爲當您在組合框中使用{Binding Path=WasteTypeData}時,它期望將其作爲JobsItems中的屬性而不是可觀察集合,因爲這是父控件(您的ListBox)綁定的內容。

添加WasteTypeData作爲靜態資源在用戶的控制,那麼你的組合框綁定到,使用"{Binding Source={StaticResource..."

<UserControl 
    ... 
    xmlns:local="WorkItems" 
    ... 
    Height="300" Width="500"> 
<UserControl.Resources> 
    <local:WasteTypeData x:Key="WasteTypeData"/> 
</UserControl.Resources> 
.. 
<ComboBox 
    x:Name="CboWasteTypes" 
    IsEditable="False" 
    ItemsSource="{Binding Source={StaticResource WasteTypeData}}" 
    DisplayMemberPath="WasteType" 
    SelectedValuePath="WasteTypeID" 
    SelectedValue="{Binding WasteTypeID}" 
    Width="200" /> 

看看是否有幫助指定它!

+0

是的,我試過那個帕特里克,但它似乎無法看到可觀察集合'WasteTypeData',即使它是JobItemsViewModel中的公共屬性。 xmlns:local =「clr-namespace:WorkItems.ViewModel」> JobItemsViewModel.cs位於名爲ViewModel的文件夾中。 未找到類型'local:WasteTypeData'。確認您沒有遺漏裝配參考,並且所有參考裝配都已建成 謝謝 Paul – Paul 2010-01-24 12:44:55

相關問題