我在主視窗中有一個資料網格。當我單擊“添加”按鈕時,它會打開一個名為“AddDetails”的新視窗。它接受用戶輸入,如姓名、年齡、城市。我在 AddDetails.xaml.cs 中創建了一個“PersonalInfo”類來定義欄位。當我單擊“AddDetails”視窗中的“submit”按鈕時,我的資料網格應該使用用戶輸入值更新。我需要關于如何在不實作 MVVM 架構的情況下做到這一點的幫助。問題是當我點擊“提交”按鈕時,它每次都會打開一個新視窗并在網格中添加詳細資訊。此外,先前添加的輸入也不存在。我想要的是在主視窗中顯示所有用戶輸入。
主視窗.xaml:
<Window x:Class="WPF_Practice.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:WPF_Practice"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<DataGrid x:Name="DataGrid1" ColumnWidth="200" HorizontalAlignment="Left" Height="244" Margin="117, 65, 0,0" VerticalAlignment="Top" Width="522" IsReadOnly="True">
<DataGrid.Columns>
<DataGridTextColumn Header="Name" Binding="{Binding _name}" Width="130"/>
<DataGridTextColumn Header="Age" Binding="{Binding _name}" Width="130"/>
<DataGridTextColumn Header="City" Binding="{Binding _name}" Width="*"/>
</DataGrid.Columns>
</DataGrid>
<Button x:Name="ADD" Click="ADD_Click" Content="ADD" HorizontalAlignment="left" VerticalAlignment="Top" Margin="127,335,0,0" Width="74"/>
</Grid>
主視窗.xaml.cs:
public partial class MainWindow : Window
{
public MainWindow()
{
}
public MainWindow(PersonalInfo personal)
{
InitializeComponent();
DataGrid1.Items.Add(personal);
}
private void ADD_Click(object sender, RoutedEventArgs e)
{
AddDetails addDetails = new AddDetails();
addDetails.Show();
}
}
添加詳細資訊.xaml:
<Window x:Class="WPF_Practice.AddDetails"
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:WPF_Practice"
mc:Ignorable="d"
Title="AddDetails" Height="450" Width="800">
<Grid>
<Button Content="Submit" HorizontalAlignment="Left" Margin="276,248,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click"/>
<Label Content="Name" HorizontalAlignment="Left" Margin="216,101,0,0" VerticalAlignment="Top"/>
<Label Content="Age" HorizontalAlignment="Left" Margin="216,147,0,0" VerticalAlignment="Top"/>
<Label Content="City" HorizontalAlignment="Left" Margin="216,186,0,0" VerticalAlignment="Top"/>
<TextBox x:Name="Name" HorizontalAlignment="Left" Height="23" Margin="334,113,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/>
<TextBox x:Name="Age" HorizontalAlignment="Left" Height="23" Margin="334,159,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/>
<TextBox x:Name="City" HorizontalAlignment="Left" Height="23" Margin="334,198,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/>
</Grid>
添加Details.xaml.cs:
public partial class AddDetails : Window
{
PersonalInfo personal = new PersonalInfo();
public AddDetails()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
this.Hide();
personal.name = Name.Text;
personal.age = Convert.ToInt32(Age.Text);
personal.city = City.Text;
MainWindow mainWindow = new MainWindow(personal);
mainWindow.Show();
}
public class PersonalInfo
{
public string name { get; set; }
public int age { get; set; }
public string city { get; set; }
}
}
這是我對對話框的實作:(編輯)
private void ADD_Click(object sender, RoutedEventArgs e)
{
Dialogue dialogue = new Dialogue();
if(dialogue.ShowDialog() == true)
{
PersonalInfo._name = dialogue.Name.Text;
PersonalInfo._age= Convert.ToInt32(dialogue.Age.Text);
PersonalInfo._city= dialogue.CityText;
if(dialogue.Name.Text.Length == 0)
{
MessageBox.Show(“Please fill all the details”);
}
DataGrid1.Items.Add(personalInfo);
}
The message box is showing. But when it does,the dialogue window automatically gets closed. I don’t want the dialogue window to get closed, when the message box is showing. This way when I click “ok” on the message box, I should be able to get back to my dialogue window and edit the fields.
Thanks in advance.
uj5u.com熱心網友回復:
您當前在視窗之間導航的方式,預計在按下提交后會有一個新的 MainWindow 實體(這也意味著,您以前的資料將被丟棄):
每次創建新AddDetails視窗時,都會PersonalInfo為 AddDetails 實體分配一個全新的物件。
PersonalInfo personal = new PersonalInfo();
當顯示您的 AddDetails 頁面時,您每次都會創建一個新的 AddDetails 物件:
private void ADD_Click(object sender, RoutedEventArgs e)
{
AddDetails addDetails = new AddDetails();
addDetails.Show();
}
每當您從此 AddDetails 視窗回傳時,您都會創建一個全新的 MainWindow 以導航回:
private void Button_Click(object sender, RoutedEventArgs e)
{
this.Hide();
personal.name = Name.Text;
personal.age = Convert.ToInt32(Age.Text);
personal.city = City.Text;
MainWindow mainWindow = new MainWindow(personal); //<-- new MainWindow instance
mainWindow.Show();
}
有多種方法可以避免這種情況:
使用新的 Window 實體并在建構式或加載事件中填充它們的資料。如果您希望將資料存盤在 Window 實體之間,則必須以持久方式存盤它們。每次卸載 MainWindow 物件時,其中的所有資料都會隨之消失,包括對其他 Windows 的參考。例如,靜態欄位或直接存盤在您的 App 類中的任何資料是一個例外。
保持您的視窗實體本身以上述方式存盤。如果您有一個 MainWindow 型別的靜態屬性并在應用程式運行時開始時將當前 Window 分配給它,您可以簡單地重用它。
這兩種解決方案都沒有考慮到像 WPF 這樣的大型框架帶來的功能。我的建議是切換到以下任一解決方案:
1:使用導航視窗。如果您的 MainWindow 是一個 NavigationWindow 而不僅僅是一個 Window,則您可以非常輕松地在頁面之間導航。AddDetails 反過來不會是一個視窗,它會是一個頁面,例如:
`public partial class AddDetails : Page`
`<Page x:Class="WPF_Practice.AddDetails"...`
2:使用對話框而不是視窗來顯示您的 AddDetails。對話是一個非常有用的 WPF 功能,您可以在其中顯示任何控制元件,就像顯示警報一樣。它將在當前視窗的頂部顯示一個新視窗,必須在與初始視窗再次互動之前提交該視窗,例如:
private void ADD_Click(object sender, RoutedEventArgs e)
{
AddDetails addDetails = new AddDetails();
if (addDetails.ShowDialog() == true)
{
//do something after the user pressed "submit"
}
}
在這種情況下,您所要做的就是告訴您的 AddDetails 對話框,當它被視為完成時。例如:
private void Button_Click(object sender, RoutedEventArgs e)
{
DialogResult = true; //true means success, false means dialog was canceled
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/342854.html
