WPF-如何在 DataTemplate 中绑定项目?
我遇到了一点问题。我创建了一个 DataTemplate 并将其放入 App.xaml 中,我想使用它来设置我拥有的十几个 ListView 中的复选框的样式。每个特定的 ListView 至少有一列带有布尔值。
这是我制作的 DataTemplate:
<DataTemplate x:Key="checkBoxDataTemplate">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<CheckBox Grid.Column="0" Grid.Row="0" Style="{StaticResource DataGridCheckBox}" IsChecked="{WhatDoIPutHere?}" />
</Grid>
</DataTemplate>
我正在尝试用它来替换它:
<GridViewColumn Header="Discovery Date">
<GridViewColumn.CellTemplate>
<DataTemplate>
<CheckBox Grid.Column="0" Grid.Row="0" Style="{StaticResource DataGridCheckBox}" IsChecked="{Binding CaseProperty.DiscoveryDate, Mode=OneWay, Converter={StaticResource booleanConverter},ConverterParameter='datetime'}" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
如您所见,我将 IsChecked 绑定到附加到我的 ListView 的对象的特定属性。如果我用我的模板替换它,如何绑定到 DataTemplate 中 CheckBox 的 IsChecked 属性?
预先感谢,
桑尼
I've run into a bit of a problem. I've created a DataTemplate and put it into App.xaml and I'd like to use it to style my CheckBoxes within a dozen or so ListViews I have. The particular ListViews each have at least one column with a bool value.
Here's the DataTemplate I made:
<DataTemplate x:Key="checkBoxDataTemplate">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<CheckBox Grid.Column="0" Grid.Row="0" Style="{StaticResource DataGridCheckBox}" IsChecked="{WhatDoIPutHere?}" />
</Grid>
</DataTemplate>
And I'm trying to use it to replace this:
<GridViewColumn Header="Discovery Date">
<GridViewColumn.CellTemplate>
<DataTemplate>
<CheckBox Grid.Column="0" Grid.Row="0" Style="{StaticResource DataGridCheckBox}" IsChecked="{Binding CaseProperty.DiscoveryDate, Mode=OneWay, Converter={StaticResource booleanConverter},ConverterParameter='datetime'}" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
As you can see, I have IsChecked bound to a specific property of an object attached to my ListView. If I replace it with my template, how do I bind to the IsChecked property of CheckBox in the DataTemplate?
Thanks in advance,
Sonny
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以尝试以下方式:
然后设置适当的
DataContext
。You can try this way:
and then set the appropriate
DataContext
.从那以后我就放弃了这种方法。我不认为 DataTemplates 被设计成以这种方式工作......哦好吧......
I've since abandoned this approach. I don't think DataTemplates are designed to work in this way... oh well...