Click here to Skip to main content
15,063,596 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I am working with a project there I am making a list with colours and some text.
To add text and colour codes it's working fine.

Here is the XAML code:

XML
<ListView Grid.Row="0" Margin="5" Name="lvCustomStream" Width="Auto">
  <ListView.View>
    <GridView >
      <GridViewColumn Header="Length" Width="90" DisplayMemberBinding="{Binding Length}" />
      <GridViewColumn Header="Speed" Width="90" DisplayMemberBinding="{Binding Speed}" />
      <GridViewColumn Header="Colour1" Width="90" DisplayMemberBinding="{Binding Colour1}" />
      <GridViewColumn Header="Colour2" Width="90" DisplayMemberBinding="{Binding Colour2}" />
    </GridView>
  </ListView.View>
</ListView>
<Grid Grid.Row="2">
<Grid.RowDefinitions>
  <RowDefinition Height="Auto"/>
  <RowDefinition Height="Auto"/>
  <RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
  <StackPanel Orientation="Horizontal" Grid.Row="2">
  <Button x:Name="btCsSave" Width="40" Margin=" 5" Content="Save" Click="btCsSave_Click"/>
  <Button x:Name="btnCsCancel" Width="40" Margin="5" Content="Cancel"     Click="btnCsCancel_Click"/>
  </StackPanel>
<StackPanel Orientation="Horizontal" Grid.Row="1">
  <Label Margin="5,5,0,5" Width="Auto" Content="Length"/>
  <TextBox x:Name="txtCsLength" Margin="0,5,5,5" Width="40"/>
  <Label Margin="5,5,0,5" Width="Auto" Content="Speed"/>
  <TextBox x:Name="txtCsSpeed" Margin="0,5,5,5" Grid.Row="1" Width="40"/>
  <Label Margin="5,5,0,5" Width="Auto" Content="Colour1"/>
  <Rectangle x:Name="rectCsColour1" Margin="0,5,5,5" Grid.Row="1" Width="25" MouseDown="txtCsColour1_MouseDown" Fill="#FFFBD904"/>
  <Label  Margin="5,5,0,5" Width="Auto" Content="Colour2"/>
  <Rectangle x:Name="rectCsColour2" Margin="0,5,5,5" Grid.Row="1" Width="25" Fill="#FFF9F904" MouseDown="rectCsColour2_MouseDown"/>
</StackPanel>
</Grid>


The code behind:

C#
private void btCsSave_Click(object sender, RoutedEventArgs e)
  {
    if (txtCsThreadLength.Text == "") return;
    lvCustomStream.Items.Add(new CsItem(txtCsLength.Text.ToString(),  txtCsSpeed.Text.ToString(), rectCsColour1.Fill.ToString(), rectCsColour1, rectCsColour2.Fill.ToString()));
  }


What is missing is: when I click on the save button, I want to get the two rectangles too into the ListView after the two colour values, not just the two Colour-values, Speed and Length.

Thanks in advance
Vidor
Posted
Comments
Sergey Alexandrovich Kryukov 17-Oct-13 10:59am
   
Not clear. Where do you try to add a rectangle object after the window is already shown? All you show is doing something with string, which looks irrelevant to the question.
—SA
[no name] 18-Oct-13 2:52am
   
Yes the window shows the rectangle and I can change the colour in it, and when I click the save button I want that rectangle to be copied with the value of colour in the List View.

1 solution

DisplayMemberBinding on GridViewColumn will display thetext representation of the binding property.

You need to set CellTemplate of GridViewColumn to a template that you want to display the data, along the lines

XML
<gridviewcolumn header="Colour1" width="90">
    <gridviewcolumn.celltemplate>
        <datatemplate>
            <rectangle fill="{Binding}"></rectangle>
        </datatemplate>
    </gridviewcolumn.celltemplate>
</gridviewcolumn>
   
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900