元素名称绑定失败 [英] ElementName Binding is failing

查看:28
本文介绍了元素名称绑定失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下 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.">

此绑定失败:MarkerEnabled="{Binding ElementName=DisplayMarkers, Path=IsChecked}"

我正在尝试绑定到名为DisplayMarkers"的复选框上的 IsChecked 属性.当我在 VS 2010 的调试模式下运行它时,输出窗口显示绑定失败.它找不到名为的元素复选框".谁能告诉我为什么?

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?

我从 VS 得到的错误是:

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

推荐答案

您可能没有尝试绑定的名称范围,您可以尝试将 ElementName 结构替换为 Source={x:Reference DisplayMarkers}.

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

它的要点是,如果您在 XAML 中有不在可视化或逻辑树中的元素,您将无法使用某些绑定,例如 RelativeSourceElementName,我怀疑 DataSeries 也不在任何树中(听起来确实很抽象).

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

有关潜在循环依赖错误的解决方法,请参阅:https://stackoverflow.com/a/6858917/546730

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

这篇关于元素名称绑定失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆