[英]Get content of textbox which is located in listView WPF
<ListView x:Name="listView2" HorizontalAlignment="Left" Height="215" Margin="348,10,0,0" VerticalAlignment="Top" Width="275">
<ListView.View>
<GridView>
<GridViewColumn Header="Id" DisplayMemberBinding="{Binding Id}"/>
<GridViewColumn Header="Artikelnr" DisplayMemberBinding="{Binding Artikelnr}"/>
<GridViewColumn Header="Bezeichnung" DisplayMemberBinding="{Binding Bezeichnung}"/>
<GridViewColumn Header="Menge">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBox x:Name="textBoxListView1" Width="80" Tag="{Binding Menge}"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
This is my ListView, which contains TextBoxes. 这是我的ListView,其中包含TextBoxes。 So there are as many textboxes as rows.
因此,文本框与行一样多。 My question is how can I get the content of these textboxes?
我的问题是如何获取这些文本框的内容?
I already tried this: 我已经尝试过了:
MyItem clMyItem = new MyItem();
clMyItem = (MyItem)listView2.Items.GetItemAt(zeile);
clArtikel.nId = Convert.ToInt32(clMyItem.Id);
clArtikel.cArtikelnr = clMyItem.Artikelnr;
clArtikel.cBezeichnung = clMyItem.Bezeichnung;
clArtikel.nMenge = clMyItem.Menge;
but for clMyItem.Menge
it only returns a null value, probably because this code only gets the content of the textbox at a status directly after the textbox was created. 但对于
clMyItem.Menge
它仅返回一个空值,这可能是因为此代码仅在创建文本框后立即将其内容获取为状态。
Try this: 尝试这个:
int index = listView2.SelectedIndex;
if (index != -1)
{
MyItem items = (MyItem)listView2.Items.GetItemAt(index);
if (items != null)
{
var textBoxContent = items.Menge;
}
}
The ItemsSource
of a ListView is an ObservableCollection
and I Binded here with Text
Property ListView的
ItemsSource
是一个ObservableCollection
,我在这里与Text
属性绑定
<DataTemplate>
<TextBox x:Name="textBox" Width="80" Text="{Binding Menge}"/>
/DataTemplate>
make sure you implemented INotifyPropertyChanged
in your MyItem
Class to make sure your Menge
property gets updated: 确保在
MyItem
类中实现了INotifyPropertyChanged
,以确保Menge
属性得到更新:
public class MyItem : INotifyPropertyChanged
{
private string _menge = "";
public String Menge
{
get { return _menge; }
set
{
_menge = value;
RaisePropertyChanged();
}
}
public string Artikelnr { get; set; } = "";
public String Bezeichnung { get; set; } = "";
public String Id { get; set; } = "";
#region Inotify
public event PropertyChangedEventHandler PropertyChanged;
public void RaisePropertyChanged([CallerMemberName]string property = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(property));
}
#endregion
}
Output: 输出:
You could also try somethin totally different. 您也可以尝试完全不同的东西。
Using the MVVM Pattern . 使用MVVM模式 。
Create a Model
first to hold all you data - lets call it "Bestellung": 首先创建一个
Model
来保存所有数据-称其为“ Bestellung”:
public class Bestellung
{
public int ID {get; set;}
public int ArtikelNummer {get; set;}
public string Bezeichnung {get; set;}
public int Menge {get; set;}
}
Create a ViewModel
like to bind your data to your view: 创建一个将数据绑定到视图的
ViewModel
:
public class ViewModel : INotifyPropertyChanged
{
public ObservableCollection<Bestellung> Bestellungen {get; set;} = new ObservableCollection<Bestellung>();
//Implement INotifyPropertyChanged here
}
What you need to do in your XAML is to set the ItemsSource
to ItemsSource="{Binding Bestellungen}"
and the DataContext
so the view knows on where to look for the Bindings
. 在XAML中,您需要将
ItemsSource
设置为ItemsSource="{Binding Bestellungen}"
和 DataContext
以便视图知道在哪里寻找Bindings
。
<ListView ItemsSouce="{Binding Bestellungen}">
<ListView.View>
<GridView>
<GridViewColumn Header="Id" DisplayMemberBinding="{Binding Id}"/>
<GridViewColumn Header="Artikelnr" DisplayMemberBinding="{Binding Artikelnr}"/>
<GridViewColumn Header="Bezeichnung" DisplayMemberBinding="{Binding Bezeichnung}"/>
<GridViewColumn Header="Menge">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBox Width="80" Text="{Binding Menge}"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
Here you bind your TextBox
containing "Menge" directrly to the property Menge
of each item in your ListView. 在这里,您将包含“ Menge”的
TextBox
绑定到ListView中每个项目的属性Menge
。
Also you could get rid of all those x:Name
s because you won't need most of them anymore. 另外,您可以摆脱所有这些
x:Name
因为您将不再需要它们中的大多数。
You could do this in YourWindow.xaml.cs
您可以在
YourWindow.xaml.cs
执行此YourWindow.xaml.cs
//Code-Behind of your Window
public class YourWindow : Window
{
public YourWindow()
{
InitializeComponent();
this.DataContext = new ViewModel();
}
}
The good thing about this is that you can separate your logic and your view. 这样做的好处是您可以将逻辑和视图分开。
That means : You won't access your textbox anymore - because you don't care if it's a TextBox
, TextBlock
or sth. 这意味着 :您将不再访问文本框-因为您不在乎它是
TextBox
, TextBlock
还是sth。 else. 其他。 Your logic does not need to know what's in your view.
您的逻辑不需要知道您的观点。
To access and change any property of one of your items you could just do this: 要访问和更改其中一项的任何属性,您可以执行以下操作:
//Add 3 pcs to the order
Bestellungen.First(bestellung => bestellung.Id == "010101").Menge += 3;
or 要么
//Delete an order
Bestellungen.Remove(Bestellungen[2]);
The ObservableCollection
observes your model and will notify the UI once something changed. ObservableCollection
观察您的模型,并在发生更改后将通知UI。 Your new value should be instantly visible. 您的新价值应该立即可见。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.