WPF GridView导出为CSV?

时间:2013-06-27 10:14:26

标签: c# wpf

我有一个ListView,其GridView为其视图。

请参阅下面的示例代码:

<ListView ItemsSource="{Binding Path=Employees}" >
    <ListView.ItemContainerStyle>
        <Style TargetType="{x:Type ListViewItem}">
            <Setter Property="Focusable" Value="False"/>
        </Style>
    </ListView.ItemContainerStyle>

    <ListView.Resources>
        <Style TargetType="{x:Type CheckBox}" x:Key="DataGridCheckBox">
            <Setter Property="HorizontalAlignment" Value="Center" />
            <Setter Property="HorizontalContentAlignment" Value="Center" />
            <Setter Property="IsEnabled" Value="True" />
            <Setter Property="Margin" Value="4" />
            <Setter Property="VerticalAlignment" Value="Center" />
            <Setter Property="VerticalContentAlignment" Value="Center" />
            <Setter Property="Width" Value="{Binding RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type GridViewColumn}},Path=ActualWidth}" />
        </Style>
    </ListView.Resources>

    <ListView.View>
        <GridView x:Name="EmployeesGridView">
            <GridViewColumn Header="IsEligible">
                <GridViewColumn.CellTemplate>
                    <DataTemplate>
                        <CheckBox Style="{StaticResource DataGridCheckBox}" IsChecked="{Binding Path=IsSelected}"/>
                    </DataTemplate>
                </GridViewColumn.CellTemplate>
            </GridViewColumn>

            <GridViewColumn Header="Name" >
                <GridViewColumn.CellTemplate>
                    <DataTemplate>
                        <TextBlock>
                            <TextBlock>
                                <TextBlock Text="{Binding Path=Employee.Name}"/>
                            </TextBlock>
                        </TextBlock>
                    </DataTemplate>
                </GridViewColumn.CellTemplate>
            </GridViewColumn>

            <GridViewColumn Header="Age">
                <GridViewColumn.CellTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding Age}"/>
                    </DataTemplate>
                </GridViewColumn.CellTemplate>
            </GridViewColumn>
        </GridView>
    </ListView.View>
</ListView>

如何将此数据导出到CSV文件。我没有得到任何我可以在GridView中出现行的属性。

我该怎么做?请建议。

1 个答案:

答案 0 :(得分:-1)

您可以在SelectedPerson中创建ViewModel媒体资源,并将ListView.SelectedItem绑定到此媒体资源。