English 中文(简体)
WPF ListView - is it better in Visual Studio 2010?
原标题:
  • 时间:2009-11-05 23:14:21
  •  标签:
  • wpf
  • listview

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 this 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.





相关问题
WPF convert 2d mouse click into 3d space

I have several geometry meshes in my Viewport3D, these have bounds of (w:1800, h:500, d:25). When a user clicks in the middle of the mesh, I want the Point3D of (900, 500, 25)... How can I achieve ...

Editing a xaml icons or images

Is it possible to edit a xaml icons or images in the expression design or using other tools? Is it possible to import a xaml images (that e.g you have exported) in the expression designer for editing?...

WPF: writing smoke tests using ViewModels

I am considering to write smoke tests for our WPF application. The question that I am faced is: should we use UI automation( or some other technology that creates a UI script), or is it good enough to ...

WPF - MVVM - NHibernate Validation

Im facing a bit of an issue when trying to validate a decimal property on domain object which is bound to a textbox on the view through the viewmodel. I am using NHibernate to decorate my property on ...

How do WPF Markup Extensions raise compile errors?

Certain markup extensions raise compile errors. For example StaticExtension (x:Static) raises a compile error if the referenced class cannot be found. Anyone know the mechanism for this? Is it baked ...

WPF design-time context menu

I am trying to create a custom wpf control, I m wondering how I can add some design-time features. I ve googled and can t seem to get to my goal. So here s my simple question, how can I add an entry ...

How to combine DataTrigger and EventTrigger?

NOTE I have asked the related question (with an accepted answer): How to combine DataTrigger and Trigger? I think I need to combine an EventTrigger and a DataTrigger to achieve what I m after: when ...

热门标签