2015-06-10 115 views
1

在我的WPF應用水平滾動的時候,當我水平滾動一個DataGrid,Visual Studio 2010的輸出打印這樣的警告:綁定警告在數據網格

System.Windows.Data Error: 5 : Value produced by BindingExpression is not valid for target property.; Value='-0.29487179487171' BindingExpression:Path=CellsPanelHorizontalOffset; DataItem='DataGrid' (Name=''); target element is 'Button' (Name=''); target property is 'Width' (type 'Double')

我在尋找數據網格模板定義;異常應該通過「按鈕」對象的結合造成的:

<Button 
     Command="{x:Static DataGrid.SelectAllCommand}" 
     Focusable="false" 
     Style="{DynamicResource {ComponentResourceKey ResourceId=DataGridSelectAllButtonStyle, TypeInTargetAssembly={x:Type DataGrid}}}" 
     Visibility="{Binding Path=HeadersVisibility, ConverterParameter={x:Static DataGridHeadersVisibility.All}, Converter={x:Static DataGrid.HeadersVisibilityConverter}, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}" 
     Width="{Binding Path=CellsPanelHorizontalOffset, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}" /> 

Width屬性是雙擊,像結合的CellsPanelHorizo​​ntalOffset財產。

我不明白什麼是錯的,你能幫助我嗎?謝謝。

+2

我猜想它不是類型,而是負值。負寬度沒有意義。 – Nzc

+0

你可以檢查你的寬度值,它似乎是負值 – ReeganLourduraj

+0

爲什麼地球上你需要綁定寬度''CellsPanelHorizo​​ntalOffset'?你能詳細談談你實際想要達到的目標嗎? –

回答

1

原因你的錯誤是從它的描述清楚:

Value produced by BindingExpression is not valid for target property.;
Value=' -0.29487179487171 ' ...
target element is ' Button '; ...
target property is ' Width '

因此,這是綁定到Button.Width數據-0.29487179487171,但很明顯,一個Width不能爲負的值。但是,如果您要使用Converter那裏永遠不會傳遞負值,您只需隱藏真正的問題,這就是CellsPanelHorizontalOffset不應該是負面的。

我只能假設您在自定義的DataGrid中使用了一些手動計算,它從DataGridCellsPanel返回錯誤的值。從MSDN上的DataGrid Class頁面:

DataGridCellsPanel : Gets the horizontal offset for the DataGridCellsPanel .

+2

那麼,我發現這個錯誤是由我應用於數據網格的樣式造成的!確切的原因是。如果我刪除這個setter,我不再有錯誤信息,但我不明白爲什麼,想法? (P.S.我用標準的WPF數據網格進行了測試,沒有自定義控件)。 –

+0

@LucaPetrini:基於一點經驗,看起來如果(a)行標題被隱藏,並且(b)用戶拖動水平滑塊,則產生這個警告。我懷疑這是WPF代碼中的一個錯誤。無論如何,儘管很煩人,但它似乎可以安全地被忽略。 – dlf