I've tried implementing a readonly grid of items using the WPF ListView.
It appears to me that this is a poorly designed and implemented control. (Unlike most of WPF which I like).
Specific issues I've come across:
Cannot make the last column expand to fill the width of the control, without writing extra non-XAML code. (See this question )
Have to override some arbitrarily hard-coded values to get a cell template to actually fill the area of a cell. (See this blog entry)
To me, these are pretty obvious flaws, implying that t开发者_如何学运维his control was rushed through into Visual Studio 2008.
Does anyone know if these have been fixed in Visual Studio 2010?
Have you checked out the WPF Toolkit?
It has a DataGrid
control that does a lot more out of the box than ListView/GridView
.
http://wpf.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=29117
Regarding your particular concerns, though, I'm not sure there's an automatic way to make the last column fill the remaining width of the container (the default is for a "blank" column to fill up the remaining space) [correction: see Edit below]. I do know you there is a Visiblity
property for the columns, though, which is a huge improvement IMO.
I do have some examples of DataGrid
that have specially colored cells, and the color goes right up to the edges of the cell, so I think your second concern should be doable.
Just as an example, here's my cell colorizer style:
<Style
x:Key="Main_DataGridCellStyle"
TargetType="mwc:DataGridCell">
<Setter
Property="BorderThickness"
Value="0" />
<Setter
Property="Background"
Value="{Binding XPath=@Background}" />
<Setter
Property="Foreground"
Value="Black" />
</Style>
Also, the DataGrid
sorts automatically, although I did have to do some pretty heavy template editing to get the arrows to appear correctly :( Hopefully this will be fixed by the time VS10 comes out--I'm pretty sure the community is aware of it.
Edit
I stand corrected (in a good way). I just tried Width="*"
with the last column in a test DataGrid
and it worked like a charm.
Also, this link confirms that DataGrid
will be part of WPF 4.0, which is part of VS 2010:
http://weblogs.asp.net/scottgu/archive/2009/10/26/wpf-4-vs-2010-and-net-4-0-series.aspx
VS 2010 is not the framework, and as far as I know .net 3.5 sp1 is the latest edition of WPF. I'm not sure if your problems have other solutions, though most likely yes, but certainly the behavior will be the same in VS 2010.
Have you looked into providing your own templates for ListView's rows and column manager?
Edit: If you were asking about upcoming .net 4, the GridViewColumn Width property is still a Double, so it doesn't support star sizing. So most likely the behavior is still the same.
XCeed's datagrid supports star sizing, though in my experience it's also kind of funky, and they have a special stretch mode you can turn on, but honestly, the attached behavior method described in the article you linked is fairly clean and well done. You can toss it in your project pretty much wholesale and just use it.
http://www.ontheblog.net/CMS/Home/tabid/36/EntryID/37/Default.aspx
It's not overriding any "arbitrarily hard-coded values", it's adding a new attached behavior, in this case stretching all columns equally when an attached property "Stretch" is set to true. You'll have to tweak it slightly for your case in order to only stretch the last column.
The attached behavior method is a nice way to go about it, completely reusable, self contained, and keeps your xaml clean without any mess in the code behind.
精彩评论