如何根据 SL4 中另一个单元格的内容使数据网格中的单元格只读?
我有两列,第二列取决于第一列的内容。默认情况下,第二列是只读的。当我输入一些有效值时,我希望第二列变得可编辑。 为了实现这一目标,我在第二列上创建了一个单元格模板和单元格编辑模板,其中背景和只读绑定到第一列。加载时,第一列为空,因此我的第二列正确地为只读。以下是第二列的单元格模板,其中背景颜色是根据第一列设置的。
<DataTemplate>
<Grid>
<Border Background="{Binding FristColumn,Converter={StaticResource ColorConverter}}"/>
<TextBlock Text="{Binding SecondColumn, Converter={StaticResource NumberFormatter}}" HorizontalAlignment="Stretch" VerticalAlignment="Center" Margin="0"/>
</Grid>
</DataTemplate>
以下是第二列的单元格编辑模板,使其可编辑。
<DataTemplate>
<Grid>
<TextBox Text="{Binding SecondColumn, Mode=TwoWay, Converter={StaticResource NumberFormatter}}" Margin="0" HorizontalAlignment="Right" IsReadOnly="{Binding FirstColumn, Converter={StaticResource readOnlyConverter}, ConverterParameter=FirstColumn}" Background="{Binding Depend,Converter={StaticResource ColorConverter}, ConverterParameter=FirstColumn}" />
</Grid>
</DataTemplate>
有了这两个模板,当在第一列中输入有效值时,我期望第二列颜色发生变化,但事实并非如此。但是,如果我双击该单元格,那么它会根据第一个单元格正常运行。我缺少什么吗?
I have two columns, the second column depends on the content on the first column. By default, the second columns is readonly. When I enter some valid value, I want the second column to become editable.
To achive this, I created a cell template and cell edit template on the second column where back ground and read only bound to the first column. On load, the first column is null so my second columns comes correctly as read only. Following is Cell Template for second column, where the background color is set by based on the first column.
<DataTemplate>
<Grid>
<Border Background="{Binding FristColumn,Converter={StaticResource ColorConverter}}"/>
<TextBlock Text="{Binding SecondColumn, Converter={StaticResource NumberFormatter}}" HorizontalAlignment="Stretch" VerticalAlignment="Center" Margin="0"/>
</Grid>
</DataTemplate>
Following is cell edit template for second column to make it editable
<DataTemplate>
<Grid>
<TextBox Text="{Binding SecondColumn, Mode=TwoWay, Converter={StaticResource NumberFormatter}}" Margin="0" HorizontalAlignment="Right" IsReadOnly="{Binding FirstColumn, Converter={StaticResource readOnlyConverter}, ConverterParameter=FirstColumn}" Background="{Binding Depend,Converter={StaticResource ColorConverter}, ConverterParameter=FirstColumn}" />
</Grid>
</DataTemplate>
With these two in place, when enter the valid value in the first column, I was expecting the second column color to change but it does not. But If I double click on the cell then it behaves properly based on the first cell. Is there some thing I am missing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题是,对象集合没有实现 INotifyPropertyChanged。一旦我实现了 INotifyPropertyChanged,颜色和单元格就会根据转换器变得可编辑和不可编辑。
希望这对其他人有帮助。
The problem was, the object collection did not have INotifyPropertyChanged implemented. Once I have INotifyPropertyChanged implemented, the colors and cell become editable and non editable based on the converter.
Hope this helps others.