2017-07-19 72 views
-1

我試圖讓一個登錄形式,但我無法弄清楚如何從組合框我這樣做,用戶名:登錄表單組合框實體框架錯誤

XAML:

<StackPanel> 
    <Grid Height="274"> 
     <Label Content="LOGGARSI PER ENTRARE" HorizontalContentAlignment="Center" FontWeight="Bold" FontSize="24" FontFamily="Sitka Heading" Margin="0,0,0,230" /> 
     <Label Content="Username" HorizontalAlignment="Left" Margin="68,64,0,0" VerticalAlignment="Top" Style="{StaticResource LabelStyle}"/> 
     <ComboBox x:Name="ComboUsers" ItemsSource="{Binding}" DisplayMemberPath="userName" SelectedValuePath="userName" SelectedItem="{Binding Path=userName}" HorizontalAlignment="Left" Margin="209,72,0,0" VerticalAlignment="Top" Width="120"/> 
     <Label Content="Password" HorizontalAlignment="Left" Margin="68,114,0,0" VerticalAlignment="Top" Style="{StaticResource ResourceKey=LabelStyle}"/> 
     <PasswordBox x:Name="passBox" HorizontalAlignment="Left" Margin="209,114,0,0" VerticalAlignment="Top" Width="120" Height="30"/> 
     <Button x:Name="BtnLogIn" Content="Entra" HorizontalAlignment="Left" Margin="158,204,0,0" VerticalAlignment="Top" Width="76" Click="BtnLogIn_Click"/> 
    </Grid> 
</StackPanel> 

這爲代碼在:

NioRepairContext ctx; 
public MainWindow() 
{ 
    InitializeComponent(); 
    FillComboUsers(); 
} 

public List<User> user { get; set; } 
private void FillComboUsers() 
{ 
    ctx = new NioRepairContext(); 
    var item = ctx.Users.ToList(); 
    user = item; 
    DataContext = user; 
} 

private void BtnLogIn_Click(object sender, RoutedEventArgs e) 
{ 
    var user = ctx.Users.Where(i => i.userName == ComboUsers.SelectedItem.ToString()).FirstOrDefault(); 
    if (user == null) 
    { 
     MessageBox.Show("Login Fallito, Credenziali inesatte"); 
    } 
    if (user.userName == ComboUsers.SelectedItem.ToString() && user.password == passBox.Password.ToString()) 
    { 
     MessageBox.Show("Benvenuto " + user.userName+ ", Login Corretto"); 
    } 
    else 
    { 
     MessageBox.Show("Login Fallito, Credenziali inesatte"); 
    } 
} 

如果我砍死的組合框結合或其他

回答

1

辛格我無法理解è您已綁定的ItemsSource屬性爲您List<User>,你可以使用SelectedItem屬性來獲取選定的用戶:

var selectedUser = ComboUsers.SelectedItem as User; 
string name = selectedUser.userName; 

您migth以及從XAML中刪除SelectedItemSelectedValuePath屬性,因爲你沒有任何源屬性綁定到:

<ComboBox x:Name="ComboUsers" ItemsSource="{Binding}" DisplayMemberPath="userName" HorizontalAlignment="Left" Margin="209,72,0,0" VerticalAlignment="Top" Width="120"/> 
+0

坦克的回答幫我解決 – Nio74

+0

請記住接受的答案,那麼:https://meta.stackexchange.com/questions/23138/how-to-accept-the -answer-上堆棧上溢 – mm8