1

我无法将示例数据绑定到我在资源中定义的堆栈面板中的文本块。我认为我以错误的方式使用样式,因为我收到 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>

这是我以编程方式添加列表视图的方法:

        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";
    }
}

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

4

1 回答 1

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}

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

于 2013-09-07T16:13:42.793 回答