C# 使用样式在listview itemtemplate中绑定数据
我无法将示例数据绑定到stackpanel中的文本块,这是我在参考资料中定义的。我认为我使用样式的方式是错误的,因为我接收的是toString()方法,而不是类绑定的字段 这是我定义样式的资源:C# 使用样式在listview itemtemplate中绑定数据,c#,windows,listview,data-binding,windows-store-apps,C#,Windows,Listview,Data Binding,Windows Store Apps,我无法将示例数据绑定到stackpanel中的文本块,这是我在参考资料中定义的。我认为我使用样式的方式是错误的,因为我接收的是toString()方法,而不是类绑定的字段 这是我定义样式的资源: <UserControl.Resources> <ItemsPanelTemplate x:Key="VirtualizingStackPanelTemplate"> <VirtualizingStackPanel Orientation="
<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>
有人能告诉我这个代码有什么问题吗?谢谢。将可观察集合创建为属性(getter/setter): 您的xaml代码将具有如下项源:
<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>
首先尝试创建一个静态列表,检查数据是否正确初始化,然后可以尝试动态创建ListView。但是上面的代码与创建动态ListView的操作相同。首先,为什么要在usercontrol.resources中创建ListView?看起来你在使用代码隐藏来设置数据,对吗?如果您使用代码隐藏,您的用户控件应该能够知道数据存在于代码隐藏本身中。您应该使用:DataContext=“{Binding RelativeSource={RelativeSource Self},Path=x}”,其中x是可观察集合的名称。你必须研究MVVM设计模式,你的代码变得更有条理,你会喜欢它的。我正在我的代码中动态创建集合和列表视图(点击按钮中的事件创建新的列表视图及其集合)。我想要一种样式的所有创建的ListView。我无法将集合绑定到listview,因为我没有在xaml中创建任何listview。有没有办法通过编程方式创建ListView及其列表,并将所有创建的列表设置为相同的样式?我尝试过这个方法,但在我的应用程序中不起作用。Listview为空。您的类是否扩展了INotifyPropertyChanged?是。顺便说一句,我不能使用一个参数方法PropertyChanged。
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";
}
}
ObservableCollection<Product> _products;
public ObservableCollection<Product> products
{
get{return _products;}
set
{
_products=value;
PropertyChanged("products");
}
}
products.Add(Product_object)
<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>
DataContext="{Binding RelativeSource={RelativeSource Self}, Path=x}