将数据(不仅是文本)添加到多列ListView(WPF)

时间:2011-06-23 09:20:32

标签: c# wpf xaml listview

我正在使用C#(.NET 4.0)中的WPF应用程序,我有一个带有GridView的ListView,它有两列。

我动态地想要添加行(在代码中)。我的困境是只有第一列会添加常规文本。第二列将包含一个包含带有TextBlocks的多列Grid的对象。 (见链接http://imageshack.us/photo/my-images/803/listview.png/

如果我想做你想在所有列中输入文本时通常会做的事情(即DisplayMemberBinding),我在第二列中得到的是文本“System.Windows.Grid”,这显然不是我的意思想。

作为参考,如果我只是尝试使用代码listView1.Items.Add(grid1)(不使用DisplayMemberBinding)添加Grid对象(使用TextBlocks),则对象仅被添加到第二列(第一列是空白)而不是它通常如何与文本一起使用,其中相同的文本在所有列中结束。

我希望我的问题足够详细,对此的任何帮助都会非常感激。

编辑:

我已经尝试了以下代码,但每次单击按钮添加新行时,每一行都会使用相同的datatemplate进行更新。 (即第二个列始终在每一行显示相同的数据。)

XAML:

<Window x:Class="TEST.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Name="AAA"  Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded">
<Grid Name="grid1">
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="374*" />
        <ColumnDefinition Width="129*" />
    </Grid.ColumnDefinitions>
    <Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="21,12,0,0" Name="button1" VerticalAlignment="Top" Width="75" Grid.Column="1" Click="button1_Click" />
</Grid>

代码:

            public partial class MainWindow : Window
{

    ListView listView1 = new ListView();
    GridViewColumn viewCol2 = new GridViewColumn();

    public MainWindow()
    {
        InitializeComponent();

        Style style = new Style(typeof(ListViewItem));
        style.Setters.Add(new Setter(ListViewItem.HorizontalContentAlignmentProperty,
            HorizontalAlignment.Stretch));
        listView1.ItemContainerStyle = style;

        GridView gridView1 = new GridView();
        listView1.View = gridView1;
        GridViewColumn viewCol1 = new GridViewColumn();
        viewCol1.Header = "Option";
        gridView1.Columns.Add(viewCol1);
        viewCol2.Header = "Value";
        gridView1.Columns.Add(viewCol2);
        grid1.Children.Add(listView1);
        viewCol1.DisplayMemberBinding = new Binding("Option");
    }

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {

    }

    private void button1_Click(object sender, RoutedEventArgs e)
    {
        DataTemplate dataTemplate = new DataTemplate();

        FrameworkElementFactory spFactory = new FrameworkElementFactory(typeof(Grid));

        Random random = new Random();
        int cols = random.Next(1, 6);
        int full = 100;
        for (int i = 0; i < cols; i++)
        {
            FrameworkElementFactory col1 = new FrameworkElementFactory(typeof(ColumnDefinition));
            int partWidth = random.Next(0, full);
            full -= partWidth;
            col1.SetValue(ColumnDefinition.WidthProperty, new GridLength(partWidth, GridUnitType.Star));
            spFactory.AppendChild(col1);
        }
        if (full > 0)
        {
            FrameworkElementFactory col1 = new FrameworkElementFactory(typeof(ColumnDefinition));
            col1.SetValue(ColumnDefinition.WidthProperty, new GridLength(full, GridUnitType.Star));
            spFactory.AppendChild(col1);
        }
        for (int i = 0; i < cols; i++)
        {
            FrameworkElementFactory text1 = new FrameworkElementFactory(typeof(TextBlock));
            SolidColorBrush sb1 = new SolidColorBrush();
            switch (i)
            {
                case 0:
                    sb1.Color = Colors.Blue;
                    break;
                case 1:
                    sb1.Color = Colors.Red;
                    break;
                case 2:
                    sb1.Color = Colors.Yellow;
                    break;
                case 3:
                    sb1.Color = Colors.Green;
                    break;
                case 4:
                    sb1.Color = Colors.Purple;
                    break;
                case 5:
                    sb1.Color = Colors.Pink;
                    break;
                case 6:
                    sb1.Color = Colors.Brown;
                    break;
            }
            text1.SetValue(TextBlock.BackgroundProperty, sb1);
            text1.SetValue(Grid.ColumnProperty, i);
            spFactory.AppendChild(text1);
        }
        if (full > 0)
        {
            FrameworkElementFactory text1 = new FrameworkElementFactory(typeof(TextBlock));
            SolidColorBrush sb1 = new SolidColorBrush(Colors.Black);
            text1.SetValue(TextBlock.BackgroundProperty, sb1);
            text1.SetValue(Grid.ColumnProperty, cols);
            spFactory.AppendChild(text1);
        }

        dataTemplate.VisualTree = spFactory;

        viewCol2.CellTemplate = dataTemplate;


        int rows = listView1.Items.Count + 1;
        listView1.Items.Add(new { Option = "Row " + rows });

    }
}

1 个答案:

答案 0 :(得分:0)

您不必使用DisplayMemberBinding,对于可以使用CellTemplate的复杂内容,例如:

<ListView ItemsSource="{Binding Data}">
    <ListView.View>
        <GridView>
            <GridViewColumn DisplayMemberBinding="{Binding Name}" />
            <GridViewColumn>
                <GridViewColumn.CellTemplate>
                    <DataTemplate>
                        <!-- This should display your grid if it is a property on your item called GridProperty -->
                        <ContentControl Content="{Binding GridProperty}" />
                    </DataTemplate>
                </GridViewColumn.CellTemplate>
            </GridViewColumn>
        </GridView>
    </ListView.View>
</ListView>