wpf ComboBox 上的 ItemsSource 正在重置?

发布于 2024-08-18 21:04:57 字数 6975 浏览 3 评论 0原文

我在 xaml 中有一个 ComboBox 设置,并设置了 ItemsSource 绑定。当我运行该项目时,ComboBox 中没有显示任何内容。如果我用 snoop 检查它,ComboBoxItemsSource 是空白的。

有人以前遇到过这个吗?

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

System.Windows.Data 错误:39:BindingExpression 路径错误:在“对象”“JobItems”(HashCode=28494546) 上找不到“WasteTypeData”属性。 BindingExpression:路径=WasteTypeData; DataItem='JobItems' (HashCode=28494546);目标元素是“ComboBox”(名称=“CboWasteTypes”);目标属性是“ItemsSource”(类型“IEnumerable”)

WasteTypeDataObservableCollection 的公共属性。

这就是我设置为 ComboBox 的绑定,如果我调试应用程序,WasteTypeData 将按预期填充 WasteTypes 列表。

我不明白为什么它要在对象 JobItems 上查找 WasteTypeData。在对象 JobItems 上找不到 WasteTypeData 属性。

JobItemsDataObservableCollection 的公共属性。

我的 xaml 有一个 ListBox,其 ItemsSource 绑定设置为 JobItemsData

ListBox 有一个 DataTemplate,其中包含几个 TextBox 和一个 ComboBox。所有TextBox都正确显示其数据。

这是 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>

谢谢 保罗

I have a ComboBox setup in xaml and have set the ItemsSource binding. When I run the project nothing shows up in the ComboBox. If I inspect it with snoop the ItemsSource of the ComboBox is blank.

Anyone come across this before?

I checked the binding errors this is the error it displays

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 is a public property of ObservableCollection<WasteTypes>.

This is what I have set as the binding of the ComboBox and if I debug the app WasteTypeData is populated with the list of WasteTypes as expected.

I can't figure out why it's looking for WasteTypeData on object JobItems. The WasteTypeData property is not found on the object JobItems.

JobItemsData is a public property of ObservableCollection<JobItems>.

My xaml has a ListBox with its ItemsSource Binding set to JobItemsData.

The ListBox has a DataTemplate with a couple of TextBoxes and one ComboBox. All the TextBoxes display their data properly.

Here's xaml if it will help shed any light on what's going on:

<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>

Thanks
Paul

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

奈何桥上唱咆哮 2024-08-25 21:04:57

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

Check the Output window for any binding errors. You may have misspelled something or not set the DataContext correctly.

氛圍 2024-08-25 21:04:57

我认为它失败是因为当您在组合框中使用 {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" />

看看这是否有帮助!

I think its failing because when you use {Binding Path=WasteTypeData} in your combobox, it expects to find it as a property in JobsItems instead of the observable collection, since that is what the parent control (your ListBox) is bound to.

Add WasteTypeData as a static resource in your user control, then bind your combobox to that, specifying it using "{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" />

See if that helps!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文