WPF 클라이언트 응용 프로그램을 프로그래밍하고 있습니다. 나머지 API가 있는데 WPF 클라이언트가이 API와 통신하고 있습니다. 나는 wpf에서 콤보 박스를 만들었고 데이터 바인딩은 괜찮습니다.일반 목록 wpf C#에서 사용자 ID가있는 사용자 이름을 얻으려면 어떻게해야합니까?
XAML 마크 업 :
<ComboBox Name="usernameBox" HorizontalAlignment="Left"
Margin="127,33,0,0" VerticalAlignment="Top" Width="152" Height="24"
ItemsSource="{Binding UserList}" DisplayMemberPath="Username"/>
것은이 잘 작동된다. 하지만 하나의 일반적인 목록 Userlist
을 가지고 있는데, 나는 이것을 콤보 박스에 바인딩하고있다. 사용자 이름에 콤보 박스가 필요하다. 나는 userid로 변수를 얻을 수있다.
모델 \ 사용자 클래스 :
namespace Desktop.Model
{
public class Users
{
public int UserId { get; set; }
public string Username { get; set; }
public string Password { get; set; }
public string Fullname { get; set; }
public int Authority { get; set; }
}
}
뷰 모델/UserViewModel 클래스 :
using GalaSoft.MvvmLight;
using Desktop.Model;
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Net.Http.Formatting;
using Newtonsoft.Json;
namespace Desktop.ViewModel
{
public class UserViewModel : ViewModelBase
{
public List<Users> UserList { get; set; }
public UserViewModel()
{
UserList = new List<Users>();
string url = "http://localhost:1234/api/users";
var json = new WebClient().DownloadString(url);
UserList = JsonConvert.DeserializeObject<List<Users>>(json);
}
}
}
이 오기 API/사용자 JSON :
내 나쁜 영어[
{
"UserID": 1,
"Username": "admin",
"Password": "password",
"Fullname": "teszt",
"Authority": 1
}
]
죄송합니다. 제발 도와주세요! 고맙습니다.
SelectedItem 바인딩을 사용하고 선택한 항목에 전체 사용자 개체가 있습니다. 이거하고 싶니? – Mohit