在数据透视项中,ListBox绑定不显示值
本文关键字:绑定 显示 ListBox 数据 透视 | 更新日期: 2023-09-27 18:30:02
Im创建集合以显示在ListBoxTransactions中,并绑定为描述。但结果是,我在ListBoxTransactions.ItemsSource中只有名称集合,但没有值。不能使用ListBox.ItemTemplate
XAML
<phone:PivotItem Header="Journal">
<Grid>
<ListBox Name="ListBoxTransactions">
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Description}" FontSize="35"/>
</StackPanel>
</ListBox>
</Grid>
</phone:PivotItem>
C#
public class TransactHelper
{
public string Description { get; set; }
}
public void ShowTransactions()
{
ListBoxTransactions.Items.Clear();
var transactFulls = _workerDb.GeTransactFull();
var list = new List<TransactHelper>();
foreach (var t in transactFulls)
{
list.Add(new TransactHelper { Description = t.Description });
}
this.ListBoxTransactions.ItemsSource = list; // dont view collection. only name collection
您应该为您的项实现ItemContainerStyle。
<ListBox Name="ListBoxTransactions" ItemContainerStyle="{DynamicResource MyItemStyle}">
<ListBox.Resources>
<Style x:Key="MyItemStyle" TargetType="{x:Type ListBoxItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Description}" FontSize="35"/>
</StackPanel>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ListBox.Resources>
</ListBox>
请注意:不要使用这对
ListBoxTransactions.Items.Clear();
this.ListBoxTransactions.ItemsSource = list;
你需要
ListBoxTransactions.ItemsSource = null;
this.ListBoxTransactions.ItemsSource = list;
或者将您的集合实现为ObservableCollection