How to specify hard-coded data for a multi-column WPF ListView?
Hey, I have a ListView with mutliple columns. I wish to attach hard-coded values using a xaml construct like ListViewItem but can not work out how to specifiy multiple columns of fixed data. For example, in the grid shown below I'd like to see two columns, 'Animal' and 'IQ', and pre-populate the data for both columns, but the code shown sets all column开发者_JAVA百科s to the same value.
<ListView>
<ListView.View>
<GridView>
<GridViewColumn Header="Animal" />
<GridViewColumn Header="IQ" />
</GridView>
</ListView.View>
<ListViewItem Content="Pig"/>
<ListViewItem Content="Dog"/>
</ListView>
BTW: This is for a simple, sample screen. As such, I do not really want to bind programatically to data.
Thanks in advance.
Dave.Not sure how to accomplish this declaratively in XAML, but if you don't want to bind data to the control, you could add programatically the items on the code-behind.
this.AnimalsList.Items.Add(new ListViewItem { Content = "Pig" });
this.AnimalsList.Items.Add(new ListViewItem { Content = "Dog" });
Just in case someone is still looking for an answer; the first answer is incorrect and will still print the same value in each column, just like the XAML example. This will do the trick:
<ListView>
<ListView.View>
<GridView>
<GridViewColumn Header="Animal" DisplayMemberBinding="{Binding Animal}" />
<GridViewColumn Header="IQ" DisplayMemberBinding="{Binding IQ}" />
</GridView>
</ListView.View>
<ListViewItem>
<local:Animals Animal="Pig" IQ="70" />
<local:Animals Animal="Dog" IQ="60" />
<local:Animals Animal="Frog" IQ="30" />
</ListViewItem>
However you do need a class like this, I don't think it can be done with pure XAML:
public class Animals
{
public string Animal { get; set; }
public int IQ { get; set; }
}
精彩评论