使用样式在listview itemtemplate中绑定数据

时间:2013-09-07 09:54:49

标签: c# windows listview data-binding windows-store-apps

我无法将我的示例数据绑定到stackpanel中的textblocks,这是我在资源中定义的。我认为我以错误的方式使用样式,因为我收到toString()方法而不是类绑定字段。

这是我定义风格的资源:

    <UserControl.Resources>
    <ItemsPanelTemplate x:Key="VirtualizingStackPanelTemplate">
        <VirtualizingStackPanel Orientation="Horizontal" />
    </ItemsPanelTemplate>
    <ListView x:Key="ListBoxTemplate" HorizontalAlignment="Left" ScrollViewer.HorizontalScrollBarVisibility="Visible">
        <ListView.ItemTemplate>
            <DataTemplate>
                <!--<ListBoxItem Background="DarkOrchid" Margin="1,1, 5,5" Height="400" HorizontalContentAlignment="Stretch" HorizontalAlignment="Stretch">-->
                    <StackPanel>
                        <TextBlock FontSize="30" Text="{Binding Title}"/>
                        <TextBlock FontSize="20" Text="{Binding Desc}"/>
                    </StackPanel>
                <!--</ListBoxItem>-->
            </DataTemplate>
            </ListView.ItemTemplate>
    </ListView>
</UserControl.Resources>

这是我以编程方式添加listview的方法:

        long rowCount = ContentGridFullView.RowDefinitions.LongCount();
        if (rowCount > 8) return;
        var c1 = new RowDefinition { Height = new GridLength(1, GridUnitType.Star) };
        ContentGridFullView.RowDefinitions.Add(c1);
        rowCount = ContentGridFullView.RowDefinitions.LongCount();
        TextBlock tb = new TextBlock {Text = "TEXTBLOCK ITEM = " + (rowCount - 1), FontSize = 40};
        Viewbox vb = new Viewbox { Child = tb };
        if (rowCount > 8) return;
        Grid.SetRow(vb, Convert.ToInt32(rowCount-1));
        Grid.SetColumn(vb, 1);
        ListView lb = new ListView();
       lb.Style = Resources["ListBoxTemplate"] as Style;
       lb.ItemsPanel = (ItemsPanelTemplate) Resources["VirtualizingStackPanelTemplate"];
       var products = new ObservableCollection<Product>() { new Product("ASDASDSADAS", "VCBVCBVCBVCBC"), new Product("ASDASDSADAS", "VCBVCBVCBVCBC"), new Product("ASDASDSADAS", "VCBVCBVCBVCBC"), new Product("ASDASDSADAS", "VCBVCBVCBVCBC") };
       lb.ItemsSource = products;
       ContentGridFullView.Children.Add(lb);
       ContentGridFullView.Children.Add(vb);
       Grid.SetRow(lb, Convert.ToInt32(rowCount - 1));
       Grid.SetColumn(lb, 2);

我想要绑定的短课:

public class Product
{
    public string Title { get; set; }
    public string Desc { get; set; }

    public Product(string title, string desc)
    {
        Title = title;
        Desc = desc;
    }

    public override string ToString()
    {
        return "I see that message instead of Title and Desc";
    }
}

有人能告诉我这段代码有什么问题吗?谢谢。

1 个答案:

答案 0 :(得分:0)

将Observable集合创建为属性(getter / setter):

ObservableCollection<Product> _products;
public ObservableCollection<Product> products
{
    get{return  _products;}
    set
    {
        _products=value;
        PropertyChanged("products");
    }
}

属性更改事件将需要指示集合已更改,当您使用ObservableCollection时需要它。您需要了解更多信息。您可以使用以下方法将产品添加到产品系列中:

        products.Add(Product_object)

您的xaml代码将具有itemsSource,如下所示:

<ListView x:Key="ListBoxTemplate" HorizontalAlignment="Left" ScrollViewer.HorizontalScrollBarVisibility="Visible" ItemsSource="{Binding products}">
    <ListView.ItemTemplate>
        <DataTemplate>
            <!--<ListBoxItem Background="DarkOrchid" Margin="1,1, 5,5" Height="400" HorizontalContentAlignment="Stretch" HorizontalAlignment="Stretch">-->
                <StackPanel>
                    <TextBlock FontSize="30" Text="{Binding Title}"/>
                    <TextBlock FontSize="20" Text="{Binding Desc}"/>
                </StackPanel>
            <!--</ListBoxItem>-->
        </DataTemplate>
        </ListView.ItemTemplate>
</ListView>

以下语句在您的xaml代码中很重要,这样您的xaml代码就会知道在哪里查找数据。

DataContext="{Binding RelativeSource={RelativeSource Self}, Path=x}

首先尝试创建静态列表并检查数据是否正确初始化,然后您可以尝试动态创建列表视图。但是上面的代码将与您创建动态列表视图一样。