C# 数据模板不支持';t按规定正确绑定属性
我在DataTemplates.xaml中有以下DataTemplateC# 数据模板不支持';t按规定正确绑定属性,c#,wpf,xaml,datatemplate,C#,Wpf,Xaml,Datatemplate,我在DataTemplates.xaml中有以下DataTemplate <DataTemplate DataType="{x:Type local:ExcelReportVM}"> <local:ExcelReport DoubleClickHandler="{Binding}"> <local:ExcelReport.RowColorConverter> <local:ReportRowColorCon
<DataTemplate DataType="{x:Type local:ExcelReportVM}">
<local:ExcelReport DoubleClickHandler="{Binding}">
<local:ExcelReport.RowColorConverter>
<local:ReportRowColorConverter/>
</local:ExcelReport.RowColorConverter>
</local:ExcelReport>
</DataTemplate>
<Syncfusion:RibbonWindow x:Class="WpfApplication1.MainWindow"
...
xmlns:local="clr-namespace:WpfApplication1"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525" WindowState="Maximized"
Syncfusion:SkinStorage.VisualStyle="Office2013"
xmlns:Syncfusion="http://schemas.syncfusion.com/wpf">
<Grid x:Name="ExcelReport22">
<ContentControl> <!-- this is where the ExcelReportVM is binded to -->
<local:ExcelReportVM/>
</ContentControl>
</Grid>
</Syncfusion:RibbonWindow>
我不知道为什么我可以成功绑定DataContext,但是我定义的
没有使用,我认为DataTemplates.xaml中的所有内容都可以访问ExcelReport.xaml
注意:输出窗口中没有错误,代码中任何地方都没有抛出异常 如果它是一个ContentControl
,那么你必须将它的内容设置为
,然后根据SomeProperty的数据类型
选择正确的数据模板
。如果它是ContentControl
,那么你必须将它的内容
设置为
,然后根据SomeProperty
的DataType
选择正确的DataTemplate
。这就是我解决问题的方法,我不会在ExcelReport.xaml中将UserControl.DataContext
设置为ExcelReportVM
;相反,我在main window.xaml中将ContentControl.Content
设置为ExcelReportVM
更新了ExcelReport.xaml上的代码:
<UserControl x:Class="WpfApplication1.ExcelReport"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
....
xmlns:syncfusion="http://schemas.syncfusion.com/wpf"
d:DesignHeight="300" d:DesignWidth="300">
<UserControl.Resources>
<DataTemplate x:Key="HeaderTemplate">
<TextBlock TextAlignment="Center" Text="{Binding .}" TextWrapping="WrapWithOverflow" />
</DataTemplate>
</UserControl.Resources>
<UserControl.DataContext>
<local:ExcelReportVM/>
</UserControl.DataContext>
<syncfusion:SfDataGrid ItemsSource="{Binding Entries}" x:Name="grid" Background="White"
HeaderTemplate="{StaticResource HeaderTemplate}"
SelectedItem="{Binding SelectedItem, RelativeSource={RelativeSource AncestorType=UserControl}}">
</syncfusion:SfDataGrid>
</UserControl>
<UserControl x:Class="WpfApplication1.ExcelReport"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
....
xmlns:syncfusion="http://schemas.syncfusion.com/wpf"
d:DesignHeight="300" d:DesignWidth="300">
<UserControl.Resources>
<DataTemplate x:Key="HeaderTemplate">
<TextBlock TextAlignment="Center" Text="{Binding .}" TextWrapping="WrapWithOverflow" />
</DataTemplate>
</UserControl.Resources>
<!-- <UserControl.DataContext> this is not longer needed
<local:ExcelReportVM/>
</UserControl.DataContext> -->
<syncfusion:SfDataGrid ItemsSource="{Binding Entries}" x:Name="grid" Background="White"
HeaderTemplate="{StaticResource HeaderTemplate}"
SelectedItem="{Binding SelectedItem, RelativeSource={RelativeSource AncestorType=UserControl}}">
</syncfusion:SfDataGrid>
</UserControl>
代码位于主窗口.xaml
<DataTemplate DataType="{x:Type local:ExcelReportVM}">
<local:ExcelReport DoubleClickHandler="{Binding}">
<local:ExcelReport.RowColorConverter>
<local:ReportRowColorConverter/>
</local:ExcelReport.RowColorConverter>
</local:ExcelReport>
</DataTemplate>
<Syncfusion:RibbonWindow x:Class="WpfApplication1.MainWindow"
...
xmlns:local="clr-namespace:WpfApplication1"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525" WindowState="Maximized"
Syncfusion:SkinStorage.VisualStyle="Office2013"
xmlns:Syncfusion="http://schemas.syncfusion.com/wpf">
<Grid x:Name="ExcelReport22">
<ContentControl> <!-- this is where the ExcelReportVM is binded to -->
<local:ExcelReportVM/>
</ContentControl>
</Grid>
</Syncfusion:RibbonWindow>
这就是我解决问题的方法,我没有在ExcelReport.xaml中将UserControl.DataContext
设置为ExcelReportVM
;相反,我在main window.xaml中将ContentControl.Content
设置为ExcelReportVM
更新了ExcelReport.xaml上的代码:
<UserControl x:Class="WpfApplication1.ExcelReport"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
....
xmlns:syncfusion="http://schemas.syncfusion.com/wpf"
d:DesignHeight="300" d:DesignWidth="300">
<UserControl.Resources>
<DataTemplate x:Key="HeaderTemplate">
<TextBlock TextAlignment="Center" Text="{Binding .}" TextWrapping="WrapWithOverflow" />
</DataTemplate>
</UserControl.Resources>
<UserControl.DataContext>
<local:ExcelReportVM/>
</UserControl.DataContext>
<syncfusion:SfDataGrid ItemsSource="{Binding Entries}" x:Name="grid" Background="White"
HeaderTemplate="{StaticResource HeaderTemplate}"
SelectedItem="{Binding SelectedItem, RelativeSource={RelativeSource AncestorType=UserControl}}">
</syncfusion:SfDataGrid>
</UserControl>
<UserControl x:Class="WpfApplication1.ExcelReport"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
....
xmlns:syncfusion="http://schemas.syncfusion.com/wpf"
d:DesignHeight="300" d:DesignWidth="300">
<UserControl.Resources>
<DataTemplate x:Key="HeaderTemplate">
<TextBlock TextAlignment="Center" Text="{Binding .}" TextWrapping="WrapWithOverflow" />
</DataTemplate>
</UserControl.Resources>
<!-- <UserControl.DataContext> this is not longer needed
<local:ExcelReportVM/>
</UserControl.DataContext> -->
<syncfusion:SfDataGrid ItemsSource="{Binding Entries}" x:Name="grid" Background="White"
HeaderTemplate="{StaticResource HeaderTemplate}"
SelectedItem="{Binding SelectedItem, RelativeSource={RelativeSource AncestorType=UserControl}}">
</syncfusion:SfDataGrid>
</UserControl>
代码位于主窗口.xaml
<DataTemplate DataType="{x:Type local:ExcelReportVM}">
<local:ExcelReport DoubleClickHandler="{Binding}">
<local:ExcelReport.RowColorConverter>
<local:ReportRowColorConverter/>
</local:ExcelReport.RowColorConverter>
</local:ExcelReport>
</DataTemplate>
<Syncfusion:RibbonWindow x:Class="WpfApplication1.MainWindow"
...
xmlns:local="clr-namespace:WpfApplication1"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525" WindowState="Maximized"
Syncfusion:SkinStorage.VisualStyle="Office2013"
xmlns:Syncfusion="http://schemas.syncfusion.com/wpf">
<Grid x:Name="ExcelReport22">
<ContentControl> <!-- this is where the ExcelReportVM is binded to -->
<local:ExcelReportVM/>
</ContentControl>
</Grid>
</Syncfusion:RibbonWindow>
运行此命令时,您的输出窗口中是否有任何错误?@JBrooks,没有,根本没有错误。您在哪里使用此命令?在您当前的代码中,我看不到这一点。@AnjumSKhan,它在DataTemplates.xaml
中,这是第一个代码block@Graviton我是说你在哪里使用它?当你运行这个时,你的输出窗口有错误吗?@JBrooks,没有,一点错误都没有。你在哪里使用这个
?在您当前的代码中,我看不到这一点。@AnjumSKhan,它在DataTemplates.xaml
中,这是第一个代码block@Graviton我是说你在哪里使用它?“{Binding SomeProperty
--什么是正确的?你能在代码中更明确一些吗?@GravitonSomeProperty
通常是ViewModel
,它的类型应该匹配local:ExcelReportVM
”{Binding SomeProperty
--什么是正确的?你能在代码中更加明确吗?@GravitonSomeProperty
通常是ViewModel
,它的类型应该匹配local:ExcelReportVM
。