powered by simpleCommunicator - 2.0.51     © 2025 Programmizd 02
Форумы / WPF, Silverlight [игнор отключен] [закрыт для гостей] / Автоматическое отображение значения свойства
4 сообщений из 4, страница 1 из 1
Автоматическое отображение значения свойства
    #37779295
DrunkCat
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Гость
есть следующий класс окна
Код: c#
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
37.
38.
39.
40.
41.
42.
43.
44.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfApplication
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            A = 2;
            B = 3;
            this.DataContext = this;
        }
        public int A
        {
            get;
            set;
        }
        public int B
        {
            get;
            set;
        }
        public int S
        {
            get
            {
                return A + B;
            }
        }
    }
}


есть его декларативное представление
Код: xml
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
<Window x:Class="WpfApplication.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" >
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto" />
            <ColumnDefinition />
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>
        <Label Grid.Row="0" Grid.Column="0">A</Label>
        <TextBox Grid.Row="0" Grid.Column="1" Text="{Binding Path=A}" />
        <Label Grid.Row="1" Grid.Column="0">B</Label>
        <TextBox Grid.Row="1" Grid.Column="1" Text="{Binding Path=B}" />
        <Label Grid.Row="2" Grid.Column="0">S</Label>
        <TextBox Grid.Row="2" Grid.Column="1" Text="{Binding Path=S, Mode=OneWay}" IsReadOnly="True" />
    </Grid>
</Window>


Необходимо чтобы при изменении числа "А" или "В" автоматически пересчитывалось и отображалось значение "S"
Исходный проект прилагаю
Заранее благодарен!
...
Рейтинг: 0 / 0
Автоматическое отображение значения свойства
    #37779319
Lelouch
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Участник
DrunkCat,

Откройте для себя INotifyPropertyChanged
...
Рейтинг: 0 / 0
Автоматическое отображение значения свойства
    #37779345
DrunkCat
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Гость
И что писать в обработчике событий?
...
Рейтинг: 0 / 0
Автоматическое отображение значения свойства
    #37779379
DrunkCat
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Гость
Разобрался! При изменение какого-либо свойства необходимо указывать что изменилось еще и зависимое
Вместо
Код: c#
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
private int _a;
        public int A
        {
            get
            {
                return _a;
            }
            set
            {
                _a = value;
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs("A"));
                }
            }
        }


Следует писать
Код: c#
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
private int _a;
        public int A
        {
            get
            {
                return _a;
            }
            set
            {
                _a = value;
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs("A"));
                    PropertyChanged(this, new PropertyChangedEventArgs("S"));
                }
            }
        }


В результате все должно выглядеть следующем образом
Код: c#
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
37.
38.
39.
40.
41.
42.
43.
44.
45.
46.
47.
48.
49.
50.
51.
52.
53.
54.
55.
56.
57.
58.
59.
60.
61.
62.
63.
64.
65.
66.
67.
68.
69.
70.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.ComponentModel;

namespace WpfApplication
{
    public partial class MainWindow : Window, INotifyPropertyChanged
    {
        public MainWindow()
        {
            InitializeComponent();
            A = 2;
            B = 3;
            this.DataContext = this;
        }
        public event PropertyChangedEventHandler PropertyChanged;
        private int _a;
        public int A
        {
            get
            {
                return _a;
            }
            set
            {
                _a = value;
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs("A"));
                    PropertyChanged(this, new PropertyChangedEventArgs("S"));
                }
            }
        }
        private int _b;
        public int B
        {
            get
            {
                return _b;
            }
            set
            {
                _b = value;
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs("B"));
                    PropertyChanged(this, new PropertyChangedEventArgs("S"));
                }
            }
        }
        public int S
        {
            get
            {
                return A + B;
            }
        }
    }
}


Всем спасибо!
...
Рейтинг: 0 / 0
4 сообщений из 4, страница 1 из 1
Форумы / WPF, Silverlight [игнор отключен] [закрыт для гостей] / Автоматическое отображение значения свойства
Целевая тема:
Создать новую тему:
Автор:
Закрыть
Цитировать
Найденые пользователи ...
Разблокировать пользователей ...
Читали форум (0):
Пользователи онлайн (0):
x
x
Закрыть


Просмотр
0 / 0
Close
Debug Console [Select Text]