I'm going to display the list of data using listview, the data class contain the following elements:
class MyData
{
static Random rnd = new Random();
public int id { get; set; }
public string name { get; set; }
public bool selected { get; set; }
private LED[] LEDs = new LED[21];
private byte[] servos = new byte[21];
}
And it is expected to display the data in table format as below,
{selected} {id} {name} {servo[1]} {servo[2]} ......
(servo[0] should be hidden)
with the layout of each column:
- {selected] : checkbox
- {id} : right alignment
- {name] : left alignment
- {servo} : right alignment, with background based on LED setting
I have build the ListView as below
<ListView x:Name="lvData" FontSize="13" Background="LightGoldenrodYellow" Margin="0,0,0,0" >
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Setter Property="HorizontalContentAlignment" Value="Stretch"/>
</Style>
</ListView.ItemContainerStyle>
<ListView.View>
<GridView>
<GridViewColumn >
<GridViewColumn.CellTemplate>
<DataTemplate>
<CheckBox IsChecked="{Binding selected}" Checked="Selection_Changed" Unchecked="Selection_Changed" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="id" Width="60">
<GridViewColumn.CellTemplate>
<DataTemplate >
<TextBlock Text="{Binding id}" TextAlignment="Right" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="name" Width="100" DisplayMemberBinding="{Binding name}"/>
<GridViewColumn Header="s01" Width="35">
<GridViewColumn.CellTemplate>
<DataTemplate>
<Grid Background="{Binding Path=LED01}" Margin="-5,0,-5,0">
<Label Margin="0,0,0,0" Content="{Binding Path=S01}" HorizontalContentAlignment="Right"></Label>
</Grid>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="s02" Width="35">
<GridViewColumn.CellTemplate>
<DataTemplate>
<Grid Background="{Binding Path=LED02}" Margin="-5,0,-5,0">
<Label Margin="0,0,0,0" Content="{Binding Path=S01}" HorizontalContentAlignment="Right"></Label>
</Grid>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
:
{repeat for s03...s20}
:
</GridView>
</ListView.View>
</ListView>
With some properties added to my data class:
public string LED01 { get { return GetLED(1); } }
public string S01 { get { return GetServo(1); } }
public string LED02 { get { return GetLED(2); } }
public string S02 { get { return GetServo(2); } }
And two methods GetLED & GetServo return the color & display based on the value of array LEDs & servos with given index.
It seems really dummy that I have repeated the GridViewColumn block many times, and 40 dummy properties have been created as it cannot bind to the method directly.
May I know if there has any simple way to build the listview?
In fact, the size of array is not fix, 21 is just the maximum size.
I'd like to know if it can build the column dynamically, so that it can be created based on the actual size, and those repeated columns can be done within a loop. And set the Background & Data content based on class method, so that I can set to GetLED(?) & GetServo(?) directly without building a dummy property for it.
Thanks in advance.
