Forráskód Böngészése

Стили, триггеры, анамация, конвертеры привязки

fly9024 1 hónapja
szülő
commit
2b462fad9a

+ 24 - 0
AvaloniaApplication2/App.axaml

@@ -11,5 +11,29 @@
   
     <Application.Styles>
         <FluentTheme />
+		<Style Selector="TextBlock">
+			<Setter Property="Background" Value="Aqua"/>
+		</Style>
+		<Style Selector="TextBlock.MyTB">
+			<Setter Property="Background" Value="Bisque"/>
+		</Style>
+		<Style Selector="TextBlock.MyTB:pointerover">
+			<Setter Property="Background" Value="Bisque"/>
+			<Setter Property="FontSize" Value="48"/>
+		</Style>
+		<Style Selector="TextBlock.AnimatedTB">
+			<Style.Animations>
+				<Animation Duration="0:0:3" IterationCount="INFINITE" PlaybackDirection="Alternate" Easing="BounceEaseIn">
+					<KeyFrame Cue="0%">
+						<Setter Property="FontSize" Value="12"/>
+					</KeyFrame>
+					<KeyFrame Cue="100%">
+						<Setter Property="FontSize" Value="48"/>
+					</KeyFrame>
+				</Animation>
+			</Style.Animations>
+		</Style>
+		
+		
     </Application.Styles>
 </Application>

+ 30 - 0
AvaloniaApplication2/Resources/DateConverter.cs

@@ -0,0 +1,30 @@
+using Avalonia.Data.Converters;
+using System;
+using System.Collections.Generic;
+using System.Globalization;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace AvaloniaApplication2.Resources
+{
+    internal class DateConverter : IValueConverter
+    {
+        // from VM to Viev
+        public object? Convert(object? value, Type targetType, object? parameter, CultureInfo culture)
+        {         
+            if (value is DateTime) return new DateTimeOffset((DateTime)value, TimeSpan.Zero); 
+            return null;
+        }
+
+        //from View to VM
+        public object? ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture)
+        {
+            if (value is DateTimeOffset myDateTimeOffset)
+            {
+                return new DateTime(myDateTimeOffset.Year, myDateTimeOffset.Month, myDateTimeOffset.Day);
+            }
+            return null;
+        }
+    }
+}

+ 18 - 0
AvaloniaApplication2/Resources/UserLogicalConverter.cs

@@ -0,0 +1,18 @@
+using Avalonia.Data.Converters;
+using System;
+using System.Collections.Generic;
+using System.Globalization;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace AvaloniaApplication2.Resources
+{
+    internal class UserLogicalConverter : IMultiValueConverter
+    {
+        public object? Convert(IList<object?> values, Type targetType, object? parameter, CultureInfo culture)
+        {
+            return !(values[0].Equals(values[1]));
+        }
+    }
+}

+ 4 - 1
AvaloniaApplication2/ViewModels/MainWindowViewModel.cs

@@ -1,5 +1,6 @@
 using Avalonia.Controls;
 using AvaloniaApplication2.Models;
+using AvaloniaApplication2.Views;
 using CommunityToolkit.Mvvm.ComponentModel;
 using CommunityToolkit.Mvvm.Input;
 using Microsoft.EntityFrameworkCore;
@@ -43,10 +44,12 @@ namespace AvaloniaApplication2.ViewModels
                 switch (CurrentUser.IdRole)
                 {
                     case 1:
-                        Page = new PageStartAdmin();
+                        //Page = new PageStartAdmin();
+                        Page = new PageAdminNew();
                         break;
                     case 2:
                         page2VM = new Page2ViewModel(currentUser.User.IdUser);
+                       // Page = new Page2New();
                         Page = new Page2();
                         break;
                 }                   

+ 3 - 0
AvaloniaApplication2/ViewModels/PageStartAdminViewModel.cs

@@ -19,6 +19,9 @@ namespace AvaloniaApplication2.ViewModels
         [ObservableProperty] string textFind;
         [ObservableProperty] List<Gender> genders;
         [ObservableProperty] Gender filteredGender;
+
+
+        public int ID => 1;
       public PageStartAdminViewModel() 
         {
             users = db.Users.Include(x => x.IdUserNavigation.IdRoleNavigation).ToList();

+ 2 - 2
AvaloniaApplication2/Views/MainWindow.axaml

@@ -17,9 +17,9 @@
     </Design.DataContext>
 	<!--<ContentControl Content="{Binding Page}" />-->
 	<Grid ColumnDefinitions="200,*" RowDefinitions="50,*,50">
-		<TextBlock Text="Заголовок" Grid.ColumnSpan="2" HorizontalAlignment="Center" VerticalAlignment="Center"/>
+		<TextBlock Text="Заголовок" Grid.ColumnSpan="2" HorizontalAlignment="Center" VerticalAlignment="Center" Classes="MyTB"/>
 		<TextBlock Text="Меню" Grid.Row="1" Grid.RowSpan="2" HorizontalAlignment="Center" VerticalAlignment="Center"/>
-		<TextBlock Text="Футер" Grid.Column="1" Grid.Row="2" HorizontalAlignment="Center" VerticalAlignment="Center"/>
+		<TextBlock Text="Футер" Grid.Column="1" Grid.Row="2" HorizontalAlignment="Center" VerticalAlignment="Center" Classes="AnimatedTB"/>
 		<ContentControl Content="{Binding Page}" Grid.Column="1" Grid.Row="1"/>
 	</Grid>
 	

+ 13 - 3
AvaloniaApplication2/Views/Page2.axaml

@@ -5,12 +5,22 @@
              mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
              x:Class="AvaloniaApplication2.Page2"
 			 xmlns:vm="using:AvaloniaApplication2.ViewModels"
-			 x:DataType="vm:MainWindowViewModel">
+			 x:DataType="vm:MainWindowViewModel"
+			 xmlns:conv="using:AvaloniaApplication2.Resources">
+	<UserControl.Resources>
+		<conv:DateConverter x:Key="myDateConverter"/>
+	</UserControl.Resources>
+	<UserControl.Styles>
+		<Style Selector="TextBlock">
+			<Setter Property="Background" Value="Red"/>
+		</Style>
+	</UserControl.Styles>
 	<StackPanel>
 		<Image Source="{Binding Page2VM.ImageUser}" Width="100" Height="100"/>
-		<TextBox Text="{Binding Page2VM.EditedUser.Name}"/>
+		<TextBox Text="{Binding Page2VM.EditedUser.Name}" />
 		<TextBox Text="{Binding Page2VM.EditedUser.BirthDate}"/>		
-		<DatePicker SelectedDate="{Binding Page2VM.DateTimeOffset}"/>
+		<!--<DatePicker SelectedDate="{Binding Page2VM.DateTimeOffset}"/>-->
+		<DatePicker SelectedDate="{Binding Page2VM.EditedUser.BirthDate, Converter={StaticResource myDateConverter}}"/>
 		<ComboBox ItemsSource="{Binding Page2VM.GendersList}" SelectedItem="{Binding Page2VM.EditedUser.IdGenderNavigation}">
 			<ComboBox.ItemTemplate>
 				<DataTemplate>

+ 26 - 0
AvaloniaApplication2/Views/Page2New.axaml

@@ -0,0 +1,26 @@
+<UserControl xmlns="https://github.com/avaloniaui"
+             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
+             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
+             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"          
+			 xmlns:vm="using:AvaloniaApplication2.ViewModels"
+			 mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
+			 x:Class="AvaloniaApplication2.Views.Page2New"
+			 x:DataType="vm:MainWindowViewModel">
+	<StackPanel>
+		<Image Source="{Binding Page2VM.ImageUser}" Width="100" Height="100"/>
+		<TextBox Text="{Binding Page2VM.EditedUser.Name}"/>
+		<TextBox Text="{Binding Page2VM.EditedUser.BirthDate}"/>
+		<DatePicker SelectedDate="{Binding Page2VM.DateTimeOffset}"/>
+		<ComboBox ItemsSource="{Binding Page2VM.GendersList}" SelectedItem="{Binding Page2VM.EditedUser.IdGenderNavigation}">
+			<ComboBox.ItemTemplate>
+				<DataTemplate>
+					<TextBlock Text="{Binding Gender1}"/>
+				</DataTemplate>
+			</ComboBox.ItemTemplate>
+		</ComboBox>
+		<Button Content="Выбрать изображение пользователя" Command="{Binding Page2VM.Image}"/>
+		<Button Content="Сохранить изменения" Command="{Binding Page2VM.Save}"/>
+		<Button Content="Развойти обратно" Command="{Binding Page2VM.GoBack}"/>
+	</StackPanel>
+
+</UserControl>

+ 13 - 0
AvaloniaApplication2/Views/Page2New.axaml.cs

@@ -0,0 +1,13 @@
+using Avalonia;
+using Avalonia.Controls;
+using Avalonia.Markup.Xaml;
+
+namespace AvaloniaApplication2.Views;
+
+public partial class Page2New : UserControl
+{
+    public Page2New()
+    {
+        InitializeComponent();
+    }
+}

+ 46 - 0
AvaloniaApplication2/Views/PageAdminNew.axaml

@@ -0,0 +1,46 @@
+<UserControl xmlns="https://github.com/avaloniaui"
+             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
+             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
+             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
+             mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
+             x:Class="AvaloniaApplication2.Views.PageAdminNew"
+			 xmlns:vm="using:AvaloniaApplication2.ViewModels"
+			 x:DataType="vm:PageStartAdminViewModel">
+	<Design.DataContext>
+		<vm:PageStartAdminViewModel/>
+	</Design.DataContext>
+	
+	
+	<Grid RowDefinitions="70,*">
+		<StackPanel Orientation="Horizontal" Grid.Row="0">
+			<TextBlock Text="Поиск:"/>
+			<TextBox Text="{Binding TextFind}" Watermark="Введите текст для поиска"/>
+			<TextBlock Text="Сортировка по дате рождения:"/>
+			<StackPanel>
+				<RadioButton Content="По возрастанию" Command="{Binding DateSort}" CommandParameter="1"/>
+				<RadioButton Content="По убыванию" Command="{Binding DateSort}" CommandParameter="2"/>
+			</StackPanel>
+			<TextBlock Text="Фильтр по полу"/>
+			<ComboBox ItemsSource="{Binding Genders}" SelectedItem="{Binding FilteredGender}">
+				<ComboBox.ItemTemplate>
+					<DataTemplate>
+						<TextBlock Text="{Binding Gender1}"/>
+					</DataTemplate>
+				</ComboBox.ItemTemplate>
+			</ComboBox>
+		</StackPanel>
+		<ListBox ItemsSource="{Binding Users}" Grid.Row="1">
+			<ListBox.ItemTemplate>
+				<DataTemplate>
+					<StackPanel>
+						<TextBlock Text="{Binding Name, StringFormat=Имя: {0}}"/>
+						<TextBlock Text="{Binding BirthDate,StringFormat=Дата рождения: {0:dd/MM/yyyy}}"/>
+						<TextBlock Text="{Binding IdGenderNavigation.Gender1, StringFormat=Пол: {0}}"/>
+						<TextBlock Text="{Binding IdUserNavigation.IdRoleNavigation.RoleName, StringFormat=Роль в системе: {0}}"/>
+						<Button Content="Изменить персональные данные пользователя" Command="{Binding $parent[UserControl].((vm:PageStartAdminViewModel)DataContext).Press}" CommandParameter="{Binding IdUser}" IsEnabled="{Binding !!IdUser}"/>
+					</StackPanel>
+				</DataTemplate>
+			</ListBox.ItemTemplate>
+		</ListBox>
+	</Grid>
+</UserControl>

+ 15 - 0
AvaloniaApplication2/Views/PageAdminNew.axaml.cs

@@ -0,0 +1,15 @@
+using Avalonia;
+using Avalonia.Controls;
+using Avalonia.Markup.Xaml;
+using AvaloniaApplication2.ViewModels;
+
+namespace AvaloniaApplication2.Views;
+
+public partial class PageAdminNew : UserControl
+{
+    public PageAdminNew()
+    {
+        InitializeComponent();
+        DataContext = new PageStartAdminViewModel();
+    }
+}

+ 15 - 2
AvaloniaApplication2/Views/PageStartAdmin.axaml

@@ -5,7 +5,13 @@
              mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
              x:Class="AvaloniaApplication2.PageStartAdmin"
 			 xmlns:vm="using:AvaloniaApplication2.ViewModels"
-			 x:DataType="vm:PageStartAdminViewModel">
+			 x:DataType="vm:PageStartAdminViewModel"
+			 xmlns:conv="using:AvaloniaApplication2.Resources">
+	<UserControl.Resources>
+		<conv:UserLogicalConverter x:Key="myUserLogicalConverter"/>
+	</UserControl.Resources>
+	
+
 	<Grid RowDefinitions="70,*">		
 			<StackPanel Orientation="Horizontal" Grid.Row="0">
 				<TextBlock Text="Поиск:"/>
@@ -32,7 +38,14 @@
 							<TextBlock Text="{Binding BirthDate,StringFormat=Дата рождения: {0:dd/MM/yyyy}}"/>
 							<TextBlock Text="{Binding IdGenderNavigation.Gender1, StringFormat=Пол: {0}}"/>
 							<TextBlock Text="{Binding IdUserNavigation.IdRoleNavigation.RoleName, StringFormat=Роль в системе: {0}}"/>
-							<Button Content="Изменить персональные данные пользователя" Command="{Binding $parent[UserControl].((vm:PageStartAdminViewModel)DataContext).Press}" CommandParameter="{Binding IdUser}"/>
+							<Button Content="Изменить персональные данные пользователя" Command="{Binding $parent[UserControl].((vm:PageStartAdminViewModel)DataContext).Press}" CommandParameter="{Binding IdUser}">
+								<Button.IsVisible>
+									<MultiBinding Converter="{StaticResource myUserLogicalConverter}">
+										<Binding Path="IdUser"/>
+										<Binding Path="((vm:PageStartAdminViewModel)DataContext).ID" RelativeSource="{RelativeSource Mode=FindAncestor,AncestorType=UserControl}"/>
+									</MultiBinding>
+								</Button.IsVisible>
+							</Button>
 						</StackPanel>
 					</DataTemplate>
 				</ListBox.ItemTemplate>

+ 1 - 1
AvaloniaApplication2/Views/PageStartAdmin.axaml.cs

@@ -12,6 +12,6 @@ public partial class PageStartAdmin : UserControl
     public PageStartAdmin()
     {
         InitializeComponent();
-        DataContext = new PageStartAdminViewModel();
+       DataContext = new PageStartAdminViewModel();
     }
 }