WPF 下拉框绑定 示例1

Window1.xaml

<Window
    x:Class="MedicalRecord.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
    xmlns:Extend="clr-namespace:MedicalRecord"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:local="clr-namespace:MedicalRecord"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    Title="Window1"
    Width="800"
    Height="450"
    mc:Ignorable="d"       
    
    xmlns:prism="http://prismlibrary.com/"
        prism:ViewModelLocator.AutoWireViewModel="True">
    
    
    <Extend:Form Width="500" HorizontalAlignment="Left">
        <Expander
            Extend:Form.IsItemItsOwnContainer="True"
            Header="用户信息"
            IsExpanded="True">
            <Extend:Form Padding="0" Grid.IsSharedSizeScope="False">
                <TextBox
                    Extend:Form.IsRequired="True"
                    Extend:Form.Label="用户名"
                    Text="{Binding Name, Mode=TwoWay}" />
                <PasswordBox Extend:Form.IsRequired="True" Extend:Form.Label="密码" />

                <ComboBox ItemsSource="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}, 
    Path=DataContext.WifeList}" x:Name="_Printer" 
     DisplayMemberPath="ChName"  SelectedValuePath="EnName"
    SelectedValue="{Binding Wife, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}">

                    <i:Interaction.Triggers>
                        <i:EventTrigger EventName="SelectionChanged">
                            <i:InvokeCommandAction Command="{Binding selectItemChangedCommand}"   
                                                       CommandParameter="{Binding ElementName=_Printer,Path=SelectedIndex}"/>
                        </i:EventTrigger>

                    </i:Interaction.Triggers>

                </ComboBox>






            </Extend:Form>
        </Expander>
    </Extend:Form>
</Window>

Window1.xaml.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
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.Shapes;

namespace MedicalRecord
{
    /// <summary>
    /// Window1.xaml 的交互逻辑
    /// </summary>
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
        }
    }
}

Window1ViewModel.cs

using MedicalRecord.ComponentModule.ViewModels;
using Prism.Commands;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace MedicalRecord
{
    public class Window1ViewModel
    {

        private DelegateCommand<object> _selectItemChangedCommand;
        public DelegateCommand<object> selectItemChangedCommand =>
   _selectItemChangedCommand ?? (_selectItemChangedCommand = new DelegateCommand<object>(BindViewModelObject));

        async void BindViewModelObject(object obj)
        {
            if (obj != null)
            {
                MessageBox.Show(obj.ToString());
                //objectMap(obj as MedicalRecordCatalogue);
            }
        }

        public ObservableCollection<Person> PersonList =>
            new ObservableCollection<Person>
            {
                new Person{UserName="孙悟空",UserId="1"},
                new Person{UserName="猪八戒",UserId="2"},
                new Person{UserName="沙和尚",UserId="3"},
                new Person{UserName="唐僧",UserId="4"},
            };

        public ObservableCollection<WifeInfo> WifeList =>
            new ObservableCollection<WifeInfo>
            {
                new WifeInfo{ChName="铁扇公主",EnName="Mary"},
                new WifeInfo{ChName="观音菩萨",EnName="Lily"},
                new WifeInfo{ChName="女儿国国王",EnName="Lisa"},
                new WifeInfo{ChName="兔子精",EnName="White"},
            };
    }


















}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值