2010-01-21 129 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

相关问题