Browse Source

Добавлена привязка выбранного элемента

Вадим Королёв 6 months ago
parent
commit
ff8c6b0e81

+ 35 - 14
src/SASDesktop/ViewModels/Transport/Create.cs

@@ -9,34 +9,55 @@ namespace SASDesktop.ViewModels.Transport
 {
     class Create : BindableBase
     {
-        private Models.Brand _selectedBrand;
-        private List<Models.Brand> _suggestedBrands;
+        // -- VIN --
         private string _vin;
-        private string _brandSearch;
+        public string VIN
+        {
+            get { return _vin; }
+            set { _vin = value; RaisePropertyChanged(nameof(VIN)); }
+        }
+        
+        // -- Производитель --
+        // Выбранный производитель
+        private Models.Brand _selectedBrand;
+        public Models.Brand SelectedBrand
+        {
+            get { return _selectedBrand; }
+            set {
+                _selectedBrand = value;
 
+                // В поле ввода производителя должно подставиться имя
+                // производителя, но фильтрации происходить не должно
+                _brandSearch = _selectedBrand.Name;
+                RaisePropertyChanged(nameof(BrandSearch));
+            }
+        }
+        // Предлагаемые производители
+        private List<Models.Brand> _suggestedBrands;
         public List<Models.Brand> SuggestedBrands
         {
             get { return _suggestedBrands; }
             set { _suggestedBrands = value; RaisePropertyChanged(nameof(SuggestedBrands)); }
         }
-        public string VIN
-        {
-            get { return _vin; }
-            set { _vin = value; RaisePropertyChanged(nameof(VIN)); }
-        }
+        // Поиск производителя по имени
+        private string _brandSearch;
         public string BrandSearch
         {
             get => _brandSearch;
             set 
             {
-                var db = new Models.SASEntities();
                 _brandSearch = value;
 
-                // Применить фильтр
-                _suggestedBrands.Clear();
-                _suggestedBrands = new List<Models.Brand>(db.Brands.Where(b => b.Name.ToLower().Contains(_brandSearch.ToLower())));
+                // Обновление подсказок
+                // Если value - пустая строка, то в список попадают все
+                // сущности
+                var db = new Models.SASEntities();
+                SuggestedBrands = new List<Models.Brand>(
+                    db.Brands.Where(b =>
+                        string.IsNullOrWhiteSpace(_brandSearch) ||
+                        b.Name.ToLower().Contains(_brandSearch.ToLower())
+                );
 
-                RaisePropertyChanged(nameof(SuggestedBrands));
                 RaisePropertyChanged(nameof(BrandSearch)); 
             }
         }
@@ -44,7 +65,7 @@ namespace SASDesktop.ViewModels.Transport
         public Create() 
         {
             var db = new Models.SASEntities();
-            _suggestedBrands = new List<Models.Brand>(db.Brands);
+            _suggestedBrands = new List<Models.Brand>();
         }
     }
 }

+ 22 - 30
src/SASDesktop/Views/Transport/Create.xaml

@@ -19,49 +19,39 @@
                 Text="Создание транспортного средства"/>
 
             <StackPanel Orientation="Vertical">
-
                 <!--VIN-->
-                <Grid>
-                    <Grid.ColumnDefinitions>
-                        <ColumnDefinition Width="*"/>
-                    </Grid.ColumnDefinitions>
-
-                    <!--Фамилия-->
-                    <StackPanel 
-                        Grid.Column="0"
-                        Margin="4">
-
-                        <StackPanel Orientation="Horizontal">
-                            <TextBlock Style="{StaticResource H5}" Text="VIN номер"/>
-                            <Label Style="{StaticResource RequiredMark}" Margin="4,0,0,0"/>
-                        </StackPanel>
-
-                        <TextBox
-                            Margin="0,4"
-                            VerticalContentAlignment="Center"
-                            Text="{Binding VIN}"/>
+                <StackPanel Margin="4">
+                    <!--Надпись-->
+                    <StackPanel Orientation="Horizontal">
+                        <TextBlock Style="{StaticResource H5}" Text="VIN номер"/>
+                        <Label Style="{StaticResource RequiredMark}" Margin="4,0,0,0"/>
                     </StackPanel>
-                </Grid>
+                    <!--Поле ввода-->
+                    <TextBox
+                        Margin="0,4"
+                        VerticalContentAlignment="Center"
+                        Text="{Binding VIN}"/>
+                </StackPanel>
 
-                <!--Марка и модель-->
+                <!--Производитель и модель-->
                 <Grid>
                     <Grid.ColumnDefinitions>
                         <ColumnDefinition Width="*"/>
                         <ColumnDefinition Width="*"/>
                     </Grid.ColumnDefinitions>
 
-                    <!--Марка (бренд)-->
-                    <StackPanel 
-                        Grid.Column="0"
-                        Margin="4">
-
+                    <!--Производитель-->
+                    <StackPanel Grid.Column="0" Margin="4">
+                        <!--Надпись-->
                         <StackPanel Orientation="Horizontal">
                             <TextBlock Style="{StaticResource H5}" Text="Производитель"/>
                             <Label Style="{StaticResource RequiredMark}" Margin="4,0,0,0"/>
                         </StackPanel>
-
+                        <!--Поле ввода и автодополнение-->
                         <StackPanel>
-                            <TextBox x:Name="tbBrand" Text="{Binding BrandSearch,UpdateSourceTrigger=PropertyChanged}"/>
+                            <TextBox
+                                x:Name="tbBrand"
+                                Text="{Binding BrandSearch,UpdateSourceTrigger=PropertyChanged}"/>
                             <Popup 
                                 Placement="Bottom"
                                 Width="{Binding RelativeSource={RelativeSource Self},Path=PlacementTarget.ActualWidth}"
@@ -80,7 +70,9 @@
                                         </Style.Triggers>
                                     </Style>
                                 </Popup.Style>
-                                <ListBox ItemsSource="{Binding SuggestedBrands}">
+                                <ListBox
+                                    ItemsSource="{Binding SuggestedBrands}"
+                                    SelectedItem="{Binding SelectedBrand}">
                                     <ListBox.ItemTemplate>
                                         <DataTemplate>
                                             <TextBlock Text="{Binding Name}"/>