2014-10-30 26 views
3

试图在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')

他们出现在每个DataGrid。这让我感到很不舒服!

要重现该问题我做了一个小项目

public class MyItem 
{ 
    public string A { get; set; } 
} 

public class ViewModel 
{ 
    public List<MyItem> List { get; set; } 

    public ViewModel() 
    { 
     List = new List<MyItem>(new[] { new MyItem() }); 
    } 
} 

public partial class MainWindow : Window 
{ 
    public MainWindow() 
    { 
     InitializeComponent(); 
     DataContext = new ViewModel(); 
    } 
} 

XAML

<DataGrid ItemsSource="{Binding List}" AutoGenerateColumns="False"> 
    <DataGrid.Columns> 
     <DataGridTextColumn Binding="{Binding A}" Header="A"/> 
    </DataGrid.Columns> 
    <DataGrid.GroupStyle> 
     <GroupStyle> 
      <GroupStyle.ContainerStyle> 
       <Style TargetType="GroupItem"> 
        <Setter Property="Template"> 
         <Setter.Value> 
          <ControlTemplate TargetType="GroupItem"> 
           <!-- anything or nothing here --> 
          </ControlTemplate> 
         </Setter.Value> 
        </Setter> 
       </Style> 
      </GroupStyle.ContainerStyle> 
     </GroupStyle> 
    </DataGrid.GroupStyle> 
</DataGrid> 

一些观察:

  • 没有DataGrid.GroupStyle没有错误;
  • AutoGenerateColumns = true没有错误;
  • 没有约束力(直接设置DataGrid.ItemsSource)没有错误。

只有与这三个条件相反的组合才会开始发送垃圾邮件Output带上述消息的窗口。

我该怎么办?我不能忽略错误,也没有办法解决它们。

谷歌搜索并没有真正的帮助,例如,this case被称为一个错误,我试图应用其解决方法,但没有为我工作。

P.S:在使用DataGrid第一次尝试这样发现的bug是非常士气低落。


试图处理第二个错误。

<DataGrid.RowHeaderStyle> 
    <Style TargetType="DataGridRowHeader"> 
     <Setter Property="Visibility" Value="Collapsed"/> 
     <Setter Property="Template" Value="{x:Null}"/> 
    </Style> 
</DataGrid.RowHeaderStyle> 

但错误依然

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')

回答

3

当时与控制模板和改变一个DataGridRow错误都不见了后!

<DataGrid.RowStyle> 
    <Style TargetType="DataGridRow"> 
     <Setter Property="Template"> 
      <Setter.Value> 
       <ControlTemplate TargetType="DataGridRow"> 
        <Border BorderThickness="{TemplateBinding Border.BorderThickness}" BorderBrush="{TemplateBinding Border.BorderBrush}" Background="{TemplateBinding Panel.Background}" Name="DGR_Border" SnapsToDevicePixels="True"> 
         <SelectiveScrollingGrid> 
          <DataGridCellsPresenter ItemsPanel="{TemplateBinding ItemsControl.ItemsPanel}" SnapsToDevicePixels="{TemplateBinding UIElement.SnapsToDevicePixels}"/> 
         </SelectiveScrollingGrid> 
        </Border> 
       </ControlTemplate> 
      </Setter.Value> 
     </Setter> 
    </Style> 
</DataGrid.RowStyle> 

我删除DataGridDetailsPresenterDataGridRowHeader从默认的模板,因为我不会使用它们。


我救一个错误

System.Windows.Data Error: 4 : Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='System.Windows.Controls.DataGrid', AncestorLevel='1''. BindingExpression:Path=NewItemMargin; DataItem=null; target element is 'DataGridRow' (Name=''); target property is 'Margin' (type 'Thickness')

我固定加入Margin二传手到DataGrid.RowStyle

<Setter Property="Margin" Value="0"/> 

似乎所有这些错误可以通过重新理默认模板固定。

+0

谢谢,伙计!通过Google搜索搜索了很多“解决方法”之后,只有您的方式有效! – 2017-06-19 02:08:53

+0

也为我工作。谢谢! – GordoFabulous 2017-08-03 21:11:17

0

首先感谢Sinatr为问题分析和解决方案,这几乎为我工作。 我确实与DataGrid控件有同样的问题。除了输出中的绑定错误之外,新项目行有轻微的位移(在我的情况下,DataRowMargin被两个DIP关闭)。

DataGridRow invalid margin

整个问题是由风格触发启动的时候,IsNewItemProperty等于真,DataGridRow没有得到适当的空白值造成的。 Sinatr的答案确实解决了绑定问题,但仍然使用了错误的保证金。这是删除绑定错误并设置正确边距的样式。

<Style TargetType="DataGridRow"> 
    <Setter Property="Margin" Value="0,0,0,0"/> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="DataGridRow"> 
       <Border BorderThickness="{TemplateBinding Border.BorderThickness}" BorderBrush="{TemplateBinding Border.BorderBrush}" Background="{TemplateBinding Panel.Background}" Name="DGR_Border" SnapsToDevicePixels="True"> 
        <SelectiveScrollingGrid> 
         <DataGridCellsPresenter ItemsPanel="{TemplateBinding ItemsControl.ItemsPanel}" SnapsToDevicePixels="{TemplateBinding UIElement.SnapsToDevicePixels}"/> 
        </SelectiveScrollingGrid> 
       </Border> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
    <Style.Triggers> 
     <Trigger Property="IsNewItem" Value="True"> 
      <Setter Property="Margin" Value="1,0,0,0"/> 
     </Trigger> 
    </Style.Triggers> 
</Style>