Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
819 views
in Technique[技术] by (71.8m points)

wpf - 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')
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...