MaterialDesign Or ComboBox 联动查询

 <ComboBox Width="200" Height="30" x:Name="ComboxName" Text="{Binding CboxText,Mode=TwoWay}" ItemsSource="{Binding TenantsModel}" SelectedItem="{Binding Model}" DisplayMemberPath="name"  IsEditable="True" materialDesign:HintAssist.Hint="{Binding QueryNmae}" >
                <i:Interaction.Triggers>
                    <i:EventTrigger EventName="SelectionChanged">
                        <i:InvokeCommandAction Command="{Binding QueryTenantsCommand}" CommandParameter="{Binding ElementName=ComboxName}"/>
                    </i:EventTrigger>
                </i:Interaction.Triggers>
            </ComboBox>

后台代码

 public class GetTenants
    {
        public string tenancyName { get; set; }
        public string name { get; set; }
        public string editionDisplayName { get; set; }
        public string connectionString { get; set; }
        public string isActive { get; set; }
        public string creationTime { get; set; }
        public string subscriptionEndDateUtc { get; set; }
        public string editionId { get; set; }
        public string isInTrialPeriod { get; set; }

        public string id { get; set; }
    }
    public class MainViewModel : ViewModelBase
    {
        
      
        private ObservableCollection<GetTenants> _TenantsModel;

        public ObservableCollection<GetTenants> TenantsModel
        {
            get { return _TenantsModel; }
            set { _TenantsModel = value;RaisePropertyChanged(); }
        }

        private string userName = string.Empty;
        private string passWord = string.Empty;
        public static string Token = string.Empty;
        public string Password
        {
            get { return passWord; }
            set { passWord = value; RaisePropertyChanged(); }
        }
        public string UserName
        {
            get { return userName; }
            set { userName = value; RaisePropertyChanged(); }
        }

        private GetTenants _Model;

        public GetTenants Model
        {
            get { return _Model; }
            set { _Model = value;RaisePropertyChanged(); 
                 }
        }

        private string _CboxText;

        public string CboxText
        {
            get { return _CboxText; }
            set { _CboxText = value;RaisePropertyChanged(); QueryTenantsList(value); }
        }
        private string _QueryNmae;

        public string QueryNmae
        {
            get { return _QueryNmae; }
            set { _QueryNmae = value; RaisePropertyChanged(); }
        }

        public  RelayCommand<object> QueryTenantsCommand { get; set; }
        public RelayCommand Close { get; set; }
     
        public RelayCommand Logasync { get; private set; }


        public MainViewModel()
        {
           QueryTenantsCommand = new RelayCommand<object>((o) => SelectModel(o));
            TenantsModel = new ObservableCollection<GetTenants>();
            Close = new RelayCommand(()=> ApplicationShutdown());
            Logasync = new RelayCommand(()=>LogCommand());
        }

        private async void LogCommand()
        {
            if (string.IsNullOrEmpty(UserName) && string.IsNullOrEmpty(Password)) return;
            JObject jObjects = new JObject();
            jObjects["userNameOrEmailAddress"] = UserName;
            jObjects["password"] = Password;
            var LoginTask = QueryOrUpdateHttp.LoginAsync("http://47.106.71.73:5000/api/TokenAuth/Authenticate", jObjects);
            var timeouttask = Task.Delay(3000);
            var completedTask = await Task.WhenAny(LoginTask, timeouttask);
            if (completedTask == timeouttask) return;
            var jObject = JObject.Parse(LoginTask.Result.ToString());
            if (string.IsNullOrEmpty(jObject.ToString()) || !string.IsNullOrEmpty(jObject["error"].ToString())) return;
            Token = jObject["result"]["accessToken"].ToString();
            if (string.IsNullOrEmpty(Token)) return;
        }

        public  void ApplicationShutdown()
        {
            Messenger.Default.Send("", "ApplicationShutdown");
        }

        private  void SelectModel(object o)
        {
            GetTenants combo = (o as ComboBox).SelectedItem as GetTenants;
            if (combo == null) return;

            QueryNmae =  combo.name.Trim();

        }

        public  static ObservableCollection<T> DeserializeJsonToList<T>(string json) where T : class
        {
            JsonSerializer serializer = new JsonSerializer();
            using (StringReader sr = new StringReader(json))
            {
           object o =  serializer.Deserialize(new JsonTextReader(sr), typeof(ObservableCollection<T>));
            ObservableCollection<T> list = o as ObservableCollection<T>;
            return list;
            } 
          
        }
        private async void QueryTenantsList(string value)
        {
            if (string.IsNullOrEmpty(value)) return;
            GetTenants loadValue = new GetTenants() {  id="1", name="正在加载...."};
            TenantsModel.Add( loadValue);
            string result = await QueryOrUpdateHttp.GetTenantId("/api/services/app/Tenant/GetTenants?Filter=" + CboxText);
            var JResult = JObject.Parse(result);

            if (bool.Parse(JResult["success"].ToString()))
            {
                var results = JResult["result"]["items"].ToString();
                TenantsModel.Clear(); 

                TenantsModel = DeserializeJsonToList<GetTenants>(results);
               
            }
            else
            {
                MessageBox.Show("没有改公司名称");
            }
        }

   
    }
View Code

 

posted @ 2020-04-13 12:01  可乐_加冰  阅读(453)  评论(0编辑  收藏  举报