简体   繁体   中英

WPF: Drawing shapes in ListView

I have a list of lines with attributes such as Brush, line thickness, etc. and I have a custom legend, which is a GridView in a ListView. What I want to accomplish is to draw the actual lines (with the Brush and thickness attributes) in the ListView to represent each line. Currently I have two columns, one for color and one for thickness. However, the color column is in hex, so it's not very useful.

Or, if it makes it simpler, it's enough to draw a square of the color under the color column instead of lines.

Any links or tutorials are appreciated. I am not familiar with drawing shapes/canvas/etc. in general, so I am a little lost.

Much appreciated!

I would use a Rectangle to draw the line and bind the Height to the thickness value and the Fill to the color value. Use the CellTemplate and set it to one of the following in the GridViewColumn .

<DataTemplate x:Key="ThicknessTemplate">
  <Rectangle Height="{Binding LineThickness}" Width="10" Fill="{Binding LineColor}"/>
<DataTemplate/>
<DataTemplate x:Key="ColorTemplate">
  <Rectangle Height="10" Width="10" Fill="{Binding LineColor}"/>
<DataTemplate/>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM