frendguo's blog

UWP中的数据绑定(Data Binding)

关于数据绑定, 可以看这里: https://docs.microsoft.com/en-us/windows/uwp/data-binding/

普通元素绑定

这个比较简单, 也没有什么坑 可以直接看官方文档的实例就好啦, 这里搬了个示例:

Recording.cs:

namespace Quickstart
{
    public class Recording
    {
        public string ArtistName { get; set; }
        public string CompositionName { get; set; }
        public DateTime ReleaseDateTime { get; set; }
        public Recording()
        {
            this.ArtistName = "Wolfgang Amadeus Mozart";
            this.CompositionName = "Andante in C for Piano";
            this.ReleaseDateTime = new DateTime(1761, 1, 1);
        }
        public string OneLineSummary
        {
            get
            {
                return $"{this.CompositionName} by {this.ArtistName}, released: "
                    + this.ReleaseDateTime.ToString("d");
            }
        }
    }
    public class RecordingViewModel
    {
        private Recording defaultRecording = new Recording();
        public Recording DefaultRecording { get { return this.defaultRecording; } }
    }
}

MainPage.xaml.cs:

namespace Quickstart
    {
        public sealed partial class MainPage : Page
        {
            public MainPage()
            {
                this.InitializeComponent();
                this.ViewModel = new RecordingViewModel();
            }
            public RecordingViewModel ViewModel { get; set; }
        }
    }

MainPage.xaml:

<Page x:Class="Quickstart.MainPage" ... >
        <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
            <TextBlock Text="{x:Bind ViewModel.DefaultRecording.OneLineSummary}"
            HorizontalAlignment="Center"
            VerticalAlignment="Center"/>
        </Grid>
    </Page>

绑定集合

List<T> 和ObservableCollection<T>

绑定集合的时候最好选择ObservableCollection<T> 而不选择List<T>, 因为前者实现了 INotifyPropertyChangedINotifyCollectionChanged 接口.当添加或删除项目或者列表本身的属性更改时,这些接口将向绑定提供更改通知。而List<T>则不会通知, 也就是说, 如果使用List<T>来作为绑定源的话, 绑定目标将会变成静态的, 就是说将不会改变了.

List<T> list ---->  ObservableCollection<T>:

list.ForEach(p => observableCollection.add(p));

绑定自定义集合

  • 推荐使用LIst<T>, ObservableCollection<T>等内置的集合类型
  • 实现自定以的集合常用的方法就是从IList 接口派生实现自定义集合类
  • 如果想实现动态绑定, 则需要实现INotifyPropertyChangedINotifyCollectionChanged 接口

绑定数据转换

如果绑定源和绑定目标之间只是以某种关系关联起来, 而不是完全相等的关系. 这个时候, 我们需要使用转换器了. 

我们可以通过创建一个实现了IValueConverter 接口针对每个具体的应用场景自定义该转换器.(在 .cs 文件中)

public class DateToStringConverter : IValueConverter
{
    // Define the Convert method to convert a DateTime value to 
    // a month string.
    public object Convert(object value, Type targetType, 
        object parameter, string language)
    {
        // value is the data from the source object.
        DateTime thisdate = (DateTime)value;
        int monthnum = thisdate.Month;
        string month;
        switch (monthnum)
        {
            case 1:
                month = "January";
                break;
            case 2:
                month = "February";
                break;
            default:
                month = "Month not found";
                break;
        }
        // Return the value to pass to the target.
        return month;
    }

    // ConvertBack is not implemented for a OneWay binding.
    public object ConvertBack(object value, Type targetType, 
        object parameter, string language)
    {
        throw new NotImplementedException();
    }
}

在Xaml 文件中使用:

<UserControl.Resources>
  <local:DateToStringConverter x:Key="Converter1"/>
</UserControl.Resources>

...

<TextBlock Grid.Column="0" 
  Text="{x:Bind ViewModel.Month, Converter={StaticResource Converter1}}"/>

<TextBlock Grid.Column="0" 
  Text="{Binding Month, Converter={StaticResource Converter1}}"/>

具体的看这边>>>>

到这里 大致结束了, 先留点坑吧, 以后遇到再填~

填坑日志之数据绑定的那些坑~

Happy Coding!o(* ̄▽ ̄*)o

Add comment

Loading