UWP appã§ã¯ãå¾æ¥ã®ãã¼ã¿ãã¤ã³ãã£ã³ã°ã«å ãã¦ã³ã³ãã¤ã«æãã¼ã¿ãã¤ã³ãã£ã³ã°ã¨ãããã®ã追å ããã¦ãã¾ããã³ã³ãã¤ã«æãã¼ã¿ãã¤ã³ãã£ã³ã°ã¯ãååã®éãå¾æ¥ã¯å®è¡æã«ãã£ã¦ããè²ã ãªå¦çãã³ã³ãã¤ã«æã«è¡ã£ã¦ãã¾ãã¨ãããã®ã§ããããã«ããããã¼ã¿ãã¤ã³ãã£ã³ã°ãå¤ç¨ããã¢ããªï¼XAMLç³»ã¢ããªã±ã¼ã·ã§ã³ã®å ´åã¯å¤§ä½ããã§ãããï¼ã§ã®æ§è½åä¸ãè¦è¾¼ãã¾ãã
ã·ã³ãã«ãªãã¼ã¿ãã¤ã³ã
ã³ã³ãã¤ã«æãã¼ã¿ãã¤ã³ãã£ã³ã°ã使ãä¸ã§å¤§ããç°ãªãç¹ã2ã¤ããã¾ãã
1ã¤ç®ãããã¼ã¯ã¢ããæ¡å¼µã{Binding ...}ã§ã¯ãªã{x:Bind ...}ã¨ãããã®ã使ãã¨ããç¹ã§ãã2ã¤ç®ãPathã®èµ·ç¹ã¨ãªãã®ãå¾æ¥ã®ãã¼ã¿ãã¤ã³ãã£ã³ã°ã§ã¯DataContextããããã£ã ã£ãã®ããã³ã³ãã¤ã«æãã¼ã¿ãã¤ã³ãã£ã³ã°ã§ã¯Pageãèµ·ç¹ã¨ããç¹ã§ãã
å®éã«ãã£ã¦ã¿ããã¨æãã¾ããSimpleBindingã¨ããã¢ããªã±ã¼ã·ã§ã³ãä½ã£ã¦åç´ãªããããã£ãæã£ãã ãã®ã·ã³ãã«ãªViewModelãä½æãã¾ãã
namespace SimpleBinding { public class MainPageViewModel { public string Name { get; } = "okazuki"; } }
ããã¦ãMainPageã¯ã©ã¹ã«ä»¥ä¸ã®ããã«ViewModelã¨ããååï¼ååã¯ãªãã§ãããã§ãï¼ã®ããããã£ãä½ãã¾ãã
using Windows.UI.Xaml.Controls; // 空ç½ãã¼ã¸ã®ã¢ã¤ãã ãã³ãã¬ã¼ãã«ã¤ãã¦ã¯ãhttp://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409 ãåç §ãã¦ãã ãã namespace SimpleBinding { /// <summary> /// ããèªä½ã§ä½¿ç¨ã§ãã空ç½ãã¼ã¸ã¾ãã¯ãã¬ã¼ã å ã«ç§»åã§ãã空ç½ãã¼ã¸ã /// </summary> public sealed partial class MainPage : Page { public MainPageViewModel ViewModel { get; } = new MainPageViewModel(); public MainPage() { this.InitializeComponent(); } } }
ããã¦ããã®ViewModelããããã£ãæå®ãã¦XAMLã§ãã¤ã³ããã¾ãã
<Page x:Class="SimpleBinding.MainPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:SimpleBinding" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d"> <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"> <TextBlock Text="{x:Bind ViewModel.Name}" Style="{ThemeResource BodyTextBlockStyle}"/> </Grid> </Page>
å®è¡ããã¨ä»¥ä¸ã®ããã«ãªãã¾ãã
Modeã®éã
次ã«ã³ã³ãã¤ã«æãã¼ã¿ãã¤ã³ãã£ã³ã°ãå¾æ¥ã®ãã¼ã¿ãã¤ã³ãã£ã³ã°ã¨ç°ãªãç¹ã¯ãModeã®ããã©ã«ãå¤ã§ããå¾æ¥ã®ãã¼ã¿ãã¤ã³ãã£ã³ã°ãOneWayãããã©ã«ãã ã£ãã®ã«å¯¾ãã¦ã³ã³ãã¤ã«æãã¼ã¿ãã¤ã³ãã£ã³ã°ã¯ãOneTimeãããã©ã«ãã«ãªãã¾ãããã®ãããINotifyPropertyChangedãå®è£ ããããããã£ããã¤ã³ããã¦ããå¤ã®å¤æ´ã¯ç»é¢ã«åæ ããã¾ããã
å®éã«OneWayBindingã¨ããããã¸ã§ã¯ããä½ã£ã¦è©¦ãã¦ã¿ã¾ãããã®ããã«INotifyPropertyChangedãå®è£ ããViewModelãä½æãã¾ãã
using System.ComponentModel; namespace OneWayBinding { public class MainPageViewModel : INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; private static readonly PropertyChangedEventArgs NamePropertyChangedEventArgs = new PropertyChangedEventArgs(nameof(Name)); private string name = "okazuki"; public string Name { get { return this.name; } set { if (this.name == value) { return; } this.name = value; this.PropertyChanged?.Invoke(this, NamePropertyChangedEventArgs); } } } }
ããã¦ãMainPageã¯ã©ã¹ã«ViewModelã¨ããããããã£ã§ä½¿ããããã«ãã¾ãã
using Windows.UI.Xaml.Controls; // 空ç½ãã¼ã¸ã®ã¢ã¤ãã ãã³ãã¬ã¼ãã«ã¤ãã¦ã¯ãhttp://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409 ãåç §ãã¦ãã ãã namespace OneWayBinding { /// <summary> /// ããèªä½ã§ä½¿ç¨ã§ãã空ç½ãã¼ã¸ã¾ãã¯ãã¬ã¼ã å ã«ç§»åã§ãã空ç½ãã¼ã¸ã /// </summary> public sealed partial class MainPage : Page { public MainPageViewModel ViewModel { get; } = new MainPageViewModel(); public MainPage() { this.InitializeComponent(); } } }
ããã¦ã以ä¸ã®ããã«ãããã©ã«ãã®ã¾ã¾ã®x:Bindã¨ModeãOneWayã«ããx:Bindãæå®ããTextBlockã並ã¹ã¾ãã
<Page x:Class="OneWayBinding.MainPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:OneWayBinding" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d"> <StackPanel Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"> <TextBlock Text="ããã©ã«ã" Style="{ThemeResource TitleTextBlockStyle}" /> <TextBlock Text="{x:Bind ViewModel.Name}" Style="{ThemeResource BodyTextBlockStyle}" /> <TextBlock Text="OneWay" Style="{ThemeResource TitleTextBlockStyle}" /> <TextBlock Text="{x:Bind ViewModel.Name, Mode=OneWay}" Style="{ThemeResource BodyTextBlockStyle}" /> <Button Content="ChangeName" Click="Button_Click" /> </StackPanel> </Page>
ãã¿ã³ã®ã¯ãªãã¯ã¤ãã³ããã³ãã©ã§Nameããããã£ãæ¸ãæãã¾ãã
using Windows.UI.Xaml.Controls; // 空ç½ãã¼ã¸ã®ã¢ã¤ãã ãã³ãã¬ã¼ãã«ã¤ãã¦ã¯ãhttp://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409 ãåç §ãã¦ãã ãã namespace OneWayBinding { /// <summary> /// ããèªä½ã§ä½¿ç¨ã§ãã空ç½ãã¼ã¸ã¾ãã¯ãã¬ã¼ã å ã«ç§»åã§ãã空ç½ãã¼ã¸ã /// </summary> public sealed partial class MainPage : Page { public MainPageViewModel ViewModel { get; } = new MainPageViewModel(); public MainPage() { this.InitializeComponent(); } private void Button_Click(object sender, Windows.UI.Xaml.RoutedEventArgs e) { this.ViewModel.Name = "kazuakix"; } } }
å®è¡ããã¨ä»¥ä¸ã®ããã«ãªãã¾ãã
ãã¿ã³ãæ¼ãã¨ã以ä¸ã®ããã«OneWayãæå®ããã»ãã ããã¼ã¿ãæ¸ãæããã®ããããã¾ãã
ã³ã³ããã¼ã«ã®ãã¤ã³ãã£ã³ã°
å¾æ¥ã®ãã¤ã³ãã£ã³ã°ã§ElementNameãæå®ãã¦ããã³ã³ããã¼ã«ã®ãã¤ã³ãã£ã³ã°ã¯ãPathã«ã³ã³ããã¼ã«åãç´æ¥æå®ããã ãã§ããããã«ãªã£ã¦ãã¾ããPathã®èµ·ç¹ããã¼ã¸ãªã®ã§å½ç¶ã®åä½ã¨ããã°å½ç¶ã®åä½ã§ãããControlBindingã¨ããååã®ããã¸ã§ã¯ããä½ã£ã¦åä½ãè¦ã¦ã¿ã¾ãã
Sliderã¨TextBlockãç½®ãã¦ãSliderã®å¤ã¨TextBlockã®Textããã¤ã³ããã¦ãã¾ãã
<Page x:Class="ControlBinding.MainPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:ControlBinding" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d"> <StackPanel Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"> <Slider x:Name="Slider" /> <TextBlock Text="{x:Bind Slider.Value, Mode=OneWay}" Style="{ThemeResource BodyTextBlockStyle}" /> </StackPanel> </Page>
ãã¤ã³ãã¯Sliderã«x:Nameã§ååãä»ãã¦ããç¹ã¨ããã®ååã使ã£ã¦TextBlockã§ãã¤ã³ããã¦ããç¹ã«ãªãã¾ããå®è¡ããã¨ä»¥ä¸ã®ããã«Sliderã®å¤ã¨TextBlockã®å¤ãé£åãã¦ãããã¨ããããã¾ãã
ã¤ãã³ãã®ãã¤ã³ãã£ã³ã°
ã³ã³ãã¤ã«æãã¤ã³ãã£ã³ã°ã§ã¯ãã¤ãã³ãã®ãã¤ã³ããè¡ããããã«ãªã£ã¦ãã¾ãã ä¾ãã°Buttonã®Clickã¤ãã³ããViewModelã«ãã¤ã³ãããå ´åã¯ä»¥ä¸ã®ããã«è¡ãã¾ããEventBindingã¨ããååã®ããã¸ã§ã¯ããä½ã£ã¦ä»¥ä¸ã®ãããªViewModelãä½æãã¾ãã
ã¤ãã³ãã«ç´ã¥ãã§ããã¡ã½ããã¯ãå¼æ°ç¡ãã¨ãobjectåã®å¼æ°ã2ã¤ãããã®ã¨ãæ®éã®ã¤ãã³ããã³ãã©ã®å½¢ã®å¼æ°ã®ãã®ã®3ãã¿ã¼ã³ãããã¾ãã
using System; using Windows.UI.Popups; using Windows.UI.Xaml; namespace EventBinding { public class MainPageViewModel { public async void Click1() { var d = new MessageDialog("Click1"); await d.ShowAsync(); } public async void Click2(object sender, object e) { var d = new MessageDialog("Click2"); await d.ShowAsync(); } public async void Click3(object sender, RoutedEventArgs e) { var d = new MessageDialog("Click3"); await d.ShowAsync(); } } }
Pageã«ViewModelããããã£ãå®ç¾©ãã¾ãã
using Windows.UI.Xaml.Controls; // 空ç½ãã¼ã¸ã®ã¢ã¤ãã ãã³ãã¬ã¼ãã«ã¤ãã¦ã¯ãhttp://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409 ãåç §ãã¦ãã ãã namespace EventBinding { /// <summary> /// ããèªä½ã§ä½¿ç¨ã§ãã空ç½ãã¼ã¸ã¾ãã¯ãã¬ã¼ã å ã«ç§»åã§ãã空ç½ãã¼ã¸ã /// </summary> public sealed partial class MainPage : Page { public MainPageViewModel ViewModel { get; } = new MainPageViewModel(); public MainPage() { this.InitializeComponent(); } } }
ããã¦ãXAMLã§ViewModelã®ã¤ãã³ãã«ãã¤ã³ããã¾ããã¤ãã³ãã®æå®ã®ä»æ¹ã¯åç´ã«ã¤ãã³ãã¸ã®ãã¹ãæå®ããã ãã§ãã
<Page x:Class="EventBinding.MainPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:EventBinding" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d"> <StackPanel Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"> <Button Content="Button1" Click="{x:Bind ViewModel.Click1}" /> <Button Content="Button2" Click="{x:Bind ViewModel.Click2}" /> <Button Content="Button3" Click="{x:Bind ViewModel.Click3}" /> </StackPanel> </Page>
å®è¡ãã¦ãã¿ã³ãã¯ãªãã¯ããã¨ã¡ãã»ã¼ã¸ããã¯ã¹ã表示ããã¾ãã
TextBoxã®ãã¤ã³ãã£ã³ã°
TextBoxã®Textããããã£ã¯ç¹å¥æ±ãããã¦ãã¾ããã»ãã®ããããã£ãå¤ãå¤ãã£ãã¿ã¤ãã³ã°ã§å¤æ´ãå ¥ãã®ã«å¯¾ãã¦ãTextBoxã®Textããããã£ã¯ãã©ã¼ã«ã¹ãå¤ããã¿ã¤ãã³ã°ã§å¤ãå¤ããã¾ãã
TextBoxBindingã¨ããããã¸ã§ã¯ããä½ã£ã¦TextBoxã¨TextBlockã®éãTwoWayãã¤ã³ãã£ã³ã°ããã¾ããTwoWayãã¤ã³ãã£ã³ã°ã®ã»ãã®æ³¨æç¹ã¨ãã¦ã¯ãä¾åé¢ä¿ããããã£ãããªãã¨ãã¡ã¨ããç¹ããã£ãããã¾ãã
<Page x:Class="TextBoxBinding.MainPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:TextBoxBinding" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d"> <StackPanel Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"> <TextBox x:Name="TextBox" Text="{x:Bind TextBlock.Text, Mode=TwoWay}" /> <TextBlock x:Name="TextBlock" Style="{ThemeResource BodyTextBlockStyle}" /> </StackPanel> </Page>
å®è¡ãã¦åä½ã確èªãã¾ããæåãæã¡è¾¼ãã ã ãã®æç¹ã§ã¯TextBlockã«å¤æ´ãåæ ããã¾ããã
ãã©ã¼ã«ã¹ãå¤ãã¨å¤æ´ãåæ ããã¾ãã
ã³ã¬ã¯ã·ã§ã³ã®ãã¤ã³ãã£ã³ã°
ItemsSourceããããã£ã«ã³ã¬ã¯ã·ã§ã³ããã¤ã³ããããã¨ã§ã³ã¬ã¯ã·ã§ã³ã®ãã¤ã³ããã§ãã¾ããCollectionBindingã¨ããããã¸ã§ã¯ããä½ã£ã¦ä»¥ä¸ã®ãããªã³ã¬ã¯ã·ã§ã³ã«è¡¨ç¤ºããé ç®ã表ãã¯ã©ã¹ãä½ãã¾ãã
using System.ComponentModel; namespace CollectionBinding { public class Person : INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; private static readonly PropertyChangedEventArgs NamePropertyChangedEventArgs = new PropertyChangedEventArgs(nameof(Name)); private string name; public string Name { get { return this.name; } set { if (this.name == value) { return; } this.name = value; this.PropertyChanged?.Invoke(this, NamePropertyChangedEventArgs); } } } }
ããã¦ãPersonã¯ã©ã¹ã®ã³ã¬ã¯ã·ã§ã³ãæã£ãMainPageViewModelã¯ã©ã¹ãä½ãã¾ãã
using System.Collections.ObjectModel; using System.Linq; namespace CollectionBinding { public class MainPageViewModel { public ObservableCollection<Person> People { get; } = new ObservableCollection<Person>(); public MainPageViewModel() { var source = Enumerable.Range(1, 100) .Select(x => new Person { Name = "okazuki" + x }); foreach (var p in source) { this.People.Add(p); } } } }
MainPageã¯ã©ã¹ã«ViewModelããããã£ã追å ãã¾ãã
using Windows.UI.Xaml.Controls; // 空ç½ãã¼ã¸ã®ã¢ã¤ãã ãã³ãã¬ã¼ãã«ã¤ãã¦ã¯ãhttp://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409 ãåç §ãã¦ãã ãã namespace CollectionBinding { /// <summary> /// ããèªä½ã§ä½¿ç¨ã§ãã空ç½ãã¼ã¸ã¾ãã¯ãã¬ã¼ã å ã«ç§»åã§ãã空ç½ãã¼ã¸ã /// </summary> public sealed partial class MainPage : Page { public MainPageViewModel ViewModel { get; } = new MainPageViewModel(); public MainPage() { this.InitializeComponent(); } } }
ããã¦ãListBoxãªã©ã®ã³ã¬ã¯ã·ã§ã³ã表示ããã³ã³ããã¼ã«ã®ItemsSourceã«ã³ã¬ã¯ã·ã§ã³ããã¤ã³ããã¾ãããã®ã¨ãããã¼ã¿ã表示ããããã®DataTemplateãæå®ãããã¨ãã»ã¨ãã©ã ã¨æãã¾ãããDataTemplateå ã§ã³ã³ãã¤ã«æãã¤ã³ãã£ã³ã°ã使ãã«ã¯x:DataTypeå±æ§ã§DataTemplateã«è¡¨ç¤ºãããåãæå®ããå¿ è¦ãããã¾ãã
ListBoxã«ViewModelã®Peopleããããã£ããã¤ã³ããã¦ãDataTemplateã§Personã¯ã©ã¹ã表示ããå ´åã¯ä»¥ä¸ã®ããã«æ¸ãã¾ãã
<ListBox ItemsSource="{x:Bind ViewModel.People}" Grid.Row="1"> <ListBox.ItemTemplate> <DataTemplate x:DataType="local:Person"> <TextBlock Text="{x:Bind Name}" Style="{ThemeResource BodyTextBlockStyle}" /> </DataTemplate> </ListBox.ItemTemplate> </ListBox>
å®è¡ããã¨ä»¥ä¸ã®ãããªè¦ãç®ã«ãªãã¾ãã
ItemTemplateã«UserControlãæå®ããå ´å
ItemTemplateã®ä¸èº«ãUserControlã«ãããã¨ã¯ããããã¨æãã¾ãããã®å ´åã«ã³ã³ãã¤ã«æãã¤ã³ãã£ã³ã°ã使ãã«ã¯ãUserControlã«ViewModelã¨ããDataContextããããã£ããã£ã¹ããã¦å ¬éããããããã£ãå®ç¾©ãã¾ãã
以ä¸ã®ããã«ãªãã¾ãã
PersonViewã¨ããUserControlãä½ã£ã¦Personåã®ViewModelä¾åé¢ä¿ããããã£ãä½ãã¾ãã
using Windows.UI.Xaml.Controls; namespace CollectionBinding { public sealed partial class PersonView : UserControl { public Person ViewModel => this.DataContext as Person; public PersonView() { this.InitializeComponent(); this.DataContextChanged += (_, __) => this.Bindings.Update(); } } }
Bindingsããããã£ã«ã¤ã㦠ã³ã³ãã¤ã«æãã¤ã³ãã£ã³ã°ã使ãã¨Bindingsããããã£ã¨ãããã®ãViewã«å®ç¾©ããã¾ãã ããã®Updateã¡ã½ãããå¼ã³åºãã¨ãã¹ã¦ã®ã³ã³ãã¤ã«æãã¤ã³ãã£ã³ã°ãå¼·å¶çã«åè©ä¾¡ãããã®ã«ãªãã¾ãã
DataContextãå¤ãã度ã«ãã³ã³ãã¤ã«æãã¼ã¿ãã¤ã³ãã£ã³ã°ãåè©ä¾¡ãã¦ããç¹ããã¤ã³ãã§ãã
ããã¦ãUserControlã®XAMLã§Personã¯ã©ã¹ã®è¦ãç®ãå®ç¾©ãã¾ãã
<UserControl x:Class="CollectionBinding.PersonView" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:CollectionBinding" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" d:DesignHeight="300" d:DesignWidth="400"> <StackPanel> <TextBlock Text="åå" Style="{ThemeResource TitleTextBlockStyle}" /> <TextBlock Text="{x:Bind Name}" /> </StackPanel> </UserControl>
ãããItemTemplateã«è¨å®ãã¾ãã
<ListBox ItemsSource="{x:Bind ViewModel.People}" Grid.Row="1" Grid.Column="1"> <ListBox.ItemTemplate> <DataTemplate> <local:PersonView /> </DataTemplate> </ListBox.ItemTemplate> </ListBox>
å®è¡ããã¨ä»¥ä¸ã®ããã«ãªãã¾ãã
Converter
ã³ã³ãã¤ã«æãã¤ã³ãã£ã³ã°ããConverterã使ããã¨ãã§ãã¾ããé常ã®Converterã®ã¨ãã¦ã®ä½¿ãæ¹ã®ä»ã«ã³ã³ãã¤ã«æã«å解決ãè¡ãé¢ä¿ä¸ããã¦ãªãä»æ§ã«ãªã£ã¦ãã¾ã£ã¦ããã¨ããã§ãã¡ãã£ã¨ä½¿ããã¨ãããã¾ãã å ã»ã©ã®ã³ã¬ã¯ã·ã§ã³ã®Bindingã§ãSelectedItemã¨ViewModelã«å®ç¾©ãã以ä¸ã®ãããªããããã£ããã¤ã³ãããã¨ã³ã³ãã¤ã«ã¨ã©ã¼ã«ãªãã¾ãã
<ListBox ItemsSource="{x:Bind ViewModel.People}" SelectedItem="{x:Bind ViewModel.SelectedItem, Mode=TwoWay}" Grid.Row="1" Grid.Column="1"> <ListBox.ItemTemplate> <DataTemplate> <local:PersonView /> </DataTemplate> </ListBox.ItemTemplate> </ListBox> <TextBlock Text="{x:Bind ViewModel.SelectedItem.Name, Mode=OneWay}" Grid.Row="2" Grid.ColumnSpan="2" />
private Person selectedItem; public Person SelectedItem { get { return this.selectedItem; } set { if (this.selectedItem == value) { return; } this.selectedItem = value; this.PropertyChanged?.Invoke(this, SelectedItemPropertyChangedEventArgs); } }
ã³ã³ãã¤ã«ã¨ã©ã¼ã®å 容ã¯ä»¥ä¸ã®ããã«ãªãã¾ãã
ç¡å¹ãªãã¤ã³ã ãã¹ 'ViewModel.SelectedItem' : ã³ã³ãã¼ã¿ã¼ã使ç¨ããã«å 'CollectionBinding.Person' ã 'System.Object' ã«ãã¤ã³ãã§ãã¾ãã
ã¤ã¾ãSelectedItemãobjectåãªã®ã«å¯¾ãã¦SelectedItemãPersonåãªã®ã§ãã®ã¾ã¾ã ã¨ä»£å ¥ã§ããªãã¨ãããã¦ãã®ã§ããã§ãSelectedItemã¯ä»åã®å ´åã¯Personåã§ãããã¨ã¯ç¢ºå®ãªã®ã§ä»£å ¥ãã¦ã»ããã¨ããã§ãããã
æç´ã£ã¦ãä»æ¹ãªãã®ã§è§£æ±ºçã§ããä½ãããªãConverterãä½ã£ã¦ããã¨ããã®ã解決çã«ãªãã¾ãã
using System; using Windows.UI.Xaml.Data; namespace CollectionBinding { public class NoopConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, string language) { return value; } public object ConvertBack(object value, Type targetType, object parameter, string language) { return value; } } }
ããã¦ããããConverterã«æå®ãã¾ãã
<Page.Resources> <local:NoopConverter x:Key="NoopConverter" /> </Page.Resources> <ListBox ItemsSource="{x:Bind ViewModel.People}" SelectedItem="{x:Bind ViewModel.SelectedItem, Mode=TwoWay, Converter={StaticResource NoopConverter}}" Grid.Row="1" Grid.Column="1"> <ListBox.ItemTemplate> <DataTemplate> <local:PersonView /> </DataTemplate> </ListBox.ItemTemplate> </ListBox>
Converterã¯Pageã®ResourcesãApp.xamlã®Resourcesãããããã¨ããããã«å®ç¾©ããå¿ è¦ãããã¾ãã
ããã§å®è¡ããã¨ã¡ããã¨ã³ã³ãã¤ã«ãéãããã«ãªãã¾ãã
ãã®ä»
ããã§ã¯ç´¹ä»ãã¦ãã¾ããããx:Bindã§ã¯ã以ä¸ã®ãããªããããã£ãæå®ã§ãã¾ãã
- TargetNullValue: nullã®æã«ãããã«è¡¨ç¤ºããå¤
- FallbackValue: 失æããã¨ãã®è¡¨ç¤ºããå¤
ã¾ã¨ã
x:Bindã§ã¯ãã§ãããã¨ãé常ã®Bindingã«æ¯ã¹ã¦å°ãªãã§ããã使ããå ´æã§ã¯ä½¿ãããã«ããã¨ããx:Bindã§è§£æ±ºã§ããããã«ã¢ããªãè¦ç´ããã¨ã§ããã©ã¼ãã³ã¹ã®åä¸ãè¦è¾¼ãã¾ãã
æå¾ã«ãããã§æ¸ããããã°ã©ã ã¯ä»¥ä¸ã®GitHubã®ãªãã¸ããªãããã¦ã³ãã¼ãã§ãã¾ãã