1

ComboBox在 xaml 中有一个设置并设置了ItemsSource绑定。当我运行项目时,ComboBox. 如果我用 snoop 检查它,则它ItemsSourceComboBox空白的。

有人遇到过这个吗?

我检查了绑定错误这是它显示的错误

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我不知道为什么它在 object上寻找JobItemsWasteTypeData在对象上找不到该属性JobItems

JobItemsData是 的公共财产ObservableCollection<JobItems>

我的 xaml 有一个ListBoxBindingItemsSource设置为JobItemsData.

ListBoxaDataTemplate和几个TextBoxes 和一个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>

谢谢保罗

4

2 回答 2

1

检查输出窗口是否有任何绑定错误。您可能拼写错误或未正确设置 DataContext。

于 2010-01-21T17:06:54.300 回答
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" />

看看有没有帮助!

于 2010-01-22T19:19:21.710 回答