如何将ID绑定到datagrid.selecteditem

时间:2017-05-23 20:48:21

标签: c# wpf datagrid

如何绑定这两个属性?我尝试了SelectedValue,我得到了

  

System.NullReferenceException

我尝试用SelectedIndex添加它,但它没有绑定正确的。

我尝试了SelectedItem,但我得到了

  

System.InvalidCastException

还尝试使用xaml中的绑定:SelectedValuePath="ClientId"

我的问题我该怎么用来绑定这些属性?

Reservation rs = new Reservation();
{
      rs.ClientId = (int)ClientDataGrid.SelectedValue;
      rs.RoomId = (int)AvailableRoomDataGrid.SelectedValue;
}

in class Reservation :

public int? ClientId { get; set; }
[ForeignKey("ClientId")]
public Client Client { get; set; }

1 个答案:

答案 0 :(得分:0)

这是一个快速而肮脏的样本。 在加载时,我初始化网格并向其添加几个记录。然后,当单击按钮时,选定的FName将填充web.FindElement(By.XPath(DS_login.lBtn)).Click();` <- works if (Class_doExist("visual-label-main") // it is there and tries to click { FindElement(By.ClassName("visual-label-main")).Click(); //<- this click doesnt work } 变量。

我猜这个(为你的课程量身定做)会照顾你在SelectedItem上的无效演员表。 TheName

MainWindow.xaml

((FamilyMember)dg_MainGrid.SelectedItem).FName

MainWindow.xaml.cs

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApplication1"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <DataGrid x:Name="dg_MainGrid" HorizontalAlignment="Left" Margin="249,114,0,0" VerticalAlignment="Top" RenderTransformOrigin="-14.748,-2.867"/>
        <Button x:Name="button" Content="Button" HorizontalAlignment="Left" Margin="238,291,0,0" VerticalAlignment="Top" Width="75" Click="button_Click"/>
    </Grid>
</Window>