Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ElementName Binding is failing

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 is failing: MarkerEnabled="{Binding ElementName=DisplayMarkers, Path=IsChecked}"

I'm trying to bind to the IsChecked property on my Checkbox named 'DisplayMarkers". When I run this, in debug mode in VS 2010, the output window shows the binding is failing. It can't find the element named 'Checkbox'. Could anyone tell me why?

The error I'm getting from VS is:

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')
like image 925
Hosea146 Avatar asked Feb 03 '12 01:02

Hosea146


3 Answers

You might not have a namescope where you try to bind, you could try to replace the ElementName construct with Source={x:Reference DisplayMarkers}.

The gist of it is that if you have elements in XAML which are not in the visual or logical tree you will not be able to use certain bindings like RelativeSource and ElementName, I suspect that DataSeries is not in any tree either (it sure sounds like it's abstract).

For a workaround for potential cyclical dependency errors see: https://stackoverflow.com/a/6858917/546730

like image 88
H.B. Avatar answered Nov 16 '22 05:11

H.B.


I'm guessing that the writer of Chart, when deriving from FrameworkElement or whatever, failed to realize that they needed to add any child elements to the logical tree either manually or through an override. You don't get that for free when deriving.

Breaking the logical tree breaks the ability of children to bind by ElementName.

If you are the author of the Chart object, you can see this related question and answer.

like image 4
Jeremy White Avatar answered Nov 16 '22 05:11

Jeremy White


For other readers, another possible cause is using a UserControl instead of a custom control for what's in the role of vf:Chart, above. I wrote a split button (in the role of the chart) and changing it from a UserControl to a custom control got my ElementName binding working.

like image 2
Vimes Avatar answered Nov 16 '22 04:11

Vimes