如何将数据库列表绑定到WPF/WP7中的ListBox?

Joa*_*nge 61 .net c# data-binding wpf windows-phone-7

我正在尝试将字符串值列表绑定到列表框,以便逐行列出它们的值.现在我用这个:

<ListBox Margin="20" ItemsSource="{Binding Path=PersonNames}">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="{Binding Path=Id}"></TextBlock>
            </StackPanel>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>
Run Code Online (Sandbox Code Playgroud)

但我不知道我应该把什么放入文本块,而不是Id因为它们都是字符串值,而不是自定义类.

它还抱怨当我在MainPage中将它作为MainPage.PersonNames时,不必找到PersonNames.

我将数据上下文设置为:

DataContext="{Binding RelativeSource={RelativeSource Self}}"
Run Code Online (Sandbox Code Playgroud)

我做错了吗?

Abb*_*bas 134

如果简单地说你的ItemsSource是这样绑定的:

YourListBox.ItemsSource = new List<String> { "One", "Two", "Three" };
Run Code Online (Sandbox Code Playgroud)

您的XAML应该如下所示:

<ListBox Margin="20" Name="YourListBox">
    <ListBox.ItemTemplate> 
        <DataTemplate> 
            <StackPanel Orientation="Horizontal"> 
                <TextBlock Text="{Binding}" /> 
            </StackPanel> 
        </DataTemplate> 
    </ListBox.ItemTemplate> 
</ListBox> 
Run Code Online (Sandbox Code Playgroud)

更新:

这是使用DataContext时的解决方案.以下代码是您将传递给页面的DataContext和DataContext的设置的viewmodel:

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();
Run Code Online (Sandbox Code Playgroud)

您的XAML现在看起来像这样:

<ListBox Margin="20" ItemsSource="{Binding Items}">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="{Binding}" />
            </StackPanel>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>
Run Code Online (Sandbox Code Playgroud)

这种方法的优点是您可以在MyViewModel类中放置更多属性或复杂对象,并在XAML中提取它们.例如,传递Person对象列表:

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; }
}
Run Code Online (Sandbox Code Playgroud)

和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>
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助!:)


min*_*dia 16

你应该向我们展示PersonNames的代码,我不确定,我理解你的问题,但也许你想要像这样绑定它:

<TextBlock Text="{Binding Path=.}"/>
Run Code Online (Sandbox Code Playgroud)

要么

<TextBlock Text="{Binding"} />
Run Code Online (Sandbox Code Playgroud)

这将绑定到列表中的当前元素.(假设PersonNames是一个字符串列表).否则,您将在列表中看到类名

  • 双向绑定需要`Path = .` (4认同)

HCL*_*HCL 9

如果items source可枚举为字符串条目,请使用以下命令:

<TextBlock Text="{Binding}"></TextBlock> 
Run Code Online (Sandbox Code Playgroud)

您可以在任何对象上使用此语法.通常,然后调用ToString()方法来获取值.在许多情况下,这非常方便.但请注意,不会发生任何变更通知.


小智 6

您无需将 TextBlock 控件显式定义为列表框的一部分即可执行此操作(除非您想要更好的格式设置)。触发绑定的技巧是使用ObservableCollection<string>代替List<string>

窗口1.xaml

<ListView Width="250" Height="50" ItemsSource="{Binding MyListViewBinding}"/>
Run Code Online (Sandbox Code Playgroud)

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());
}
Run Code Online (Sandbox Code Playgroud)