程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> .NET實例教程 >> WPF中怎樣固定ListView中列的寬度?

WPF中怎樣固定ListView中列的寬度?

編輯:.NET實例教程

有朋友問這樣一個問題: 
WPF中怎樣固定ListVIEw中列的寬度?

不想多寫了,這裡有答案:
http://blogs.msdn.com/atc_avalon_team/archive/2006/04/11/573037.ASPx

為了節省你的時間,我轉貼在下面吧:


Fixed-Width Column in ListVIEw: A Column that cannot be resized
A fixed-width column is a column that cannot be resized by mouse dragging or double-clicks. You can find instances in outlook. Currently two methods can be used to achieve the effect. One is to restyle GridViewColumnHeader to remove the gripper inside its Template. The other is to subclass GridViewColumn to restrict columns'' width to a fixed size. Today''s topic is about the second solution. The resulting column is a little bit different from the restyling one because there is still a gripper inside GridVIEwColumnHeader. When mouse is over the gap between columns, the cursor is still changed to the resizing cursor.
The main WPF elements we are going to cover in this session are GridVIEwColumn, dependency property definition, dependency property coercion.
 Step 1. Subclass GridVIEwColumn
The key in this step is to override WidthProperty''s metadata to make it call CoerceWidth() to coerce its value when a new value is available.

public class FixedWidthColumn : GridVIEwColumn {
    static FixedWidthColumn() {
        WidthProperty.OverrideMetadata(typeof(FixedWidthColumn),
            new FrameworkPropertyMetadata(null, new CoerceValueCallback(OnCoerceWidth)));
}


 
private static object OnCoerceWidth(DependencyObject o, object baseValue) {
        return baseValue;
    }
}
 Step 2. Add a dependency property FixedWidth
The FixedWidth is used to set column''s fixed width in spite of whatever the column''s width is. The keys in this step are:
1.      Define a dependency property.
2.      When FixedWidth is changed, it calls CoerceValue() to coerce Width into the new value.

public double FixedWidth {
    get { return (double)GetValue(FixedWidthProperty); }
    set { SetValue(FixedWidthProperty, value); }
}
 
public static readonly DependencyProperty FixedWidthProperty =
    DependencyProperty.Register(
        "FixedWidth",
       typeof(e="COLOR: blue">double),          
        typeof(FixedWidthColumn),
        new FrameworkPropertyMetadata(double.NaN, new PropertyChangedCallback(OnFixedWidthChanged)));
 
private static void OnFixedWidthChanged(DependencyObject o, DependencyPropertyChangedEventArgs e) {
    FixedWidthColumn fwc = o as FixedWidthColumn;
    if (fwc != null)
        fwc.CoerceValue(WidthProperty);
}
Step 3. Rewrite CoerceWidth() to make it always return FixedWidth
Then the Width will always get the same value whatever value is set on Width.

private static object OnCoerceWidth(DependencyObject o, object baseValue) {
    FixedWidthColumn fwc = o as FixedWidthColumn;
    if (fwc != null)

ZE: 10pt">        return fwc.FixedWidth;
    return baseValue;
}
Step 4. Use it in GridVIEw
It is used like normal GridVIEwColumn.

<GridVIEw XMLns:l="clr-namespace:FixedWidthColumnSample">
 <l:FixedWidthColumnHeader="Coerce Fixed-Width Column"
                      DisplayMemberBinding="{Binding Name}"
                      FixedWidth="100"/>
</GridVIEw>
 
 We''re done!
 
This sample is based on the February CTP.

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved