與DataGrid
WPF應用程序:WPF數據網格[System.Windows.Data錯誤:4]我已經如下所示
數據網格(簡化):
<DataGrid x:Name="CoreServiceLogDataGrid"
Grid.Row="0"
Height="auto"
ItemsSource="{Binding Source={StaticResource CoreServiceCollection}}"
AutoGenerateColumns="False"
CanUserReorderColumns="True"
CanUserSortColumns="True"
IsReadOnly="True">
<DataGrid.Columns>
<DataGridTextColumn x:Name="ID"
Header="ID"
Binding="{Binding ID}" />
<DataGridTextColumn Binding="{Binding Timestamp}"
Header="Timestamp" />
</DataGrid.Columns>
</DataGrid>
當數據被加載;我得到以下錯誤(多次):
System.Windows.Data Error: 4 : Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='System.Windows.Controls.DataGrid', AncestorLevel='1''. BindingExpression:Path=AreRowDetailsFrozen; DataItem=null; target element is 'DataGridDetailsPresenter' (Name=''); target property is 'SelectiveScrollingOrientation' (type 'SelectiveScrollingOrientation')
System.Windows.Data Error: 4 : Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='System.Windows.Controls.DataGrid', AncestorLevel='1''. BindingExpression:Path=HeadersVisibility; DataItem=null; target element is 'DataGridRowHeader' (Name=''); target property is 'Visibility' (type 'Visibility')
我不知道爲什麼發生這種情況,如何解決這個問題。
編輯:(信息約CoreServiceLogViewCollection)
CoreServiceCollection只是爲ListCollectionView。
public static ListCollectionView CoreServiceLogViewCollection {
get {
if (_coreServiceCollection == null) {
_coreServiceCollection =
new ListCollectionView(LogSession.CoreServiceLogCollection);
}
return _coreServiceCollection;
}
}
的參數僅含有ID
,Timestamp
和其它性質
ObservableCollection
EDIT2: 的實例化在App.xaml中完成:
<ResourceDictionary>
<x:Static Member="vm2:CoreServiceLogView.CoreServiceLogViewCollection"
x:Key="CoreServiceCollection" />
</ResourceDictionary>
EDIT 3(方式.. )
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<!-- #columnHeaderDGStyle -->
<!-- Datagrid -->
<Style x:Key="Log4NetDataGridStyle"
TargetType="DataGrid">
<Setter Property="ColumnHeaderStyle"
Value="{DynamicResource DatagridColumnHeaderCustomTemplateStyle}" />
<Setter Property="RowStyle"
Value="{DynamicResource Log4NetRowStyle}" />
<Setter Property="RowDetailsTemplate"
Value="{DynamicResource RowDetailsTemplate}" />
<Setter Property="MaxHeight"
Value="1600">
</Setter>
<Setter Property="MaxWidth"
Value="2560">
</Setter>
</Style>
<Style x:Key="DataCommuGridStyle"
TargetType="DataGrid">
<Setter Property="ColumnHeaderStyle"
Value="{DynamicResource DatagridColumnHeaderCustomTemplateStyle}" />
<Setter Property="RowStyle"
Value="{DynamicResource CommuRowStyle}" />
<Setter Property="RowDetailsTemplate"
Value="{DynamicResource RowDetailsTemplate}" />
<Setter Property="MaxHeight"
Value="1600">
</Setter>
<Setter Property="MaxWidth"
Value="2560">
</Setter>
</Style>
<!-- ************************* Row Style ************************* -->
<Style x:Key="Log4NetRowStyle"
TargetType="DataGridRow">
<Setter Property="FontSize"
Value="14" />
<Setter Property="Background"
Value="{Binding Path=LogColour.ColorName}" />
<Setter Property="Height"
Value="Auto">
</Setter>
<Style.Triggers>
<DataTrigger></DataTrigger>
</Style.Triggers>
</Style>
<Style x:Key="CommuRowStyle"
TargetType="DataGridRow">
<Setter Property="FontSize"
Value="14" />
<Setter Property="Background"
Value="Azure" />
<Setter Property="Height"
Value="Auto">
</Setter>
<Style.Triggers>
<DataTrigger></DataTrigger>
</Style.Triggers>
</Style>
<DataTemplate x:Key="RowDetailsTemplate">
<Border BorderThickness="0"
Padding="5" >
<Border.Background>
<LinearGradientBrush StartPoint="0,0"
EndPoint="0,1" Opacity="0.2">
<GradientStop Color="White"
Offset="0" />
<GradientStop Color="Black"
Offset="1" />
</LinearGradientBrush>
</Border.Background>
<!-- alternative with Expancer -->
<Expander IsExpanded="True"
HorizontalAlignment="Left"
BorderThickness="1,1,1,5"
MaxHeight="300"
MaxWidth="900">
<Expander.Header>
<DockPanel>
<TextBlock FontSize="12"
Text="LoggingMessage: "
VerticalAlignment="Center" />
</DockPanel>
</Expander.Header>
<Expander.Content>
<ScrollViewer VerticalScrollBarVisibility="Auto"
HorizontalScrollBarVisibility="Auto"
CanContentScroll="True"
Style="{StaticResource LeftScrollViewer}">
<StackPanel Orientation="Vertical">
<TextBox FontSize="16"
BorderThickness="0"
IsReadOnly="True"
Background="Transparent"
Foreground="Black"
TextWrapping="Wrap"
Text="{Binding LoggingMessage, Mode=OneWay}" />
</StackPanel>
</ScrollViewer>
</Expander.Content>
</Expander>
</Border>
</DataTemplate>
<Style x:Key="GroupHeaderStyle"
TargetType="{x:Type GroupItem}">
<Setter Property="Margin"
Value="0,0,0,5" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type GroupItem}">
<Expander IsExpanded="False"
Background="#FF112255"
BorderBrush="#FF002255"
Foreground="Black"
BorderThickness="1,1,1,5">
<Expander.Header>
<DockPanel>
<TextBlock FontWeight="Bold"
Foreground="White"
Text="{Binding Path=Name}"
Margin="5,0,0,0"
Width="100" />
<TextBlock FontWeight="Bold"
Foreground="White"
Text="{Binding Path=ItemCount}" />
</DockPanel>
</Expander.Header>
<Expander.Content>
<ItemsPresenter />
</Expander.Content>
</Expander>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<!-- ******************** DataTemplate ******************** -->
</ResourceDictionary>
可以請你發佈你在CoreServiceCollection中有什麼嗎? – mchicago 2013-03-21 13:20:42
我可以上傳更多,但我不認爲問題出在Collection – RayOldProf 2013-03-21 13:28:54
,那麼您在哪裏創建CoreServiceLogViewCollection的實例?它是在資源中實例化的XAML中,還是已經附加在datacontext中? – mchicago 2013-03-21 13:29:49