亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

通過數據綁定WPF/MVVM從數據庫填充的組合框中獲取選定的值,以進行比較

通過數據綁定WPF/MVVM從數據庫填充的組合框中獲取選定的值,以進行比較

C#
蠱毒傳說 2023-07-09 10:05:52
我是 C# 初學者;該項目是在 Visual Studio (wpf) 中使用實體框架和視圖模型制作的。我僅使用數據庫中的“員工”姓名填充了組合框。我想要做的是獲取變量中選定的項目值(或直接將其與數據庫中存儲的名稱進行比較,以獲取與該名稱關聯的所有其他表列;在我的情況下,我有年齡、屬性等)如果我直接進入MainWindow.xaml.cs,我可以輕松使用Object selectedItem = ComboBoxName.SelectedItem; 它完成了工作。但是,我需要在我的MainWindowsViewModel.cs. 我發現SelectedItem="{Binding ..., Mode=...}"并嘗試了幾種解決方案,但找不到任何可行的解決方案。我的 XAML 中的組合框: <ComboBox x:Name="comboPersonal" Grid.Column="6" HorizontalAlignment="Left" Margin="13,60,-62,0" Grid.Row="1" VerticalAlignment="Top" Width="183" ItemsSource="{Binding ang1}" SelectedItem="{Binding Path=Employers, Mode=TwoWay}"  />組合框通過以下方式填充: public IList<string> ang1        {            get            {                using (ProbaHotel_1Entities db = new ProbaHotel_1Entities())                {                    var employee = db.Personal.Select(ang => ang.Nume).ToList();                    return employee.ToList();                }            }        }在我的 MainWindowViewModel 中(這大部分是錯誤的): public IList<string> Employers        {            get            {                using (ProbaHotel_1Entities db = new ProbaHotel_1Entities())                {                    var vNume = db.Personal.Select(ang => ang.Nume);                    this.NumeText = vNume.ToString();                }                return this.Employers;            }            set { }        }NumeText是文本框綁定到的變量。當我從組合框中選擇一個項目時,我希望將該值傳輸到文本框。Select 語句錯過了WHERE與selected valuenames in the database文本框XAML:<TextBox x:Name="text_nume" HorizontalAlignment="Left" Height="23"  TextWrapping="Wrap" Text="{Binding NumeText}" VerticalAlignment="Top" Width="89" Grid.Column="1" Grid.Row="0" />在我的 MainWindowViewModel 中:private string numeText;        public string NumeText        {            get            {                return this.numeText;            }            set            {                this.numeText = value;            }        }
查看完整描述

1 回答

?
慕妹3242003

TA貢獻1824條經驗 獲得超6個贊

您可以使用包含模型的 List 并綁定到組合框項目源,并且可以綁定 DisplayMemberPath 來表示“Name”(模型的屬性)并將 SelectedItem 綁定到該模型。


請看代碼。我使用了虛擬字符串值。在您的情況下,這應該是從數據庫填充的,正如您提到的。請注意,在 SelectedEmployee 的設置器中,我根據您的要求為 NumText 分配值。一旦選定的項目發生更改,它將被分配并顯示在 XAML 文本框中


在您的情況下,您錯誤地將 SelectedItem 分配給 list 。它應該與您綁定的itemsource相關。您創建了單獨的列表,一個用于項目源,另一個用于所選項目。而且我在代碼中看不到與 INotifyPropertyChanged 相關的屬性設置器的任何更改。


XAML:


<Window x:Class="WpfApplication13.MainWindow"

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

        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:local="clr-namespace:WpfApplication13"

        mc:Ignorable="d"

        Title="MainWindow" Height="350" Width="525">

    <Grid>

        <Grid.RowDefinitions>

            <RowDefinition Height="50*"/>

            <RowDefinition Height="50*"/>

        </Grid.RowDefinitions>

        <TextBox x:Name="text_nume" HorizontalAlignment="Center" Height="23"  TextWrapping="Wrap" Text="{Binding NumeText}" VerticalAlignment="Center" Width="89" Grid.Row="0" />

        <ComboBox x:Name="comboPersonal" Grid.Row="1" Height="30" Width="100" HorizontalAlignment="Center" DisplayMemberPath="Name" VerticalAlignment="Center" Margin="13,60,-62,0"  ItemsSource="{Binding Ang1}" SelectedItem="{Binding SelectedEmployee}"  />

    </Grid>

</Window>

視圖模型:


 public class ViewModel : INotifyPropertyChanged

    {


        #region Constants and Enums


        #endregion


        #region Private and Protected Member Variables 

        private IList<EmployeeModel> ang1;

        EmployeeModel _selectedEmployee;

        private string numeText;

        #endregion


        #region Private and Protected Methods

        private void OnPropertyChanged(string propName)

        {

            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propName));

        }

        #endregion


        #region Constructors

        public ViewModel()

        {

            Ang1 = new List<EmployeeModel>();

            Ang1.Add(new EmployeeModel() {  Name="1"});

            Ang1.Add(new EmployeeModel() { Name = "2" });

        }

        #endregion


        #region Public Properties

        public IList<EmployeeModel> Ang1

        {

            get

            {

                return ang1;

            }

            set

            {

                ang1 = value;

                OnPropertyChanged(nameof(Ang1));

            }

        }


        public EmployeeModel SelectedEmployee

        {

            get

            {

                return _selectedEmployee;

            }

            set

            {

                _selectedEmployee = value;

                NumeText = value.Name;

                OnPropertyChanged(nameof(SelectedEmployee));

            }

        }


        public string NumeText

        {

            get

            {

                return this.numeText;

            }

            set

            {


                this.numeText = value;

                OnPropertyChanged(nameof(NumeText));

            }

        }

        #endregion


        #region Public Methods

        public event PropertyChangedEventHandler PropertyChanged;


        #endregion



    }

模型:


 public class EmployeeModel

    {

        public string Name { get; set; }

    }

我使用 INotifyPropertyChanged 來綁定通知。讓我知道這是否有幫助


查看完整回答
反對 回復 2023-07-09
  • 1 回答
  • 0 關注
  • 147 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號