2017-07-19 69 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