我在读取由我无法控制的Perl脚本编写的.xls文件时遇到问题.这些文件包含单元格内的一些格式和换行符.
filename = '/home/shared/testfile.xls'
book = xlrd.open_workbook(filename)
sheet = book.sheet_by_index(0)
for rowIndex in xrange(1, sheet.nrows):
row = sheet.row(rowIndex)
Run Code Online (Sandbox Code Playgroud)
这会引发以下错误:
_locate_stream(Workbook): seen
0 5 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4
20 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4
172480= 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 …Run Code Online (Sandbox Code Playgroud) 我有两个具有不同属性的类,但都继承了其他一些基类:
public class BaseClass { }
public class ClassA : BaseClass
{
public string PropertyA { get; set; }
}
public class ClassB : BaseClass
{
public string PropertyB { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
代码隐藏:
public ObservableCollection<BaseClass> Items { get; set; }
public MainWindow()
{
Items = new ObservableCollection<BaseClass>
{
new ClassA {PropertyA = "A"},
new ClassB {PropertyB = "B"}
};
}
Run Code Online (Sandbox Code Playgroud)
我的XAML看起来像这样:
<ListView ItemsSource="{Binding Items}">
<ListView.View>
<GridView>
<GridViewColumn DisplayMemberBinding="{Binding PropertyA, FallbackValue=''}"/>
<GridViewColumn DisplayMemberBinding="{Binding PropertyB, FallbackValue={x:Null}}"/>
</GridView>
</ListView.View>
</ListView>
Run Code Online (Sandbox Code Playgroud)
在调试模式下运行时,输出窗口显示: …
<ListView ItemsSource="{Binding MyData}">
<ListView.View>
<GridView>
<GridViewColumn Header="col1" DisplayMemberBinding="{Binding Path=value1}">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock TextAlignment="Right" Text="{Binding Path=value1}"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="col2">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock TextAlignment="Center" Text="{Binding Path=value2}"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="col3" DisplayMemberBinding="{Binding Path=value3}"/>
</GridView>
</ListView.View>
<ListView ItemsSource="{Binding MyData}">
Run Code Online (Sandbox Code Playgroud)
col1应该是右对齐的.(不工作)
col2应该是居中对齐的.(工作)
col3应该是左对齐的.(工作)
DisplayMemberBinding是否有理由重写CellTemplate?如果是这样,是否有一个修复(仍然使用DisplayMemberBinding)?
编辑:我结束了实施像这样:
<Window xmlns:util="clr-namespace:TestProject.Util">
<Window.Resources>
<Style TargetType="ListViewItem">
<Setter Property="HorizontalContentAlignment" Value="Stretch"/>
</Style>
<Style TargetType="GridViewColumnHeader">
<Setter Property="HorizontalContentAlignment" Value="Left"/>
</Style>
<DataTemplate x:Key="value1Template">
<TextBlock TextAlignment="Right" Text="{Binding Path=value1}"/>
</DataTemplate>
<DataTemplate x:Key="value2Template">
<TextBlock TextAlignment="Right" Text="{Binding Path=value2}"/>
</DataTemplate>
</Window.Resources>
<Grid>
<ListView ItemsSource="{Binding MyData}" IsSynchronizedWithCurrentItem="True" …Run Code Online (Sandbox Code Playgroud)