有朋友問這樣一個問題:
WPF中怎樣固定ListVIEw中列的寬度?
不想多寫了,這裡有答案:
http://blogs.msdn.com/atc_avalon_team/archive/2006/04/11/573037.ASPx
為了節省你的時間,我轉貼在下面吧:
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;