将双精度值绑定到 Datagrid 列 MaxWidth 属性

发布于 2024-12-04 11:57:36 字数 566 浏览 5 评论 0原文

我有一个数据网格,我想通过绑定设置其列的 MaxWidth 属性。我有一个转换器可以将“绑定”更改为“双精度”,但转换器永远不会被击中,应用程序因 xaml 错误而停止:“System.Windows.Data.Binding”无法转换为“System.Double”类型。当我在 datagrid MaxWidth 属性之外的其他属性上使用转换器时,它工作正常。为什么这不起作用?

<Grid.Resources>
    <Converter:DoubleConverter x:Key="ConverterForDoubles" />
</Grid.Resources>

<data:DataGridTextColumn Header="Task ID" Binding="{Binding TaskId}" 
MaxWidth="{Binding Source={StaticResource MainViewModel}, Path=TaskIdColumnMaxWidth,
Mode=TwoWay, Converter={StaticResource ConverterForDoubles}}" />

I have a datagrid whose columns I would like to set the MaxWidth property of through binding. I have a converter to change 'binding' to 'double', but the converter never gets hit, the app stops on a xaml error: 'System.Windows.Data.Binding' cannot be converted to type 'System.Double'. When I use the converter on other properties outside of the datagrid MaxWidth property then it works fine. Why does this not work?

<Grid.Resources>
    <Converter:DoubleConverter x:Key="ConverterForDoubles" />
</Grid.Resources>

<data:DataGridTextColumn Header="Task ID" Binding="{Binding TaskId}" 
MaxWidth="{Binding Source={StaticResource MainViewModel}, Path=TaskIdColumnMaxWidth,
Mode=TwoWay, Converter={StaticResource ConverterForDoubles}}" />

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

动听の歌 2024-12-11 11:57:36

DataGridTextColumn.MaxWidth 不是依赖属性,因此您无法绑定到它。

当它期望从 XAML 解析双精度值时,它只是抱怨 "{Binding...}" 的存在。

解决方案包括添加附加属性或子类化 DataGridTextColumn 以及添加在设置 MaxWidth 时设置 MaxWidth 的依赖项属性。

DataGridTextColumn.MaxWidth is not a dependency property, so you cannot bind to it.

It is just complaining about the presence of "{Binding...}" when it expected a double value to be parsed from the XAML.

Solutions include adding attached properties or subclassing the DataGridTextColumn and adding a Dependency property that does set the MaxWidth when it is set.

看透却不说透 2024-12-11 11:57:36

发现这有助于绑定到设置器: 设置值绑定助手

Found this to help with binding to setters: Setting Value Binding Helper

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文