C# WPF绑定和上下文
我在绑定列标题的代码隐藏字符串属性时遇到问题。运行应用程序时,标头始终为空字符串C# WPF绑定和上下文,c#,wpf,data-binding,C#,Wpf,Data Binding,我在绑定列标题的代码隐藏字符串属性时遇到问题。运行应用程序时,标头始终为空字符串 <UserControl [...] DataContext="{Binding RelativeSource={RelativeSource Self}}" > <c:DataGrid Name="m_dataGrid" ItemsSource="{Binding Configurations}" >
<UserControl [...]
DataContext="{Binding RelativeSource={RelativeSource Self}}" >
<c:DataGrid Name="m_dataGrid"
ItemsSource="{Binding Configurations}" >
<c:DataGrid.Columns>
<!-- Column 'Importieren/Exportieren' -->
<c:DataGridTemplateColumn Width="Auto"
MinWidth="100">
<c:DataGridTemplateColumn.HeaderTemplate>
<DataTemplate>
<CheckBox Name="m_checkBoxExportAllDefinitions"
Content="{Binding ImportExportColumnHeader, Mode=OneWay}"/>
</DataTemplate>
</c:DataGridTemplateColumn.HeaderTemplate>
<c:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<CheckBox IsChecked="{Binding IsDefinitionExportEnabled, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
</DataTemplate>
</c:DataGridTemplateColumn.CellTemplate>
</c:DataGridTemplateColumn>
</c:DataGrid>
</UserControl>
在用于绑定标题文本的代码隐藏属性(在HeaderTemplate
中定义)中设置断点时,永远不会调用getter:
public string ImportExportColumnHeader {
get {
return IsImport ? ErgaenzungsfelderResources.ImportExportSelectionControlImportierenColumnHeader :
ErgaenzungsfelderResources.ImportExportSelectionControlExportierenColumnHeader;
}
}
CellTemplate
到的绑定是DefinitionExportEnabled
工作。这是包含在ImportExportConfiguration
类中的属性,而ImportExportColumnHeader
不是
我假设wpf试图从不存在的ImportExportConfiguration
中获取ImportExportColumnHeader
属性;这就是它显示空标题的原因。这是正确的吗
如何访问代码隐藏属性?指定正确的源代码解决了我的问题:
<CheckBox Name="m_checkBoxExportAllDefinitions"
Content="{Binding ImportExportColumnHeader, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type importExport:ImportExportSelectionControl}}}" />
usercontrol的dataContext似乎被设置为自身。请删除datacontext并重试。同时检查输出窗口以进行绑定errors@user1我希望以这种方式绑定它,以便能够绑定到代码隐藏属性。删除DataContext
会断开CellTemplate
的绑定。我认为出现这个问题是因为我想将HeaderTemplate(对于所有绑定项都是相同的)绑定到代码隐藏属性和CellTemplate
(对于所有绑定项都是不同的)在配置
@user1中包含的属性上,outupt窗口不会显示任何绑定错误。在这种情况下,绑定是正常的。我认为问题在于ImportExportColumnHeader。您可能应该将其作为get/set属性propertychanged@user1我已经在ImportExportColumnHeader
更改时实现了INotifyPropertyChanged
并在PropertyChanged(“ImportExportColumnHeader”)上启用了。标头仍然为空,并且仍然没有调用getter!?
<CheckBox Name="m_checkBoxExportAllDefinitions"
Content="{Binding ImportExportColumnHeader, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type importExport:ImportExportSelectionControl}}}" />