tags:

views:

84

answers:

3

I have a problem whit binding in wpf i have a textbox where i can do some input, then i try to bind the textinput to a custom usercontrol. This work for the usercontrol within RowDetailsTemplate but not in the CellTemplate. For each object in the CellTemplate i get this error output:

System.Windows.Data Error: 4 : Cannot find source for binding with reference 'ElementName=ScaleTextBox'. BindingExpression:Path=Text; DataItem=null; target element is 'Chart' (Name=''); target property is 'MaxValue' (type 'Int32')

My code looks like this:

XAML
<ToolBarTray ToolBarTray.IsLocked="True"  DockPanel.Dock="Top" Height="25">
    <ToolBar Name="ButtonBar" >
        <TextBox Height="23" Name="ScaleTextBox" Width="120" Text="400"/>
    </ToolBar>
</ToolBarTray>
<DataGrid ItemsSource="{Binding Path=Items}" AutoGenerateColumns="False" IsReadOnly="True" RowHeight="25" RowDetailsVisibilityMode="VisibleWhenSelected">
       <DataGrid.RowDetailsTemplate>
        <DataTemplate>
            <StackPanel Orientation="Vertical" >
                <my:UserControl ItemsSource="{Binding Path=Samples}" MaxValue="{Binding ElementName=ScaleTextBox, Path=Text}"/>-->
            </StackPanel>
        </DataTemplate>
    </DataGrid.RowDetailsTemplate>
    <DataGrid.Columns>
        <DataGridTemplateColumn MinWidth="150" Header="Chart" >
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <my:UserControl ItemsSource="{Binding Path=Samples}" MaxValue="{Binding ElementName=ScaleTextBox, Path=Text}"/><!-- this is the problem -->
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>

    </DataGrid.Columns>

</DataGrid>

C#
public static readonly DependencyProperty MaxValueProperty = DependencyProperty.Register("MaxValue", typeof(int), typeof(Chart), new FrameworkPropertyMetadata(MaxValuePropertyChanged));
private static void MaxValuePropertyChanged(DependencyObject source, DependencyPropertyChangedEventArgs e)
{
    Console.WriteLine(e.NewValue);
}

What do i do wrong?

+1  A: 

From this link

The Columns collection is just a property in the Datagrid; this collection is not in the logical (or visual) tree, therefore the DataContext is not being inherited, which leads to there being nothing to bind to.

Hence it works for your RowDetailsTemplate and not for your columns I guess.

Veer
That explains it, see if I can manage to do a workaround. Would be nice to find a XAML solution. Thanks for the info.
Cinaird
This particular problem is actually related to issues with NameScope, not DataContext. Using an ElementName Binding explicitly defines the Source rather than falling back to the current DataContext as the source. The statement about the logical and visual tree still applies nicely.
John Bowen
A: 

You can try this:

<DataTemplate>
   <my:UserControl 
     ItemsSource="{Binding Path=Samples}" 
     MaxValue="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type local:YourControlClassName}}, ElementName=ScaleTextBox, Path=Text}"/>
</DataTemplate> 
Andy
I get innerException:Binding.ElementName cannot be set while using Binding.RelativeSource.I tried whit this too but same problem: AncestorType={x:Type DockPanel}
Cinaird
You could create a property on your control, called 'ScaleFactor', to hold the value of the scale factor, bind the Text property of the TextBox to it(TwoWay), and then in the DataTemplate use: {Binding RelativeSource={...}, Path=ScaleFactor, ...}
Andy
A: 

Ok, i Solved it using ElementSpy, to see how elementSpy works look here: http://joshsmithonwpf.wordpress.com/2008/07/22/enable-elementname-bindings-with-elementspy/

and the xaml:

<my:UserControl  local:ElementSpy.NameScopeSource="{StaticResource ElementSpy}" ItemsSource="{Binding Path=Samples}" MaxValue="{Binding ElementName=ScaleTextBox, Path=Text}" />
Cinaird