C# 在 WPF DataGrid 中绑定 DataGridColumn 的 Visible 属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/669060/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 12:38:13  来源:igfitidea点击:

Binding Visible property of a DataGridColumn in WPF DataGrid

c#wpfdata-binding

提问by Matt Randle

I cannot bind the Visible property of the WPF datagridtextcolumn to a boolean value.

我无法将 WPF datagridtextcolumn 的 Visible 属性绑定到布尔值。

My binding expression is,

我的绑定表达式是,

{Binding Path=DataContext.IsThisColumnVisible, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Window},Converter={StaticResource vc}}

I have checked that the converter works (bool to the visibility enum) and is in scope.

我已经检查过转换器是否工作(bool 到可见性枚举)并且在范围内。

If I use the same expression for the header of the column, the header displays 'false' as expected.

如果我对列的标题使用相同的表达式,则标题会按预期显示“false”。

Visible is a dependency property so should be bindable.

Visible 是一个依赖属性,所以应该是可绑定的。

Anyone see what Im doing wrong? Or has anyone else been able to bind to the visible property.

有人看到我做错了什么吗?或者其他任何人都能够绑定到可见属性。

Regards,

问候,

Matt

马特

采纳答案by Matt Randle

I worked this out.

我解决了这个问题。

DataGridCOlumn is not a framework element so the FindAncestor call was failing (DataGridColumn not part of visual tree)

DataGridCOlumn 不是框架元素,因此 FindAncestor 调用失败(DataGridColumn 不是可视化树的一部分)

Have to set source property of binding expression to a staticresource and it works fine.

必须将绑定表达式的 source 属性设置为 staticresource 并且它工作正常。

回答by Kent Boogaart

Hard to say from so little of your code. What is in Visual Studio's Output window (under Debug)? That will often give you a clue as to the problem.

很难从你的代码这么少说。Visual Studio 的输出窗口(在调试下)中有什么?这通常会给你一个关于问题的线索。

回答by Joseph

If you can bind from code you can use

如果您可以从代码绑定,则可以使用

BindingOperations.SetBinding(DatagridColumInstance,
                             DatagridColum.VisibilityProperty,
                             myNewBindDef);

回答by Guge

I was looking for the same thing and found an execellent way to do it in an article about forwarding datacontext to columns.

我一直在寻找同样的东西,并在一篇关于将数据上下文转发到列的文章中找到了一种很好的方法来做到这一点。