簡體   English   中英

如何將文本框中的字符串添加到另一個 window 中的列表視圖

[英]How to add a string from a textbox to a listview in another window

我正在嘗試在主窗口中創建一個列表視圖,如果用戶想要添加一個項目,他首先需要打開一個菜單 window。 從那個 window 開始,他去了一個帶有文本框的 window。 單擊按鈕后,應將輸入到文本框中的字符串添加到列表視圖中。 我遇到的問題是我無法將字符串發送到主窗口,因為文本框 window 中的方法需要是非靜態的才能讀取文本框的內容。 因此,我無法將文本框的字符串從主窗口添加到 ListView。

我已經了解了如何將字符串發送到主窗口而不將其設為 static。 我努力了:

MainWindow newWnd = new MainWindow();
newWnd.Show();

這確實使文本框中的文本出現在 ListView 中,但是在將另一個項目添加到 ListView 時,第一個項目會消失。 我可以創建一種將以前的項目添加到新的 window 的方法,但這只會在一定程度上起作用。 我不想每次用戶將項目添加到 ListView 時都需要打開一個新的 window。 我想從原始主窗口向 ListView 添加一個項目。

我的代碼:

主窗口:XAML:

<ListView Name="ingredientsList"/>

<Button Content="Add +" Click="Button_Click"/>

。CS:

public void Button_Click(object sender, RoutedEventArgs e)
        {
            //Opens the menu window
            Menu popup = new Menu();
            popup.ShowDialog();
        }

        public void AddIngredient(string e)
        {
            ingredientsList.Items.Add(e);
        }

菜單 window 打開文本框 window(無需代碼)

文本框 window: XAML:

<TextBox x:Name="Ingredient"/>
<Button Content="Add" Click="Add_Click"/>
<Button Content="Cancel" Height="25" Width="70" Margin="125,150,0,0" Click="Cancel_Click" IsCancel="True"/>

。CS:

 public void Add_Click()
        {

            Window.AddIngredient(Ingredient.Text);

        }

        private void Cancel_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

您應該使用數據綁定將TextBox輸入存儲在對話框的屬性中,例如Ingredient 單擊“添加”按鈕時,將此項目添加到對話框的公共集合中,例如Menu.NewIngredients並清除TextBox
關閉對話框后,您可以使用數據綁定將Menu.NewIngredients的新項添加到ListView

*菜單.xaml.cs**

partial class Menu : Window
{
  public static readonly DependencyProperty IngredientProperty = DependencyProperty.Register(
    "Ingredient",
    typeof(string),
    typeof(MainWindow),
    new PropertyMetadata("Boom"));

  public string Ingredient
  {
    get => (string)GetValue(MainWindow.IngredientProperty);
    set => SetValue(MainWindow.IngredientProperty, value);
  }

  public List<string> NewIngredients { get; set; }

  public Menu()
  {
    this.NewIngredients = new List<string>();
  }

  public void Add_Click(object sender, RoutedEventArgs e)
  {
    this.NewIngredients.Add(this.Ingredient);
    this.Ingredient = string.Empty;
  }

  private void Cancel_Click(object sender, RoutedEventArgs e)
  {
    this.Close();
  }
}

菜單.xaml

<Window x:Name="Window">
  <StackPanel>
    <TextBox Text="{Binding ElementName=Window, Path=Ingredient}" />
    <Button Content="Add" Click="Add_Click"/>
    <Button Content="Cancel" Click="Cancel_Click" IsCancel="True"/>
  </StackPanel>
</Window>

主窗口.xaml.cs

partial class MainWindow : Window
{
  public static readonly DependencyProperty IngredientsProperty = DependencyProperty.Register(
    "Ingredients",
    typeof(ObservableCollection<string>),
    typeof(MainWindow),
    new PropertyMetadata(default(ObservableCollection<string>)));

  public ObservableCollection<string> Ingredients
  {
    get => (ObservableCollection<string>) GetValue(MainWindow.ResultsProperty);
    set => SetValue(MainWindow.ResultsProperty, value);
  }

  public MainWindow()
  {
    this.Ingredients = new ObservableCollection<string>();
  }

  public void Button_Click(object sender, RoutedEventArgs e)
  {
    // Opens the menu window
    Menu popup = new Menu();
    popup.ShowDialog();

    popup.NewIngredients.Foreach(this.Ingredients.Add);
  }
}

主窗口.xaml

<Window x:Name="Window">
  <StackPanel>
    <ListView ItemsSource="{Binding ElementName=Window, Path=Ingredients}" />
    <Button Content="Add +" Click="Button_Click"/>
  </StackPanel>
</Window>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM