简体   繁体   English

UWP C#数据透视表数据绑定

[英]UWP C# Pivot Data Binding

I'm trying to get a dynamic pivot. 我正在尝试找到一个动态的枢纽。 So dynamic headers and items. 因此动态标题和项目。 They depend on the number of Objects in a collection and they have a Name and a Title. 它们取决于集合中对象的数量,并且具有名称和标题。 But I don't know how to realize that, because I can't acces this data: 但是我不知道如何实现,因为我无法访问这些数据:

  <Pivot 
        x:Name="PivotExample"
        ItemsSource="{x:Bind Books}">
        <Pivot.HeaderTemplate>
            <DataTemplate x:DataType="Books">
                <TextBlock Text="{Binding Books.Name, Mode=OneWay}"/>
            </DataTemplate>
        </Pivot.HeaderTemplate>
        <Pivot.ItemTemplate>
             <DataTemplate>
                <TextBlock Text="{Binding Books.Title}"
             </DataTemplate>
  </Pivot>

I don't know how I can make it work. 我不知道该如何运作。 Help is appreciated. 感谢帮助。

Thanks J. 谢谢J.

Here's a working sample (if you get any error messages during compilation, rebuild the project manually): 这是一个工作示例(如果在编译过程中收到任何错误消息,请手动重建项目):

MainPage.xaml MainPage.xaml中

<Pivot ItemsSource="{x:Bind books}">

    <Pivot.HeaderTemplate>
        <DataTemplate x:DataType="local:Book">
            <TextBlock Text="{x:Bind Name}"/>
        </DataTemplate>
    </Pivot.HeaderTemplate>

    <Pivot.ItemTemplate>
        <DataTemplate x:DataType="local:Book">
            <TextBlock Text="{x:Bind Title}"/>
        </DataTemplate>
    </Pivot.ItemTemplate>

</Pivot>

MainPage.xaml.cs MainPage.xaml.cs中

public sealed partial class MainPage : Page
{
    ObservableCollection<Book> books = new ObservableCollection<Book>
    {
        new Book { Name = "Name1", Title = "Title1" },
        new Book { Name = "Name2", Title = "Title2" },
        new Book { Name = "Name3", Title = "Title3" },
        new Book { Name = "Name4", Title = "Title4" }
    };

    public MainPage()
    {
        this.InitializeComponent();
    }
}

Book.cs Book.cs

public class Book
{
    public string Name { get; set; }
    public string Title { get; set; }
}

Note that x:Bind uses OneTime Mode by default, which means changes made to the names and titles of the books won't be automatically reflected in the Pivot control. 请注意, x:Bind默认使用OneTime Mode ,这意味着对书名和书名所做的更改不会自动反映在“ Pivot控件中。 If that's not the desired behavior, then you'll need to use OneWay binding Mode and implement the INotifyPropertyChanged interface in the Book class: 如果这不是理想的行为,则需要使用OneWay绑定Mode并在Book类中实现INotifyPropertyChanged接口:

MainPage.xaml MainPage.xaml中

...
    <TextBlock Text="{x:Bind Name, Mode=OneWay}"/>
...
    <TextBlock Text="{x:Bind Title, Mode=OneWay}"/>
...

Book.cs Book.cs

public class Book : INotifyPropertyChanged
{
    private string name;
    public string Name
    {
        get
        {
            return name;
        }
        set
        {
            name = value;
            NotifyPropertyChanged("Name");
        }
    }

    private string title;
    public string Title
    {
        get
        {
            return title;
        }
        set
        {
            title = value;
            NotifyPropertyChanged("Title");
        }
    }


    public event PropertyChangedEventHandler PropertyChanged;
    private void NotifyPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }
}

You can use nameof operator to avoid magic strings in the setters. 您可以使用nameof运算符来避免在setter中使用魔术字符串。

Read more about data binding using x:Bind here . 在此处阅读有关使用x:Bind数据绑定的更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM