c# - windows store app Listview 绑定(bind)

标签 c# xaml listview data-binding windows-8

我有一个包含数据的类:

public class routedata : INotifyPropertyChanged

{

    private List<double> distances;
    public List<double> Distances
    {
        get { return this.distances; }
        set
        {
            if (this.distances != value)
            {
                this.distances = value;
                this.onPropertyChanged("Distances");
            }
        }
    }

    private List<string> instructions;
    public List<string> Instructions
    {
        get { return this.instructions; }
        set
        {
            if (this.instructions != value)
            {
                this.instructions = value;
                this.onPropertyChanged("Instructions");
            }
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    protected virtual void onPropertyChanged(string property)
    {
        if (this.PropertyChanged != null)
        {
            this.PropertyChanged(this, new PropertyChangedEventArgs(property));
        }
    }        
}

我正在尝试将它绑定(bind)到这样的 ListView :

     <GridView Name="routeView" HorizontalAlignment="Left" Height="310" Margin="1025,318,0,0" Grid.Row="1"      
           VerticalAlignment="Top" Width="340" >
            <ListView Name="routeList" Height="300" Width="330" ItemsSource="{Binding routeData}">
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <StackPanel Orientation="Horizontal">
                            <TextBlock Text="{Binding Instructions}"
                                                      TextWrapping="Wrap" Width="200"/>
                            <TextBlock Text="{Binding Distances}"
                                                      Margin="10,0,0,0" />
                            <TextBlock Text="km"/>
                        </StackPanel>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>
        </GridView>

我的 C# 代码后面有:routeList.datacontext = this;

但它仍然没有绑定(bind), ListView 中只填充了一个空行。我检查了数据,它都存在。任何帮助将不胜感激谢谢。

最佳答案

ListView 将单个集合作为 ItemsSource,因此如果您想为每个项目显示多个 TextBlocks - 您需要一个具有多个文本属性的对象集合来绑定(bind)到您的 DataTemplate。在您的情况下, routeData 不是集合。相反,您需要定义项目 View 模型,例如

public class RoutePoint
{
    public double Distance { get; set; }
    public string Instruction { get; set; }
}

然后您会将 ListView.ItemSource 绑定(bind)到一个列表,并在您的 DataTemplate 中像这样绑定(bind)它:

<TextBlock Text="{Binding Distance}"/>
<TextBlock Text="{Binding Instruction}"/>

如果您的集合在您第一次将其绑定(bind)到 ListView 后从未更改(SelectedItem 不构成更改),则无需使用 ObservableCollection。

关于c# - windows store app Listview 绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14541463/

相关文章:

c# - 将 C# 集成到 C 到 Java 时出现 OutOfMemoryError/程序崩溃

c# - 什么会导致此属性偶尔抛出 NullReferenceException?

java - MainActivity 中的两个 Intent

android - 用光标填充 ListView

c# - 通过从 App.xaml 绑定(bind)加载 ResourceDictionary?

Android按钮onClick拦截滚动

c# - 将 datetime2 数据类型转换为 datetime 数据类型会导致值超出范围

c# - 是否有自动为您格式化代码的 TFS checkin 策略?

c# - 如何创建一个完全不可见的按钮?

xaml - Xamarin MVVM 将用户数据推送到 viewmodel