的標記值,充分利用這個<code>XAML</code>基於控制
<ListView>
<ListView.View>
<GridView>
<GridViewColumn>
<GridViewColumn.CellTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal"
VerticalAlignment="Center"
HorizontalAlignment="Center">
<TextBlock Text="{Binding ProductDescription}"/>
</StackPanel>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn>
<GridViewColumn.CellTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal"
VerticalAlignment="Center"
HorizontalAlignment="Center">
<TextBox x:Name"txtExchange"
Tag="{Binding ProductBarcode}"/>
<Button Content="Add"
Tag="{Binding ProductBarcode}"
Click="SelectExchangeProduct" />
</StackPanel>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
,因爲插入都會有相同的TextBox
所有行,使用x:Name
來獲取值是不可能的。
我添加了對Tag
屬性的綁定,以便我可以區分所有TextBox
。問題是我如何才能找到具體的TextBox
?我嘗試使用FindName()
,但我不知道如何獲得具有特定Tag
值的TextBox
。
我也試過這樣:
var txtExchangeQuantity = someParentControl.Children.OfType<TextBox>().Where(x => x.Tag.ToString() == barcode).FirstOrDefault();
也沒有工作。我看到了Where
部分的潛力,但不知道如何實現它。
這裏是Click
事件:
private void SelectExchangeProduct(object sender, RoutedEventArgs e)
{
Button btn = sender as Button;
string barcode = btn.Tag.ToString();
var txtExchangeQuantity = grdExchange.Children.OfType<TextBox>().Where(x => x.Tag.ToString() == barcode).FirstOrDefault();
}
這裏是我結合對象(使用ObservableCollection
):
class TransactionList{
private string _productBarcode;
public string ProductBarcode
{
get { return _productBarcode; }
set { _productBarcode = value; }
}
private string _productDescription;
public string ProductDescription
{
get { return _productDescription; }
set { _productDescription = value; }
}
}
你不知道。這是WPF,而不是WinForms。使用[命令](https://docs.microsoft.com/en-us/dotnet/framework/wpf/advanced/commanding-overview)而不是點擊處理程序。您可以將文本作爲參數傳遞。 – nvoigt
我想'grdExchange.Children'只有堆疊面板。你的情況很好。 – rmbq
grdExchange是一個擁有這個ListView和其他子元素的Grid。我省略了,因爲我不認爲這是必要的。它只是具有'.Children'的最近控件。 – Swellar