wpf ComboBox 上的 ItemsSource 正在重置?
我在 xaml 中有一个 ComboBox
设置,并设置了 ItemsSource
绑定。当我运行该项目时,ComboBox
中没有显示任何内容。如果我用 snoop 检查它,ComboBox
的 ItemsSource
是空白的。
有人以前遇到过这个吗?
我检查了绑定错误,这是它显示的错误
System.Windows.Data 错误:39:BindingExpression 路径错误:在“对象”“JobItems”(HashCode=28494546) 上找不到“WasteTypeData”属性。 BindingExpression:路径=WasteTypeData; DataItem='JobItems' (HashCode=28494546);目标元素是“ComboBox”(名称=“CboWasteTypes”);目标属性是“ItemsSource”(类型“IEnumerable”)
WasteTypeData
是 ObservableCollection
的公共属性。
这就是我设置为 ComboBox
的绑定,如果我调试应用程序,WasteTypeData
将按预期填充 WasteTypes
列表。
我不明白为什么它要在对象 JobItems
上查找 WasteTypeData
。在对象 JobItems
上找不到 WasteTypeData
属性。
JobItemsData
是 ObservableCollection
的公共属性。
我的 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 TextBox
es and one ComboBox
. All the TextBox
es 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
检查输出窗口是否有任何绑定错误。您可能拼写错误或未正确设置 DataContext。
Check the Output window for any binding errors. You may have misspelled something or not set the DataContext correctly.
我认为它失败是因为当您在组合框中使用
{Binding Path=WasteTypeData}
时,它希望将其作为 JobsItems 中的属性而不是可观察集合找到,因为这是父控件(您的ListBox) 绑定到。将 WasteTypeData 添加为用户控件中的静态资源,然后将组合框绑定到该资源,并使用
"{Binding Source={StaticResource..."
指定它,看看这是否有帮助!
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..."
See if that helps!