1

私はComboBoxxamlでセットアップを行い、ItemsSourceバインディングを設定しました。プロジェクトを実行すると、ComboBox. スヌープで調べると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れます。

WasteTypeDataon objectを探している理由がわかりませんJobItemsWasteTypeDataプロパティがオブジェクトに見つかりませJobItemsん。

JobItemsDataのパブリック プロパティですObservableCollection<JobItems>

私の xaml にはListBoxBindingItemsSourceが に設定された がありJobItemsDataます。

にはListBoxDataTemplateいくつかのTextBoxes と 1 つの があり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

{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 に答える
1

出力ウィンドウでバインディング エラーを確認します。スペルが間違っているか、DataContext が正しく設定されていない可能性があります。

于 2010-01-21T17:06:54.300 に答える