views:

45

answers:

1

I get the following errors from the code below... not sure why (and yes, it produces all 4 even though it's the same 2 repeated). Oh, and it doesn't produce the alternating rows effect, even though prior to these errors popping up the same code was working.

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

<UserControl x:Class="MyProject.Views.RegistrationAllView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:MyProject.Views"
             >
    <Grid>
        <DataGrid Name="TestGrid" Grid.Row="2" Grid.ColumnSpan="2" AutoGenerateColumns="True"
                  ItemsSource="{Binding Registrations}" SelectedValue="{Binding CurrentRegistration}" IsReadOnly="True" GridLinesVisibility="None"
                  AlternatingRowBackground="#FFCAC6C6"
                  >
            <DataGrid.RowStyle>
                <Style>
                    <EventSetter Event="DataGridRow.MouseDoubleClick" Handler="TestGrid_MouseDoubleClick" />
                </Style>
            </DataGrid.RowStyle>
        </DataGrid>
    </Grid>
</UserControl>


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using MyProject.ViewModels;

using WPFBase;
using WPFBase.ViewModels;

namespace MyProject.Views
{
    public partial class RegistrationAllView : UserControl
    {
        public RegistrationAllView()
        {
            InitializeComponent();
        }

        private void TestGrid_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            DependencyObject source = e.OriginalSource as DependencyObject;

            RegistrationEntity entity = (RegistrationEntity)TestGrid.CurrentItem;

            TabControl TabCollection = (TabControl)UIHelper.TryFindParentControl<TabControl>(this);

            RegistrationForm view = new RegistrationForm();

            XTabItem tabItem = new XTabItem();
            tabItem.Header = String.Format("Registration (#{0})", entity.ID);
            tabItem.Content = view;

            TabCollection.Items.Add(tabItem);

            tabItem.Focus();

            AbstractViewModel vm = new RegistrationViewModel(entity);

            view.DataContext = vm;
        }
    }
}
A: 

First off, the WPF datagrid's rows are white by default so why are you setting them white in your style? You could get rid of the DataGrid.Resources bit completely and replace AlternationCount=2 with AlternatingRowBackground="FFCAC6C6" (although this will cause the first row to be white and the second one to be colored etc. If that's not acceptable you can still delete the trigger which sets the background white).

About the errors - since the code your provided does not contain any bindings with RelativeSource set, I can only conclude two things:

1) Either you didn't provide the full code and you need to relook your bindings which have RelativeSource in them, since clearly there is an error somewhere.

2) You aren't using the WPF's builtin DataGrid. Perhaps the WPF toolkit DataGrid from codeplex? Although I belive that it shouldn't have those errors either, it's more likely conclusion 1 again.

Marko
@Marko: I've posted the entirety of the code, though there really wasn't much more to post. I did make changes to the AlternatingRowBackground as suggested and it works fine and is more condensed. I still get the errors, though they don't really seem to affect the functionality or display. Oh, and I am using the built-in Datagrid (not the one from the WPF Toolkit).
myermian
There must be some bit of code that has a binding with ancestor look up mode. 1) Perhaps some class in xmlns:local="clr-namespace:MyProject.Views"? 2) Or some template is overriden somewhere... 3) If you remove the ItemsSource and SelectedValue bindings from the datagrid do the errors dissapear? 4) I think there ought to be a away to turn up the verbosity of databinding errors, google for it and perhaps you'll get more clues as to where those errors are thrown at. 5) Since this is a user control which can't be debugged on it's own, maybe the errors come from the hosting window?
Marko