如何在WPF/WP7中将字符串列表数据绑定到ListBox
本文关键字:列表 字符串 数据绑定 ListBox WPF WP7 | 更新日期: 2023-09-27 18:25:19
我正在尝试将字符串值的列表绑定到列表框,以便逐行列出它们的值。现在我用这个:
<ListBox Margin="20" ItemsSource="{Binding Path=PersonNames}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Path=Id}"></TextBlock>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
但我不知道应该在文本块中放入什么,而不是Id
,因为它们都是字符串值,而不是自定义类。
此外,它还抱怨当我在MainPage.PersonNames.中有PersonNames时,不必找到它
我将数据上下文设置为:
DataContext="{Binding RelativeSource={RelativeSource Self}}"
我做错了?
如果简单地说,您的ItemsSource绑定如下:
YourListBox.ItemsSource = new List<String> { "One", "Two", "Three" };
你的XAML应该看起来像:
<ListBox Margin="20" Name="YourListBox">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding}" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
更新:
这是使用DataContext时的解决方案。以下代码是您将传递给页面的DataContext的视图模型和DataContext:的设置
public class MyViewModel
{
public List<String> Items
{
get { return new List<String> { "One", "Two", "Three" }; }
}
}
//This can be done in the Loaded event of the page:
DataContext = new MyViewModel();
你的XAML现在看起来是这样的:
<ListBox Margin="20" ItemsSource="{Binding Items}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding}" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
这种方法的优点是,您可以在MyViewModel类中放入更多的属性或复杂对象,并在XAML中提取它们。例如传递人员对象列表:
public class ViewModel
{
public List<Person> Items
{
get
{
return new List<Person>
{
new Person { Name = "P1", Age = 1 },
new Person { Name = "P2", Age = 2 }
};
}
}
}
public class Person
{
public string Name { get; set; }
public int Age { get; set; }
}
和XAML:
<ListBox Margin="20" ItemsSource="{Binding Items}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Path=Name}" />
<TextBlock Text="{Binding Path=Age}" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
你应该向我们展示PersonNames的代码,我不确定我是否理解你的问题,但也许你想这样绑定它:
<TextBlock Text="{Binding Path=.}"/>
或
<TextBlock Text="{Binding}"/>
这将绑定到列表中的当前元素(假设PersonNames是字符串列表)。否则,您将在列表中看到类名。
如果项源可枚举为字符串项,请使用以下内容:
<TextBlock Text="{Binding}"></TextBlock>
您可以在任何对象上使用此语法。通常,ToString()-方法会被调用以获取值。这在很多情况下都非常方便。但请注意,不会发生更改通知。
您可以做到这一点,而无需将TextBlock控件明确定义为ListBox的一部分(除非您想要更好的格式)。使绑定触发的技巧是使用ObservableCollection<string>
而不是List<string>
Window1.xaml
<ListView Width="250" Height="50" ItemsSource="{Binding MyListViewBinding}"/>
Window1.xaml.cs
public Window1()
{
InitializeComponent();
DataContext = this;
// Need to initialize this, otherwise you get a null exception
MyListViewBinding = new ObservableCollection<string>();
}
public ObservableCollection<string> MyListViewBinding { get; set; }
// Add an item to the list
private void Button_Click_Add(object sender, RoutedEventArgs e)
{
// Custom control for entering a single string
SingleEntryDialog _Dlg = new SingleEntryDialog();
// OutputBox is a string property of the custom control
if ((bool)_Dlg.ShowDialog())
MyListViewBinding.Add(_Dlg.OutputBox.Trim());
}