图像图标和字符串不会出现在WPF的Listview中



这是XAMl代码图像图标和字符串未显示在WPF的ListView中怎么了?

不仅图像不会出现,其他文本字符串也不会出现。尝试其他方法表明ItemSource是重叠的。

是否未应用绑定?

<GroupBox Grid.Column="1" Grid.RowSpan="2" Background="Aqua">
<StackPanel Orientation="Vertical" Background="Aquamarine" Margin="5">
<StackPanel.Resources>
<Style x:Key="ListviewStyle" TargetType="{x:Type ListView}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ListView}">
<Border x:Name="corner"
CornerRadius="9"
Background="White"
Height="1000">
<ContentPresenter
VerticalAlignment="Center"
HorizontalAlignment="Center"
/>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</StackPanel.Resources>
<TextBlock Text="I/O Input"
FontSize="30" 
VerticalAlignment="Center" 
HorizontalAlignment="Center" 
Margin="10"/>
<ListView Style="{StaticResource ListviewStyle}" 
x:Name="lv_input">
<ListView.View>
<GridView>
<GridViewColumn Width="50" Header="InputStatus">
<GridViewColumn.CellTemplate>
<DataTemplate>
<Image Source="{Binding InputStatus}"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Width="200" Header="InputName"
DisplayMemberBinding="{Binding InputName}"/>
</GridView>
</ListView.View>
</ListView>
</StackPanel>
</GroupBox>

这是幕后黑手图像图标和字符串未显示在WPF的ListView中怎么了?

不仅图像不会出现,其他文本字符串也不会出现。尝试其他方法表明ItemSource是重叠的。

是否未应用绑定?在此处输入代码

public partial class IOSetPage : Page
{
public ObservableCollection<ListViewItemsData> ListViewItemsCollections { get { return ListViewItemsCollections; } }
ObservableCollection<ListViewItemsData> _ListViewItemsCollections = new ObservableCollection<ListViewItemsData>();
public IOSetPage()
{
InitializeComponent();
ListViewItemsCollections.Add(new ListViewItemsData()
{
InputStatus = "/Image/inputdisconnect.png",
InputName = "ASFWQEFAasdfqerwefc"
});
lv_input.ItemsSource = ListViewItemsCollections;
}
public class ListViewItemsData
{
public string InputStatus { get; set; }
public string InputName { get; set; }
}
}

考虑到您提供的示例,您的C#代码的第3行似乎有一个拼写错误。属性getter应该返回_ListViewItemsCollections,如下所示:

public ObservableCollection<ListViewItemsData> ListViewItemsCollections { get { return _ListViewItemsCollections; } }

此外,我注意到您直接使用ItemsSource,而不是在lv_input ListView上设置DataContext。虽然ItemsSource在修复拼写错误后可能会工作,但在尝试使用项目模板等时可能会遇到意外行为

一种更正确(但不完美(的方式来做你正在尝试的事情,看起来是这样的:

public partial class IOSetPage : Page
{
IOSetPageView view;
public IOSetPage()
{
InitializeComponent();

view = new IOSetPageView();
view.Items.Add(new ListViewItemsData()
{
InputStatus = "/Image/inputdisconnect.png",
InputName = "ASFWQEFAasdfqerwefc"
});
lv_input.DataContext = view;
}
}
public class IOSetPageView
{
public ObservableCollection<ListViewItemsData> Items { get; } 
= new ObservableCollection<ListViewItemsData>();
}
public class ListViewItemsData
{
public string InputStatus { get; set; }
public string InputName { get; set; }
}

ListView的XAML如下所示:

<ListView Style="{StaticResource ListviewStyle}" ItemsSource="{Binding Items}" x:Name="lv_input">
<ListView.View>
<GridView>
<GridViewColumn Width="50" Header="InputStatus">
<GridViewColumn.CellTemplate>
<DataTemplate>
<Image Source="{Binding InputStatus}"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Width="200" Header="InputName"
DisplayMemberBinding="{Binding InputName}"/>
</GridView>
</ListView.View>
</ListView>

请注意ItemSource现在是如何绑定到我创建的视图类的Items属性的。为了进一步改进,您可以为ListViewItemsData类实现INotifyPropertyChanged接口,这将允许ListView在修改属性时实时更新。

最新更新