WPF TabControl ContentTemplate不';t在Windows XP上的渲染与Windows 7相同
我有一个TabControl,看起来像这样:WPF TabControl ContentTemplate不';t在Windows XP上的渲染与Windows 7相同,wpf,wpf-4.0,Wpf,Wpf 4.0,我有一个TabControl,看起来像这样: <TabControl x:Name="tabPlaintiffs" ItemsSource="{Binding CivilPaper.Plaintiffs}" ContentTemplate="{DynamicResource PersonTemplate}" SelectedItem="{Binding SelectedPerson, Co
<TabControl x:Name="tabPlaintiffs"
ItemsSource="{Binding CivilPaper.Plaintiffs}"
ContentTemplate="{DynamicResource PersonTemplate}"
SelectedItem="{Binding SelectedPerson, Converter={StaticResource PersonRoleToPerson}, Mode=OneWayToSource}"
Margin="5" />
<DataTemplate x:Key="PersonTemplate">
<Grid Background="#FF4EFF00">
</Grid>
</DataTemplate>
ContentTemplate如下所示:
<TabControl x:Name="tabPlaintiffs"
ItemsSource="{Binding CivilPaper.Plaintiffs}"
ContentTemplate="{DynamicResource PersonTemplate}"
SelectedItem="{Binding SelectedPerson, Converter={StaticResource PersonRoleToPerson}, Mode=OneWayToSource}"
Margin="5" />
<DataTemplate x:Key="PersonTemplate">
<Grid Background="#FF4EFF00">
</Grid>
</DataTemplate>
在Windows 7上,它的渲染方式如下:
在Windows XP SP3上,它呈现(或实际上不呈现!)如下所示:
是什么造成了这种差异
编辑:删除了ItemsTemplate并删除了ContentTemplate中的所有数据绑定以排除各种情况。对于任何人来说,即使是熟悉代码的人(即您)也很难在这么多代码中找到bug 我建议您通过以下方式调试应用程序:
最终,Windows 7处理ValueConverter的方式与Windows XP不同。如果我删除:
SelectedItem="{Binding SelectedPerson, Converter={StaticResource PersonRoleToPerson}, Mode=OneWayToSource}"
然后我得到了所有合适的模板。问题出在ValueConverter中,尽管Windows XP多次启动它,而Windows 7在底层集合通知其更改时只启动了一次(应该如此!)
我的ValueConverter最初在Convert和ConvertBack中都返回null(因为它是OneWayToSource,我只需要ConvertBack)。如果任一方法都不满足对象的类型检查,则返回null。我现在认为这是一种不好的做法。相反,如果您一路遇到以下情况,它最终应返回DependencyProperty.UnsetValue:
public class PlaintiffRoleToPerson : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return DependencyProperty.UnsetValue;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
var plaintiff = value as Plaintiff;
if (plaintiff != null)
{
return plaintiff.Person;
}
return DependencyProperty.UnsetValue;
}
}
为什么Windows7的处理方式与WindowsXP不同仍然是一个谜,但我现在一切正常。我正在倒计时,直到Windows XP的支持结束,我可以从我们支持的操作系统中删除它。要点。。。我想要完整性,但它太多了,不能指望人们看穿它。我把它编辑成了一个小得多的片段。我已经在两台机器上验证了相同的运行时。如果我删除SelectedItem=“{Binding SelectedPerson,Converter={StaticResource PersonRoleToPerson},Mode=OneWayToSource}”,它就会工作。这是静态资源还是动态资源?为什么这在XP上会有所不同?ValueConverter在Windows XP上触发2次,但在Windows 7上仅触发一次。荒唐的