嗨,我有绑定问题。我将数据网格绑定到 sql 查询的结果,但我不知道如何绑定 selectedItem。我想要类似的东西SelectedItem.a
。我的问题是如何绑定到多个东西?
这是我如何绑定的代码,SelctedItem
但这不起作用。
XAML:
<DataGrid Name="grid" ItemsSource ="{Binding}" SelectedItem ="{Binding Path=SelectedItem Mode=TwoWay}" AutoGenerateColumns="False" VerticalAlignment="Top">
<DataGrid.Columns >
<DataGridTextColumn Header="A" Binding="{Binding Path=a, Mode=TwoWay}" Visibility="Hidden"/>
<DataGridTextColumn Header="B" Binding="{Binding Path=b, Mode=TwoWay}" />
<DataGridTextColumn Header="D" Binding="{Binding Path=c, Mode=TwoWay}" />
</DataGrid.Columns>
<DataGrid.ItemContainerStyle>
<Style TargetType="DataGridRow">
<EventSetter Event="MouseDoubleClick" Handler="selectRow"/>
</Style>
</DataGrid.ItemContainerStyle>
</DataGrid>
SelectedItem
定义:
searchPizzeria _selectedItem;
public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;
public foo SelectedItem {
get { return _selectedItem; }
set {
throw new Exception();//dont rise
this._selectedItem = value;
this.OnPropertyChanged("SelectedItem");
}
}
private void OnPropertyChanged(string propertyName){
var handler = this.PropertyChanged;
if (handler != null) {
handler(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName));
}
}
模型:
public class foo {
public string a { get; set; }
public string b { get; set; }
public string c { get; set; }
}
编辑。我不知道为什么它没有约束力。它必须是带有 SelectedItem 或 XAML 定义的东西。
编辑我如何在网格中设置数据
adapter = new Npgsql.NpgsqlDataAdapter(sql, conn);
adapter.SelectCommand.Parameters.AddWithValue("@foo", A_comboBox.Text);
dataTable = new DataTable();
adapter.Fill(dataTable);
grud.ItemsSource = dataTable.DefaultView;
编辑我的第一个解决方案,但我无法访问隐藏数据
DataRowView row = (DataRowView)grid.SelectedItems[0];
var x = row["Id_user"];