Created
October 6, 2017 11:06
-
-
Save runceel/a7eb3fcb78729564ec8f037cc173bab3 to your computer and use it in GitHub Desktop.
コレクションの要素が1つでも true だったら Execute 可能な ReactiveCommand の作り方
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<Page x:Class="App1.MainPage" | |
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" | |
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" | |
xmlns:local="using:App1" | |
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" | |
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" | |
mc:Ignorable="d"> | |
<Page.DataContext> | |
<local:MainPageViewModel /> | |
</Page.DataContext> | |
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"> | |
<Grid.RowDefinitions> | |
<RowDefinition Height="Auto" /> | |
<RowDefinition Height="Auto" /> | |
<RowDefinition /> | |
</Grid.RowDefinitions> | |
<Button Content="Add" | |
Command="{x:Bind ViewModel.AddCommand}" /> | |
<Button Content="{x:Bind ViewModel.Message.Value, Mode=OneWay}" | |
Command="{x:Bind ViewModel.InvokedCommand}" | |
Grid.Row="1" /> | |
<ListView ItemsSource="{x:Bind ViewModel.Items}" | |
Grid.Row="2"> | |
<ListView.ItemTemplate> | |
<DataTemplate x:DataType="local:Item"> | |
<Grid> | |
<CheckBox Content="{x:Bind Text}" | |
IsChecked="{x:Bind local:MainPageViewModel.Convert(IsChecked), BindBack=SetChecked, Mode=TwoWay}" /> | |
</Grid> | |
</DataTemplate> | |
</ListView.ItemTemplate> | |
</ListView> | |
</Grid> | |
</Page> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Reactive.Bindings; | |
using Reactive.Bindings.Extensions; | |
using System; | |
using System.Collections.ObjectModel; | |
using System.ComponentModel; | |
using System.Linq; | |
using System.Reactive.Linq; | |
using Windows.UI.Xaml.Controls; | |
// 空白ページの項目テンプレートについては、https://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x411 を参照してください | |
namespace App1 | |
{ | |
/// <summary> | |
/// それ自体で使用できる空白ページまたはフレーム内に移動できる空白ページ。 | |
/// </summary> | |
public sealed partial class MainPage : Page | |
{ | |
private MainPageViewModel ViewModel => this.DataContext as MainPageViewModel; | |
public MainPage() | |
{ | |
this.InitializeComponent(); | |
} | |
} | |
public class MainPageViewModel | |
{ | |
private ObservableCollection<Item> ItemsSource { get; } = new ObservableCollection<Item>(Enumerable | |
.Range(1, 10) | |
.Select(x => new Item())); | |
public ReadOnlyReactiveCollection<Item> Items { get; } | |
public ReactiveCommand InvokedCommand { get; } | |
public ReactiveCommand AddCommand { get; } | |
public ReadOnlyReactiveProperty<string> Message { get; } | |
public MainPageViewModel() | |
{ | |
this.Items = this.ItemsSource.ToReadOnlyReactiveCollection(); | |
this.AddCommand = new ReactiveCommand(); | |
this.AddCommand.Subscribe(_ => | |
{ | |
this.ItemsSource.Add(new Item()); | |
}); | |
this.InvokedCommand = this.Items.ObserveElementProperty(x => x.IsChecked) | |
.Select(_ => this.Items.Any(x => x.IsChecked)) | |
.ToReactiveCommand(); | |
this.Message = this.InvokedCommand | |
.Select(_ => DateTime.Now.ToString()) | |
.ToReadOnlyReactiveProperty("None"); | |
} | |
public static bool? Convert(bool x) => x; | |
} | |
public class Item : INotifyPropertyChanged | |
{ | |
public event PropertyChangedEventHandler PropertyChanged; | |
private bool isChecked; | |
public bool IsChecked | |
{ | |
get => this.isChecked; | |
set | |
{ | |
this.isChecked = value; | |
this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Item.IsChecked))); | |
} | |
} | |
public void SetChecked(bool? x) => this.IsChecked = x ?? false; | |
public string Text { get; } = Guid.NewGuid().ToString(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment