ElementName Element Binding Error
I have the following XAML:
<Grid> <Grid.RowDefinitions> ... </Grid.RowDefinitions> <DataGrid Grid.Row="0" ...> <DataGrid.Columns> ... </DataGrid.Columns> </DataGrid> <DockPanel Grid.Row="2"> <CheckBox x:Name="DisplayMarkers" DockPanel.Dock="Top" Content="Display Data Points?" Margin="8,5,0,5" d:LayoutOverrides="Height" HorizontalAlignment="Left" IsChecked="False" /> <vf:Chart DockPanel.Dock="Top" ScrollingEnabled="False" ZoomingEnabled="True" ToolBarEnabled="True"> <vf:DataSeries AxisYType="Secondary" RenderAs="Line" DataSource="{Binding CdTeRoughnessList}" XValueType="DateTime" MarkerEnabled="{Binding ElementName=DisplayMarkers, Path=IsChecked}" Color="Navy" LegendText="Roughness Std. Dev."> This binding does not work: MarkerEnabled="{Binding ElementName=DisplayMarkers, Path=IsChecked}"
I am trying to bind the IsChecked property to my flag named "DisplayMarkers". When I run it, in debug mode in VS 2010, the output window shows that the binding is not working. It cannot find an item named "Checkbox". Can someone tell me why?
The error I get from VS:
System.Windows.Data Error: 4 : Cannot find source for binding with reference 'ElementName=DisplayMarkers'. BindingExpression:Path=IsChecked; DataItem=null; target element is 'DataSeries' (Name=''); target property is 'MarkerEnabled' (type 'Nullable`1') You may not have a name pointer where you are trying to bind, you can try replacing the ElementName construct with Source={x:Reference DisplayMarkers} .
A workaround for possible circular dependency errors is see: https://stackoverflow.com/a/316829/
I assume that the author of the diagram, getting from FrameworkElement or something else, did not understand that they need to add any children to the logical tree either manually or through redefinition. You do not receive it for free upon receipt.
Breaking a logical tree breaks the ability of children to bind to ElementName.
If you are the author of the Chart object, you can see this related question and answer.