8,735
社区成员
发帖
与我相关
我的任务
分享
<Grid>
<DataGrid x:Name="datagrid1" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Height="122" Width="163" SelectionUnit="Cell" SelectionMode="Single" AutoGenerateColumns="False" CellEditEnding="datagrid1_CellEditEnding">
<DataGrid.Columns>
<DataGridTextColumn Header="ID" Binding="{Binding ID}" IsReadOnly="True"/>
<DataGridTextColumn Header="某些值" Binding="{Binding MyValue}"/>
</DataGrid.Columns>
</DataGrid>
<TextBox x:Name="textbox1" HorizontalAlignment="Left" Height="23" Margin="10,170,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="120"/>
</Grid>
public TestWindow()
{
InitializeComponent();
Test();
}
private void Test()
{
MyTest t1 = new MyTest { ID = 1, MyValue = "qwer" };
MyTest t2 = new MyTest { ID = 2, MyValue = "asdf" };
MyTest t3 = new MyTest { ID = 3, MyValue = "zxcv" };
ObservableCollection<MyTest> source = new ObservableCollection<MyTest> { t1, t2, t3 };
this.datagrid1.ItemsSource = source;
}
private void datagrid1_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
{
TextBox tb = e.EditingElement as TextBox;
if (tb != null) {
MyTest temp = e.Row.Item as MyTest;
if (temp != null) {
this.textbox1.Text = tb.Text + temp.ID.ToString();
}
}
}
public class MyTest
{
public Int32 ID { get; set; }
public String MyValue { get; set; }
}
private void dataGrid1_SelectedCellsChanged(object sender, SelectedCellsChangedEventArgs e)
{
DataGrid dg = sender as DataGrid;
var cell = dg.CurrentCell;
DataRowView item = cell.Item as DataRowView;
if (item != null) {
this.textBox1.Text = item[cell.Column.DisplayIndex].ToString();
}
}
跟这里的要求差不多http://bbs.csdn.net/topics/390617296