将双精度值绑定到 Datagrid 列 MaxWidth 属性
我有一个数据网格,我想通过绑定设置其列的 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
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.
发现这有助于绑定到设置器: 设置值绑定助手
Found this to help with binding to setters: Setting Value Binding Helper