带有空白列表框项的c#和wpf危机
我放弃了我的代码错误,所以我决定写在这里。我真的希望能找到解决办法。我想要的是:当我单击按钮时,它将从数据库中检索数据,然后将其加载到列表框中。它工作得很好。但当我添加wpf样式时,它开始出现问题,因为我想将图像添加到文本-图像(右侧)和文本(左侧)旁边的每个项目中。列表框中的结果为空,但实际上似乎有一个数据列表-请看图片。我可能在代码或wpf中做错了什么。我不确定是什么问题。。。。如果你能看看我的代码,我将不胜感激。您给出的代码将非常有用。非常感谢 WPF:带有空白列表框项的c#和wpf危机,c#,wpf,listbox,listboxitems,C#,Wpf,Listbox,Listboxitems,我放弃了我的代码错误,所以我决定写在这里。我真的希望能找到解决办法。我想要的是:当我单击按钮时,它将从数据库中检索数据,然后将其加载到列表框中。它工作得很好。但当我添加wpf样式时,它开始出现问题,因为我想将图像添加到文本-图像(右侧)和文本(左侧)旁边的每个项目中。列表框中的结果为空,但实际上似乎有一个数据列表-请看图片。我可能在代码或wpf中做错了什么。我不确定是什么问题。。。。如果你能看看我的代码,我将不胜感激。您给出的代码将非常有用。非常感谢 WPF: WPF样式: <Dat
WPF样式:
<DataTemplate x:Key="templateListBoxItem">
<Grid Margin="5">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<Border Grid.Column="0"
Grid.Row="0"
Grid.RowSpan="2"
Margin="0,0,10,0">
<Image Source="{Binding Path=Image}"
Stretch="Fill"
Height="40"
Width="40"></Image>
</Border>
<TextBlock Text="{Binding Path=Name}"
FontWeight="Bold"
Grid.Column="1"
Grid.Row="0"></TextBlock>
<TextBlock Text="{Binding Path=About}"
Grid.Column="1"
Grid.Row="1"></TextBlock>
</Grid>
</DataTemplate>
<Style x:Key="styleListBox" TargetType="{x:Type ListBox}">
<Setter Property="ItemTemplate" Value="{StaticResource ResourceKey=templateListBoxItem}"></Setter>
</Style>
C#:
private void按钮\u单击(对象发送者,路由目标)
{
_dinnerExtractor=新的dinnerExtractor();
const int oneDay=1;
_databaseDinnerList=新的ObservableCollection(_dinnerExtractor.GetDinnerDays(一天));
if(_databaseDinnerList!=null)
{
foreach(数据库列表中的变量列表)
{
添加(新的食物信息{晚餐=列表.晚餐,晚餐图像=列表.晚餐图像});
}
//lstDinner.ItemsSource=\u databaseDinnerList;
}
}
食品信息
类不包含属性:图像
和名称
(您正在尝试在数据模板
中绑定这些属性)
从代码隐藏中,我们可以使用属性Dinner
和DinnerImage
创建foodinfo
类的定义:
class FoodInformation
{
public string Dinner { get; set; }
public ImageSource DinnerImage { get; set; }
}
因此,您应该绑定到属性晚餐
和晚餐图像
,而不是图像
和名称
如果在DataTemplate
中更改相应的属性名称,一切都会正常
<DataTemplate x:Key="templateListBoxItem">
<Grid Margin="5">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<Border Grid.Column="0"
Grid.Row="0"
Grid.RowSpan="2"
Margin="0,0,10,0">
<Image Source="{Binding Path=DinnerImage }"
Stretch="Fill"
Height="40"
Width="40"></Image>
</Border>
<TextBlock Text="{Binding Path=Dinner }"
FontWeight="Bold"
Grid.Column="1"
Grid.Row="0"></TextBlock>
</Grid>
</DataTemplate>
食品信息
类不包含属性:图像
和名称
(您正在尝试在数据模板
中绑定这些属性)
从代码隐藏中,我们可以使用属性Dinner
和DinnerImage
创建foodinfo
类的定义:
class FoodInformation
{
public string Dinner { get; set; }
public ImageSource DinnerImage { get; set; }
}
因此,您应该绑定到属性晚餐
和晚餐图像
,而不是图像
和名称
如果在DataTemplate
中更改相应的属性名称,一切都会正常
<DataTemplate x:Key="templateListBoxItem">
<Grid Margin="5">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<Border Grid.Column="0"
Grid.Row="0"
Grid.RowSpan="2"
Margin="0,0,10,0">
<Image Source="{Binding Path=DinnerImage }"
Stretch="Fill"
Height="40"
Width="40"></Image>
</Border>
<TextBlock Text="{Binding Path=Dinner }"
FontWeight="Bold"
Grid.Column="1"
Grid.Row="0"></TextBlock>
</Grid>
</DataTemplate>
我使用数据库中的二进制图像并将其转换为图像。我没有使用图像文件路径。如何解决这个问题?@user1358072关于这个问题,你们可以在这里找到答案:我使用数据库中的二进制图像并将其转换为图像。我没有使用图像文件路径。如何解决这个问题?@user1358072关于这个问题,你可以在这里找到答案:@kmatyaszek在下面的答案中是正确的。作为将来的参考,如果您在运行程序时观察输出窗口,您应该会看到在本例中出现绑定错误,这应该是您的问题所在的线索。当某些东西在我的视图中显示不正确时,我总是先看那里!:)@kmatyaszek在下面的回答中是正确的。作为将来的参考,如果您在运行程序时观察输出窗口,您应该会看到在本例中出现绑定错误,这应该是您的问题所在的线索。当某些东西在我的视图中显示不正确时,我总是先看那里!:)