wpf 元素名称绑定失败
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9122592/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ElementName Binding is failing
提问by Hosea146
I have the following XAML:
我有以下 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}"
此绑定失败: 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?
我正在尝试绑定到名为“DisplayMarkers”的复选框上的 IsChecked 属性。当我在 VS 2010 的调试模式下运行它时,输出窗口显示绑定失败。它找不到名为“Checkbox”的元素. 谁能告诉我为什么?
The error I'm getting from VS is:
我从 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')
回答by H.B.
You might not have a namescope where you try to bind, you could try to replace the ElementName
construct with Source={x:Reference DisplayMarkers}
.
您可能没有尝试绑定的名称范围,您可以尝试ElementName
用Source={x:Reference DisplayMarkers}
.
For a workaround for potential cyclical dependency errors see: https://stackoverflow.com/a/6858917/546730
有关潜在循环依赖错误的解决方法,请参阅:https: //stackoverflow.com/a/6858917/546730
回答by Jeremy White
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.
我猜想 Chart 的作者在从 FrameworkElement 或其他东西派生时,没有意识到他们需要手动或通过覆盖将任何子元素添加到逻辑树中。推导时您不会免费获得它。
Breaking the logical tree breaks the ability of children to bind by ElementName.
破坏逻辑树会破坏孩子通过 ElementName 绑定的能力。
If you are the author of the Chart object, you can see this related question and answer.