【问题标题】:wpf ListBox Binding ObservableCollection show datas all messed upwpf ListBox Binding ObservableCollection 显示数据都搞砸了
【发布时间】:2012-04-05 13:15:29
【问题描述】:

我正在开发一个 Windows 小工具,它将显示来自 sharepoint 的列表 我背后的代码运行良好并从共享点收集数据。 并且我的 xaml 代码应该在列表框中显示列表,但我绑定它的方式显示数据是逐行显示的,并且只是第一项。不知道为什么。

这是我的xml代码

<Page x:Class="TipsList.Page1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Page1" Height="350" Width="525">

    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="Auto"/>
        </Grid.ColumnDefinitions>
        <ListBox Name="ListboxTips" Width="Auto" ItemsSource="{Binding StateTitle}" />
        <StackPanel Grid.Column="1">
            <Button Click="OnLoad">_Load</Button>

        </StackPanel>
    </Grid>

</Page>

这是我的代码:

        //The  Url
        siteUrl = "http://site/SandBox";
        //The Site Context using ClientContext class
        //This will create a Client connection to Web Application
        clientContext = new SPSClient.ClientContext(siteUrl);

        //The Web Site to Open

        clientWeb = clientContext.Web;

        var salesInfoList = clientContext.Web.Lists.GetByTitle("Tips");

        //Define the CAMLQuery this will be used to Query to the List

        SPSClient.CamlQuery Query = new SPSClient.CamlQuery();

        SPSClient.ListItemCollection listData = salesInfoList.GetItems(Query);

        //Now Execute the Query

        var queryResultSaleListItems = clientContext.LoadQuery(listData);

        clientContext.ExecuteQuery();

        //Read the Data into the Object

        var TipsList = from Sales in queryResultSaleListItems
                        select Sales;
        ObservableCollection<Tips> colTips = new ObservableCollection<Tips>();
        //Read Every List Item and Display Data into the DataGrid
        foreach (SPSClient.ListItem item in TipsList)
        {

            var tips = new Tips();
            tips.StateTitle = item.FieldValues.Values.ElementAt(2).ToString();
            tips.ProductName = item.FieldValues.Values.ElementAt(4).ToString();
            tips.Quantity = item.FieldValues.Values.ElementAt(5).ToString();

            colTips.Add(tips);
        }
        ListboxTips.DataContext = colTips;
    }
    catch (Exception ex)
    {
        // Log error (including InnerExceptions!)
        // Handle exception
    }

}

感谢您的帮助,

【问题讨论】:

    标签: wpf binding listbox observablecollection


    【解决方案1】:

    在您的 xaml 代码中,更改

    <ListBox Name="ListboxTips" Width="Auto" ItemsSource="{Binding StateTitle}" />
    

    <ListBox Name="ListboxTips" Width="Auto" ItemsSource="{Binding}">
      <ListBox.ItemTemplate>
        <DataTemplate>
          <Grid>
            <TextBlock Text="{Binding Path=StateTitle}"/>
          </Grid>
        </DataTemplate>
      </ListBox.ItemTemplate>
    </ListBox>
    

    祝你好运

    【讨论】:

      【解决方案2】:

      短一点

         <ListBox Name="ListboxTips" Width="Auto" ItemsSource="{Binding ColTips}" DisplayMemeberPath=StateTitle />
      

      如果您已经将上下文设置为 ColTips,那么您不需要 ItemsSource

      【讨论】:

        猜你喜欢
        • 2010-12-25
        • 2012-11-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-09-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多