查询SQL Server CE到列表框(Windows phone)

本文关键字:Windows phone 列表 SQL Server CE 查询 | 更新日期: 2023-09-27 18:05:43

我有这样的代码:

public void mostrar(ListBox medicinas_visual) {
    var query = from Medicamento in usuario_data.Current.Medicamento
                orderby Medicamento.id_med
                select new {Medicamento.id_med, Medicamento.nombre};
    medicinas_visual.ItemsSource = query.ToString();
}

当执行代码返回:{id_med = 1 nombre= Prepilon}所有字符串查询,当我想返回值

我的列表框的代码XAML

ListBox x:Name="medicinas_visual" Margin="0,56,0,4" FontFamily="/LI_health;component/Fonts/Fonts.zip#Helvetica S" FontSize="24" HorizontalAlignment="Left" Width="166"/

查询SQL Server CE到列表框(Windows phone)

实现这个。这应该可以工作:它将显示id_med和nombre到列表框

XAML:

<ListBox x:Name="medicinas_visual" Margin="0,56,0,4" FontFamily="/LI_health;component/Fonts/Fonts.zip#Helvetica S" FontSize="24" HorizontalAlignment="Left" Width="166">
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <StackPanel Orientation="Horizontal">
                            <TextBlock Text="{Binding id}"></TextBlock>
                            <TextBlock Text="{Binding data}"></TextBlock>
                        </StackPanel>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>
CS:

public void mostrar(ListBox medicinas_visual)
{
    List<Data> objList = new List<Data>();
    var query = from Medicamento in usuario_data.Current.Medicamento
                orderby Medicamento.id_med
                select Medicamento;
    foreach (var item in query)
    {
        objList.Add(new Data(item.id_med, item.nombre));
    }
    medicinas_visual.ItemsSource = objList;
}
public class Data
{
    public int id { get; set; }
    public string data { get; set; }
    public Data(){ }
    public Data(int id, string data)
    {
        this.id = id;
        this.data = data;
    }
}

尝试用XAML或c#:

为列表框添加DisplayMemberPath标签
listBox.DisplayMember = "nombre";
http://msdn.microsoft.com/en-us/library/cc265158%28v=vs.95%29.aspx

我不知道如何工作XAML,但我看到你传递列表框和添加itemssource后,你需要使用引用值传递列表框,试试这个:

public void mostrar(ref ListBox medicinas_visual) {
    var query = from Medicamento in usuario_data.Current.Medicamento
                orderby Medicamento.id_med
                select new {Medicamento.id_med, Medicamento.nombre};
    medicinas_visual.ItemsSource = query.ToString();
}