我正在尝试创建一个包含机场数据的主窗口,以及一个用于编辑数据的单独窗口。目前,我只能在同一窗口中绑定数据网格和文本框,但是如何在EditWindow中绑定它们?
AirportsWindow(主窗口):
public partial class AirportsWindow : Window
{
public AirportsWindow()
{
InitializeComponent();
using (SQLiteConnection dataConnection = new SQLiteConnection())
{
try
{
dataConnection.ConnectionString = "Data Source=database.db;Version=3;";
dataConnection.Open();
SQLiteCommand dataCommand = new SQLiteCommand();
dataCommand.Connection = dataConnection;
}
catch (Exception e)
{
Console.WriteLine("Грешка при повезивању с базом " + e.Message);
}
}
string database_connection = "Data Source=database.db;Version=3;";
string query = String.Format("SELECT * FROM airports WHERE active = '1'");
SQLiteConnection connection = new SQLiteConnection(database_connection);
connection.Open();
SQLiteCommand command = new SQLiteCommand(query, connection);
SQLiteDataAdapter dataAdapter = new SQLiteDataAdapter(command);
DataTable data = new DataTable();
dataAdapter.Fill(data);
dgAirports.DataContext = data;
}
这是EditWindow:
<Grid x:Name="FormGrid" DataContext="{Binding SelectedItem, ElementName=Dgrid}">
<Label x:Name="lblAirportId" Content="ИД" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="10,12,0,0"/>
<TextBox x:Name="txtAirportId" Text="{Binding ElementName=dgAirports, Path=SelectedValue.id}" HorizontalAlignment="Left" Height="23" TextWrapping="Wrap" VerticalAlignment="Top" Width="125" Margin="48,13,0,0"/>
<Label x:Name="lblAirportName" Content="Назив" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="0,38,0,0"/>
<TextBox x:Name="txtAirportName" Text="{Binding ElementName=dgAirports, Path=SelectedValue.airportName}" HorizontalAlignment="Left" Height="23" TextWrapping="Wrap" VerticalAlignment="Top" Width="125" Margin="48,41,0,0"/>
<Label x:Name="lblAirportCity" Content="Град" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="2,69,0,0"/>
<TextBox x:Name="txtAirportCity" Text="{Binding ElementName=dgAirports, Path=SelectedValue.airportCity}" HorizontalAlignment="Left" Height="23" TextWrapping="Wrap" VerticalAlignment="Top" Width="125" Margin="48,72,0,0"/>
<Button x:Name="btnEdit" Content="Додај" HorizontalAlignment="Left" Margin="10,100,0,0" VerticalAlignment="Top" Width="75" Click="btnEdit_Click"/>
<Button x:Name="btnCancel" Content="Одустани" HorizontalAlignment="Left" Margin="98,100,0,0" VerticalAlignment="Top" Width="75" Click="btnCancel_Click"/>
</Grid>
</Window>
这也是我在AirportsWindow(主窗口)中完成和绑定的方式
<Grid>
<DataGrid x:Name="dgAirports" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Height="100" Width="272" ItemsSource="{Binding}" AutoGenerateColumns="False" CanUserAddRows="false" VerticalScrollBarVisibility="Hidden">
<DataGrid.Columns>
<DataGridTextColumn Header="Шифра" Binding="{Binding Path = id}" Width="*"/>
<DataGridTextColumn Header="Назив" Binding="{Binding Path = airportName}" Width="*"/>
<DataGridTextColumn Header="Град" Binding="{Binding Path = airportCity}" Width="*"/>
</DataGrid.Columns>
</DataGrid>
<Label x:Name="lblAirportId" Content="ИД" HorizontalAlignment="Left" Margin="123,115,0,0" VerticalAlignment="Top"/>
<TextBox x:Name="txtAirportId" Text="{Binding ElementName=dgAirports, Path=SelectedValue.id}" HorizontalAlignment="Left" Height="23" Margin="157,115,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="125"/>
<Label x:Name="lblAirportName" Content="Назив" HorizontalAlignment="Left" Margin="109,142,0,0" VerticalAlignment="Top"/>
<TextBox x:Name="txtAirportName" Text="{Binding ElementName=dgAirports, Path=SelectedValue.airportName}" HorizontalAlignment="Left" Height="23" Margin="157,143,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="125"/>
<Label x:Name="lblAirportCity" Content="Град" HorizontalAlignment="Left" Margin="115,168,0,0" VerticalAlignment="Top"/>
<TextBox x:Name="txtAirportCity" Text="{Binding ElementName=dgAirports, Path=SelectedValue.airportCity}" HorizontalAlignment="Left" Height="23" Margin="157,171,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="125"/>
<Button x:Name="btnAdd" Content="Додај" HorizontalAlignment="Left" Margin="10,115,0,0" VerticalAlignment="Top" Width="75" Click="btnAdd_Click"/>
<Button x:Name="btnEdit" Content="Измени" HorizontalAlignment="Left" Margin="10,142,0,0" VerticalAlignment="Top" Width="75" Click="btnEdit_Click"/>
<Button x:Name="btnDelete" Content="Избриши" HorizontalAlignment="Left" Margin="10,170,0,0" VerticalAlignment="Top" Width="75" Click="btnDelete_Click"/>
</Grid>
</Window>
答案 0 :(得分:0)
使用在主窗口数据上下文中实现的ICommand或RelayCommand,并将所选项目设置为编辑窗口的数据上下文。 您可以通过编辑窗口构造函数将其作为参数传递。
当前实现中的问题是您正在从单独的xaml中按名称(Dgrid)访问元素。
我的建议是尝试使用MVVM模式实现您的应用程序。
答案 1 :(得分:0)
您应该使用MVVM模式,或者至少使用部分想法。
型号:
public class MyModel
{
public string MyProperties { get; set; }
}
MainWindow:
public partial class Window1 : Window
{
public MyModel Model { get; set; }
public Window1()
{
InitializeComponent();
// Only time creating an instance of MyModel
Model = new MyModel();
Model.MyProperties = string.Empty;
// Pass reference of MyModel to other Windows (or even better ViewModels)
Window2 wnd = new Window2(Model);
wnd.Show();
MessageBox.Show(Model.MyProperties);
// Output: "This is an example."
}
}
子窗口:
public partial class Window2 : Window
{
// Don't create another instance, just pass the existing instance reference
public MyModel Model { get; set; }
public Window2(MyModel model)
{
InitializeComponent();
Model = model;
// Modifying Properties from here will also update in Window1
Model.MyProperties = "This is an example.";
}
}