wpf - 与 ElementName 绑定(bind)不起作用

标签 wpf binding

这是 XAML:

<DataGrid Grid.Column="0"  AutoGenerateColumns="False" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" ItemsSource="{Binding Path=Columns}"
              x:Name="ColumnsGrid" RowHeaderWidth="0">
        <DataGrid.Columns>
            <DataGridTextColumn Width="*" Binding="{Binding Path=Header}" 
                                Header="{Binding ElementName=ColumnsGrid, Path=DataContext.Count, StringFormat=Columns ({0}), diag:PresentationTraceSources.TraceLevel=High}"/>
        </DataGrid.Columns>  
    </DataGrid>

绑定(bind)返回错误:System.Windows.Data 错误:2:找不到目标元素的管理 FrameworkElement 或 FrameworkContentElement。

我想念什么?

更新:

这是答案:
http://blogs.msdn.com/b/jaimer/archive/2008/11/22/forwarding-the-datagrid-s-datacontext-to-its-columns.aspx

What is happening here? The Columns collection is just a property in the Datagrid; this collection is not in the logical (or visual) tree, therefore the DataContext is not being inherited, which leads to there being nothing to bind to.



更新 2:
关于 DataGrid 警告的好文章:http://blogs.msdn.com/b/vinsibal/archive/2009/04/07/5-random-gotchas-with-the-wpf-datagrid.aspx

最佳答案

绑定(bind) DataGridColumn对于 Header abd Visibility属性需要特殊处理...

看到这个...

Bind datagrid column visibility MVVM

关于wpf - 与 ElementName 绑定(bind)不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7964387/

相关文章:

wpf - 如何使用 ObservableCollection 源实现 XAML 单选按钮控件?

c# - wpf - 将数据上下文绑定(bind)到单例类的静态属性

javascript - 克隆 sapUi5 json 模型并停止绑定(bind)

JavaFX Controller 类变量未绑定(bind)到其 FXML 对应项

wpf - 如何绑定(bind)到 ListBox 的视口(viewport)宽度(即没有滚动条的宽度)?

python - 列出设备时 PyAudio 'utf8' 错误

c# - 无论鼠标在哪里,如何访问 WPF 中的鼠标移动事件?

WPF 定位占据窗口的全高

wpf - 在程序退出之前更新依赖属性

c# - 如何使用带有两个构造函数的viewmodel的Unity框架显示子窗口?