Avlonia CombBox (ViewModels) 範例
Avlonia CombBox (ViewModels) 範例
資料來源: copilot + 自己
(XAML)
<Window 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" xmlns:vm="using:VPOS_Avalonia.ViewModels" x:DataType="vm:MemberAddressViewModel" xmlns:local="clr-namespace:VPOS_Avalonia" x:Class="VPOS_Avalonia.MemberAddress" Title="VPOS" Loaded="Window_Loaded"> <!-- ViewModels 使用步驟: ▲[固定語法(寫在axaml)] xmlns:vm="using:VPOS_Avalonia.ViewModels" ▲[對應語法(寫在axaml)] x:DataType="vm:MemberAddressViewModel" ▲[對應語法(寫在axaml)] <Design.DataContext> ▲[對應語法(寫在cs)] DataContext = new MemberAddressViewModel();//建構子函數中資源檔載入[InitializeComponent();]前必須指定ViewModel --> <Design.DataContext> <vm:MemberAddressViewModel/> </Design.DataContext> <Border BorderBrush="LightBlue" BorderThickness="5" CornerRadius="10" Margin="-3"> <Grid x:Name="FullGrid" RowDefinitions ="*,*,*,*,*,1.2*" ColumnDefinitions="*,0.5*,1.2*,1.1*,1.2*,0.5*,*" Margin="10,10,10,10"> <TextBlock x:Name="TitleLabel" Text="常用地址" Foreground="White" FontSize="20" VerticalAlignment="Center" HorizontalAlignment="Center" Grid.Row="0" Grid.ColumnSpan="7"/> <Grid x:Name="BodyGrid" Grid.Row="1" Grid.Column="0" Grid.RowSpan="4" Grid.ColumnSpan="7" RowDefinitions ="*,*,*,*" ColumnDefinitions="*,2*,*,2*,0.5*" > <TextBlock x:Name="Addrlab00" Text="標 籤" FontSize="20" Foreground="White" VerticalAlignment="Center" HorizontalAlignment="Center" Background="#194a6e" Margin="0,0,0,0" Grid.Row="0" Grid.Column="0"/> <TextBox x:Name="Addrtxt01" Background="White" Foreground="Blue" FontSize="20" Text="" Grid.Row="0" Grid.Column="1" VerticalAlignment="Center" Margin="0,0,0,0"/> <TextBlock x:Name="Addrlab10" Text="縣 市" FontSize="20" Foreground="White" VerticalAlignment="Center" HorizontalAlignment="Center" Background="#194a6e" Margin="0,0,0,0" Grid.Row="1" Grid.Column="0"/> <ComboBox x:Name="Picker11" ItemsSource="{Binding Picker11List}" DisplayMemberBinding="{Binding Picker_Name}" VerticalAlignment="Center" HorizontalAlignment="Stretch" Background="White" FontSize="20" Grid.Row="1" Grid.Column="1" SelectionChanged="Picker11_SelectedIndexChanged"/> <TextBlock x:Name="Addrlab12" Text="區 域" FontSize="20" Foreground="White" VerticalAlignment="Center" HorizontalAlignment="Center" Background="#194a6e" Margin="0,0,0,0" Grid.Row="1" Grid.Column="2"/> <ComboBox x:Name="Picker13" ItemsSource="{Binding Picker13List}" DisplayMemberBinding="{Binding Picker_Name}" VerticalAlignment="Center" HorizontalAlignment="Stretch" Background="White" FontSize="20" Grid.Row="1" Grid.Column="3"/> <TextBlock x:Name="Addrlab20" Text="地 址" FontSize="20" Foreground="White" VerticalAlignment="Center" HorizontalAlignment="Center" Background="#194a6e" Margin="0,0,0,0" Grid.Row="2" Grid.Column="0"/> <TextBox x:Name="Addrtxt21" Background="White" Foreground="Blue" FontSize="20" Text="" Grid.Row="2" Grid.Column="1" VerticalAlignment="Stretch" Margin="0,0,0,10" Grid.RowSpan="2" Grid.ColumnSpan="4"/> </Grid> <local:CustBtn x:Name="CloseBtn00" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Grid.Row="5" Grid.Column="2"/> <local:CustBtn x:Name="CloseBtn01" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Grid.Row="5" Grid.Column="4"/> <Image x:Name="KeyboardBtn" Source="avares://VPOS_Avalonia/Assets/keyboard.png" Grid.Row="5" Grid.Column="6" Width="60" VerticalAlignment="Center" HorizontalAlignment="Right" PointerPressed="KeyboardBtn_Clicked"/> </Grid> </Border> </Window>
(Code)
using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.ComponentModel; using System.Linq; using System.Text; using System.Threading.Tasks; namespace VPOS_Avalonia.ViewModels; //--- //Picker 型態區 public class PickerModel : INotifyPropertyChanged { private string _Picker_Name; public string Picker_Name {//ListBox 程式刷新顯示資料 get => _Picker_Name; set { if (_Picker_Name != value) { _Picker_Name = value; OnPropertyChanged(nameof(Picker_Name)); } } } private string _Picker_Value; public string Picker_Value {//ListBox 程式刷新顯示資料 get => _Picker_Value; set { if (_Picker_Value != value) { _Picker_Value = value; OnPropertyChanged(nameof(Picker_Value)); } } } public event PropertyChangedEventHandler PropertyChanged; protected virtual void OnPropertyChanged(string propertyName) { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); } public PickerModel(String Name = "", String Value = "") { Picker_Name = Name; Picker_Value = Value; } } //---Picker 型態區 public class MemberAddressViewModel : ViewModelBase { public ObservableCollection<PickerModel> Picker11List { get; set; } = new ObservableCollection<PickerModel>(); public ObservableCollection<PickerModel> Picker13List { get; set; } = new ObservableCollection<PickerModel>(); public PickerModel SelectedPicker11 = new PickerModel(); public PickerModel SelectedPicker13 = new PickerModel(); }
using Avalonia; using Avalonia.Controls; using Avalonia.Interactivity; using Avalonia.Markup.Xaml; using Avalonia.Media; using System; using System.Data; using VPOS; using VPOS_Avalonia.ViewModels; using VPOS_Avalonia.Views; namespace VPOS_Avalonia; public partial class MemberAddress : Window { public static bool m_blnRun = false; public static string m_StrResult = ""; private string m_Straddress_sid = ""; public MemberAddress(string StrData = "") { DataContext = new MemberAddressViewModel(); //隱藏工具列 this.ExtendClientAreaToDecorationsHint = true; this.ExtendClientAreaTitleBarHeightHint = -1; this.SystemDecorations = SystemDecorations.None; //---隱藏工具列 WindowStartupLocation = WindowStartupLocation.CenterScreen;//視窗置中 this.Background = new SolidColorBrush(Color.Parse("#ff194a6e"));//設定背景色 InitializeComponent(); this.Width = 600; this.Height = 320; m_blnRun = false; m_StrResult = ""; m_Straddress_sid = DateTime.Now.ToString("yyyyMMddHHmmss"); IBrush BackgroundColor = Brush.Parse("#FF2470A0"); double dblFontSize = (MainWindow.m_dblScreenWidth == 1024) ? 18 : 20; TitleLabel.FontSize = dblFontSize; Addrlab00.FontSize = dblFontSize; Addrtxt01.FontSize = dblFontSize; Addrlab10.FontSize = dblFontSize; Picker11.FontSize = dblFontSize; Addrlab12.FontSize = dblFontSize; Picker13.FontSize = dblFontSize; Addrlab20.FontSize = dblFontSize; Addrtxt21.FontSize = dblFontSize; if ((SQLDataTableModel.m_city_code_dataDataTable != null) && (SQLDataTableModel.m_city_code_dataDataTable.Rows.Count > 0)) { DataRow[] DataRows = SQLDataTableModel.m_city_code_dataDataTable.Select("code_type='C'"); for (int i = 0; i < DataRows.Length; i++) { PickerModel PickerModelBuf = new PickerModel(); PickerModelBuf.Picker_Value = DataRows[i]["code_no"].ToString(); PickerModelBuf.Picker_Name = DataRows[i]["show_name"].ToString(); ((MemberAddressViewModel)(DataContext)).Picker11List.Add(PickerModelBuf); } } CloseBtn00.InitDisplay(Brushes.White, BackgroundColor, BackgroundColor, "確定", dblFontSize); CloseBtn00.m_intSID = 0; CloseBtn01.InitDisplay(Brushes.White, BackgroundColor, BackgroundColor, "取消", dblFontSize); CloseBtn01.m_intSID = 1; CloseBtn00.ExternalClicked += CloseBtn_Clicked; CloseBtn01.ExternalClicked += CloseBtn_Clicked; FullGrid.IsVisible = false; SetUI(StrData); } public void Window_Loaded(object sender, RoutedEventArgs e) { FullGrid.IsVisible = true; Addrtxt01.Focus(); } private void SetUI(String StrData) { if (StrData.Length > 0) { string[] strs = StrData.Split("㊣"); m_Straddress_sid = strs[0]; Addrtxt01.Text = strs[1]; for (int i = 0; i < ((MemberAddressViewModel)(DataContext)).Picker11List.Count; i++) { if (((MemberAddressViewModel)(DataContext)).Picker11List[i].Picker_Name == strs[2]) { Picker11.SelectedIndex = i; break; } } for (int i = 0; i < ((MemberAddressViewModel)(DataContext)).Picker13List.Count; i++) { if (((MemberAddressViewModel)(DataContext)).Picker13List[i].Picker_Name == strs[3]) { Picker13.SelectedIndex = i; break; } } Addrtxt21.Text = strs[4]; } } private void On_OK() { if ((Addrtxt01.Text.Length > 0) && (Picker11.SelectedIndex >= 0) && (Picker13.SelectedIndex >= 0) && (Addrtxt21.Text.Length > 0)) { m_StrResult = $"{m_Straddress_sid}㊣{Addrtxt01.Text}㊣{((PickerModel)Picker11.SelectedItem).Picker_Name}㊣{((PickerModel)Picker13.SelectedItem).Picker_Name}㊣{Addrtxt21.Text}"; } else { m_StrResult = ""; } if (m_StrResult.Length > 0) { m_blnRun = true; this.Close(); } else { m_blnRun = false; } } private void CloseBtn_Clicked(object sender, RoutedEventArgs e) { switch (((CustBtn)sender).m_intSID) { case 0: On_OK(); break; case 1: m_StrResult = ""; m_blnRun = false; this.Close(); break; } } private void KeyboardBtn_Clicked(object sender, RoutedEventArgs e) { MainWindow.ShowSoftKeyboard(); } private void Picker11_SelectedIndexChanged(object sender, RoutedEventArgs e)//縣市選擇事件 { if (Picker11.SelectedIndex >= 0) { ((MemberAddressViewModel)(DataContext)).Picker13List.Clear(); PickerModel PickerModelMain = (PickerModel)Picker11.SelectedItem; DataRow[] DataRows = SQLDataTableModel.m_city_code_dataDataTable.Select($"parent_code='{PickerModelMain.Picker_Value}'"); for (int i = 0; i < DataRows.Length; i++) { PickerModel PickerModelBuf = new PickerModel(); PickerModelBuf.Picker_Value = DataRows[i]["code_no"].ToString(); PickerModelBuf.Picker_Name = DataRows[i]["show_name"].ToString(); ((MemberAddressViewModel)(DataContext)).Picker13List.Add(PickerModelBuf); } if (((MemberAddressViewModel)(DataContext)).Picker13List.Count > 0) { Picker13.SelectedIndex = 0; } } } }