Shortly, I'm new to grid and WPF and for some reason I cannot see my content within my ListView and I'm pretty much sure that Grid causes it.
不久,我是网格和WPF的新手,由于某种原因,我无法在ListView中看到我的内容,而且我非常确定Grid会导致它。
<Grid Canvas.Left="223" Canvas.Top="228">
<Grid.RowDefinitions>
<RowDefinition Height="9*"/>
<RowDefinition Height="28*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="8*"/>
<ColumnDefinition Width="21*"/>
<ColumnDefinition Width="29*"/>
<ColumnDefinition Width="29*"/>
</Grid.ColumnDefinitions>
<ListView HorizontalAlignment="Left" Height="123" VerticalAlignment="Top" Width="589" Name="LibraryList" Margin="-188,-76,-141.001,-10" IsEnabled="False" Grid.ColumnSpan="4" Grid.RowSpan="2" >
<ListView.View>
<GridView>
<GridViewColumn Header="Item Name" DisplayMemberBinding="{Binding ItemName}" Width="70"/>
<GridViewColumn Header="Copy Number" DisplayMemberBinding="{Binding CopyNumber}" Width="85"/>
<GridViewColumn Header="Guid" DisplayMemberBinding="{Binding Guid}" Width="100"/>
<GridViewColumn Header="Print Date" DisplayMemberBinding="{Binding Date}" Width="90"/>
<GridViewColumn Header="Best Seller" DisplayMemberBinding="{Binding BestSeller}" Width="90"/>
<GridViewColumn Header="Category" DisplayMemberBinding="{Binding Category}" Width="70"/>
<GridViewColumn Header="Sub Category" DisplayMemberBinding="{Binding SubCategory}" Width="80"/>
</GridView>
</ListView.View>
</ListView>
</Grid>
The implementation is:
实施是:
ManageCtors.InitJournal(int.Parse(copyNumber.Text), itemName.Text, DateTime.Parse(TimePrinted.Text), int.Parse(Guid.Text), (JournalCategory)Enum.Parse(typeof(JournalCategory), SubCategory.Text));
//Add to ListView
string[] row = { itemName.Text, copyNumber.Text, Guid.Text , TimePrinted .Text, Category.Text , SubCategory.Text = "None" };
ListViewItem _listViewItem = new ListViewItem();
_listViewItem.Content = row;
LibraryList.Items.Add(_listViewItem.Content);
Any Idea?
BTW: Sometimes I get a message: input was not in the correct format.
任何想法?顺便说一句:有时我收到一条消息:输入格式不正确。
1 个解决方案
#1
0
The data you are binding to is an array of strings, but in each of the GridViewColumn's you are binding by name. This is probably the problem.
您绑定的数据是一个字符串数组,但在每个GridViewColumn中,您都按名称绑定。这可能是个问题。
You would be best to model the data row out to its own class and set an instance of the class to the ListViewItem.Content property.
您最好将数据行建模到其自己的类,并将该类的实例设置为ListViewItem.Content属性。
public class ItemData
{
public string ItemName { get; set; }
public int CopyNumber { get; set; }
public Guid Guid { get; set; }
public DateTime Date { get; set; }
public string BestSeller { get; set; }
public string Category { get; set; }
public string Subcategory { get; set; }
}
ItemData row = new ItemData {
// set each property value
};
ListViewItem _listViewItem = new ListViewItem();
_listViewItem.Content = row;
LibraryList.Items.Add(_listViewItem.Content); // not sure if you need content here
I think you would also be better off binding the ItemsSource on the ListView rather than adding items directly on to the ListView Items collection.
我认为你最好还是在ListView上绑定ItemsSource,而不是直接在ListView Items集合上添加项目。
ObservableCollection<ItemData> items = new ObservableCollection<ItemData>();
// add your ItemData instances to the items collection
LibraryList.ItemsSource = items;
You would also be better off following the MVVM pattern, of which there is a lot of advice on * and elsewhere to get you started.
您最好还是遵循MVVM模式,其中有很多关于*和其他地方的建议可以帮助您入门。
#1
0
The data you are binding to is an array of strings, but in each of the GridViewColumn's you are binding by name. This is probably the problem.
您绑定的数据是一个字符串数组,但在每个GridViewColumn中,您都按名称绑定。这可能是个问题。
You would be best to model the data row out to its own class and set an instance of the class to the ListViewItem.Content property.
您最好将数据行建模到其自己的类,并将该类的实例设置为ListViewItem.Content属性。
public class ItemData
{
public string ItemName { get; set; }
public int CopyNumber { get; set; }
public Guid Guid { get; set; }
public DateTime Date { get; set; }
public string BestSeller { get; set; }
public string Category { get; set; }
public string Subcategory { get; set; }
}
ItemData row = new ItemData {
// set each property value
};
ListViewItem _listViewItem = new ListViewItem();
_listViewItem.Content = row;
LibraryList.Items.Add(_listViewItem.Content); // not sure if you need content here
I think you would also be better off binding the ItemsSource on the ListView rather than adding items directly on to the ListView Items collection.
我认为你最好还是在ListView上绑定ItemsSource,而不是直接在ListView Items集合上添加项目。
ObservableCollection<ItemData> items = new ObservableCollection<ItemData>();
// add your ItemData instances to the items collection
LibraryList.ItemsSource = items;
You would also be better off following the MVVM pattern, of which there is a lot of advice on * and elsewhere to get you started.
您最好还是遵循MVVM模式,其中有很多关于*和其他地方的建议可以帮助您入门。