WPF在datagrid的另一个窗口中显示数据

时间:2017-09-08 22:32:42

标签: c# wpf desktop

我在mainwindow中有一个datagrid,datagrid中有一个详细信息按钮。我想在另一个窗口中单击详细信息按钮显示数据(行)。只有Textblocks。

enter image description here

网格XAML

    <DataGrid x:Name="dataGrid1" Grid.Column="1" Grid.Row="1" RowHeaderWidth="0" BorderBrush="{x:Null}" FontSize="14" FontFamily="Verdana" CanUserResizeRows="False" IsReadOnly="True" RowDetailsVisibilityMode="Visible" ClipboardCopyMode="None" AutoGenerateColumns="False" CanUserReorderColumns="False" CanUserSortColumns="False" AutomationProperties.IsColumnHeader="True" AutomationProperties.IsOffscreenBehavior="Onscreen" CellStyle="{DynamicResource DataGridCellStyle1}" CanUserResizeColumns="False" GridLinesVisibility="Horizontal" HorizontalGridLinesBrush="#FF0194CA" Foreground="Black" HorizontalScrollBarVisibility="Disabled" Background="{x:Null}" UseLayoutRounding="False" VerticalScrollBarVisibility="Visible" SelectionMode="Single">
        <DataGrid.Columns>
            <DataGridTemplateColumn Header="Başlık" Width="500" CanUserResize="False">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding Path=baslik}"  FontSize="13" TextWrapping="Wrap" Padding="10" Background="{x:Null}"/>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
            <DataGridTemplateColumn Header="Büyüklük" Width="100" CanUserResize="False">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding Path=metrekare}" FontSize="13" TextWrapping="Wrap" Padding="10"/>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
            <DataGridTemplateColumn Header="Konum" Width="196" CanUserResize="False">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding Path=mevkii}" FontSize="13" TextWrapping="Wrap" Padding="10"/>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
            <DataGridTemplateColumn Header="Detay" CanUserResize="False" Width="Auto">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <Button Content="Ilan Detayları" Click="DetayButton_Click" FontSize="13" FontFamily="Verdana" Padding="10" Margin="0,10,26,10" />
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
        </DataGrid.Columns>
    </DataGrid>

MainWindow Datagrid进程

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        SQLiteConnection yeni = new SQLiteConnection(baglanti);
        yeni.Open();

        string sorgu = "Select * From Gayrimenkul";

        // SQLiteCommand komutislet = new SQLiteCommand(sorgu,baglanti);
        DataSet dataSet = new DataSet();
        SQLiteDataAdapter dataAdapter = new SQLiteDataAdapter(sorgu, baglanti);
        dataAdapter.Fill(dataSet);

        dataGrid1.ItemsSource = dataSet.Tables[0].DefaultView;
        yeni.Close();

    }

2 个答案:

答案 0 :(得分:0)

您可以使用object sender确定您点击的按钮(以及您尝试接收的对象的ID),然后将查询参数化为SELECT * FROM YourTableHere where ID = @someattribute

如果您发布更多信息,我可以帮助您。

答案 1 :(得分:0)

我找到了解决方案,我正在为朋友分享; 这是详细信息窗口。

b = []
for i in range(len(a)):
  if type(a[i]) is list:
    for j in range(len(a[i])):
      b.append(a[:i] + [a[i][j]] + a[i+1:])

这是MainWindow Click Button;

    public partial class DetayWindow : Window
{
    public DetayWindow()
    {
        InitializeComponent();
    }

    public string baslik { get; set; }
    public string yazi { get; set; }
    public string tarih { get; set; }
    public string mevkii { get; set; }
    public string metrekare { get; set; }
    public string tur { get; set; }
    public string id { get; set; }

    private void Window_Loaded_1(object sender, RoutedEventArgs e)
    {
        //textbox1.Text = sifre;
        //text1.Text = kullaniciAdi;

        idtext.Text = id;
        turtext.Text = tur;
        metrekaretext.Text = metrekare;
        konumtext.Text = mevkii;
        tarihtext.Text = tarih;
        yazitext.Text = yazi;
        basliktext.Text = baslik;

    }
}