Beispiel #1
0
        static void Bind_TwoWay()
        {
            // Binding Source (Any object).
            var person = new Person1 {
                Id = 123, Name = "Taro"
            };

            // Binding Target (DependencyObject).
            var textBox = new TextBox {
                Text = "Default"
            };

            Console.WriteLine(textBox.Text);

            // Binds target to source.
            var binding = new Binding(nameof(person.Name))
            {
                Source = person, UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged
            };

            textBox.SetBinding(TextBox.TextProperty, binding);
            Console.WriteLine(textBox.Text);

            // Changes source value.
            person.Name = "Jiro";
            Console.WriteLine(textBox.Text);

            // Changes target value.
            textBox.Text = "Saburo";
            Console.WriteLine(person.Name);
        }
Beispiel #2
0
        static void Bind_Collection()
        {
            var taro = new Person1 {
                Id = 123, Name = "Taro"
            };
            var jiro = new Person1 {
                Id = 234, Name = "Jiro"
            };

            // Binding Source (collection).
            var people = new ObservableCollection <Person1> {
                taro
            };

            // Binding Target.
            var itemsControl = new ItemsControl();

            Console.WriteLine(itemsControl.Items.Count);

            // Binds target to source.
            // MEMO: Binding Source のオブジェクト自体が変更されないのであれば、
            // ItemsSource プロパティのデータ バインディングは必須ではありません。
            itemsControl.ItemsSource = people;
            Console.WriteLine(itemsControl.Items.Count);

            // Changes source collection.
            people.Add(jiro);
            Console.WriteLine(itemsControl.Items.Count);
            people.RemoveAt(0);
            Console.WriteLine(itemsControl.Items.Count);

            // MEMO: ItemsSource に値を設定している場合、Items を直接変更しようとすると例外が発生します。
            //itemsControl.Items.Add(jiro);
        }
Beispiel #3
0
        static void Bind_OneWay()
        {
            // Binding Source (Any object).
            var person = new Person1 {
                Id = 123, Name = "Taro"
            };

            // Binding Target (DependencyObject).
            var textBlock = new TextBlock {
                Text = "Default"
            };

            Console.WriteLine(textBlock.Text);

            // Binds target to source.
            var binding = new Binding(nameof(person.Name))
            {
                Source = person
            };

            textBlock.SetBinding(TextBlock.TextProperty, binding);
            Console.WriteLine(textBlock.Text);

            // Changes source value.
            person.Name = "Jiro";
            Console.WriteLine(textBlock.Text);
        }
Beispiel #4
0
        static void Bind_OneWay()
        {
            // Binding Source (Any object).
            var person = new Person1 { Id = 123, Name = "Taro" };

            // Binding Target (DependencyObject).
            var textBlock = new TextBlock { Text = "Default" };
            Console.WriteLine(textBlock.Text);

            // Binds target to source.
            var binding = new Binding(nameof(person.Name)) { Source = person };
            textBlock.SetBinding(TextBlock.TextProperty, binding);
            Console.WriteLine(textBlock.Text);

            // Changes source value.
            person.Name = "Jiro";
            Console.WriteLine(textBlock.Text);
        }
Beispiel #5
0
        static void Bind_TwoWay()
        {
            // Binding Source (Any object).
            var person = new Person1 { Id = 123, Name = "Taro" };

            // Binding Target (DependencyObject).
            var textBox = new TextBox { Text = "Default" };
            Console.WriteLine(textBox.Text);

            // Binds target to source.
            var binding = new Binding(nameof(person.Name)) { Source = person, UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged };
            textBox.SetBinding(TextBox.TextProperty, binding);
            Console.WriteLine(textBox.Text);

            // Changes source value.
            person.Name = "Jiro";
            Console.WriteLine(textBox.Text);

            // Changes target value.
            textBox.Text = "Saburo";
            Console.WriteLine(person.Name);
        }
Beispiel #6
0
        static void Bind_Collection_CurrentItem()
        {
            var taro = new Person1 {
                Id = 123, Name = "Taro"
            };
            var jiro = new Person1 {
                Id = 234, Name = "Jiro"
            };
            var people = new[] { taro, jiro };

            var grid = new Grid {
                DataContext = people
            };
            var listBox = new ListBox {
                IsSynchronizedWithCurrentItem = true
            };

            grid.Children.Add(listBox);
            var textBlock = new TextBlock {
                Text = "Default"
            };

            grid.Children.Add(textBlock);

            textBlock.SetBinding(FrameworkElement.DataContextProperty, new Binding("/"));
            textBlock.SetBinding(TextBlock.TextProperty, new Binding("Name"));
            Console.WriteLine((listBox.SelectedValue as Person1)?.Name ?? "null");
            Console.WriteLine(textBlock.Text);

            // MEMO: In case that IsSynchronizedWithCurrentItem is false, SelectedValue is null.
            listBox.SetBinding(ItemsControl.ItemsSourceProperty, new Binding());
            Console.WriteLine((listBox.SelectedValue as Person1)?.Name ?? "null");
            Console.WriteLine(textBlock.Text);

            listBox.SelectedValue = jiro;
            Console.WriteLine((listBox.SelectedValue as Person1)?.Name ?? "null");
            Console.WriteLine(textBlock.Text);
        }
Beispiel #7
0
        static void Bind_Collection_CurrentItem()
        {
            var taro = new Person1 { Id = 123, Name = "Taro" };
            var jiro = new Person1 { Id = 234, Name = "Jiro" };
            var people = new[] { taro, jiro };

            var grid = new Grid { DataContext = people };
            var listBox = new ListBox { IsSynchronizedWithCurrentItem = true };
            grid.Children.Add(listBox);
            var textBlock = new TextBlock { Text = "Default" };
            grid.Children.Add(textBlock);

            textBlock.SetBinding(FrameworkElement.DataContextProperty, new Binding("/"));
            textBlock.SetBinding(TextBlock.TextProperty, new Binding("Name"));
            Console.WriteLine((listBox.SelectedValue as Person1)?.Name ?? "null");
            Console.WriteLine(textBlock.Text);

            // MEMO: In case that IsSynchronizedWithCurrentItem is false, SelectedValue is null.
            listBox.SetBinding(ItemsControl.ItemsSourceProperty, new Binding());
            Console.WriteLine((listBox.SelectedValue as Person1)?.Name ?? "null");
            Console.WriteLine(textBlock.Text);

            listBox.SelectedValue = jiro;
            Console.WriteLine((listBox.SelectedValue as Person1)?.Name ?? "null");
            Console.WriteLine(textBlock.Text);
        }
Beispiel #8
0
        static void Bind_Collection()
        {
            var taro = new Person1 { Id = 123, Name = "Taro" };
            var jiro = new Person1 { Id = 234, Name = "Jiro" };

            // Binding Source (collection).
            var people = new ObservableCollection<Person1> { taro };

            // Binding Target.
            var itemsControl = new ItemsControl();
            Console.WriteLine(itemsControl.Items.Count);

            // Binds target to source.
            // MEMO: Binding Source のオブジェクト自体が変更されないのであれば、
            // ItemsSource プロパティのデータ バインディングは必須ではありません。
            itemsControl.ItemsSource = people;
            Console.WriteLine(itemsControl.Items.Count);

            // Changes source collection.
            people.Add(jiro);
            Console.WriteLine(itemsControl.Items.Count);
            people.RemoveAt(0);
            Console.WriteLine(itemsControl.Items.Count);

            // MEMO: ItemsSource に値を設定している場合、Items を直接変更しようとすると例外が発生します。
            //itemsControl.Items.Add(jiro);
        }