[英]Obtain the value from textbox inside of a DataTemplate (WPF)
我剛剛為ListBox創建了一個數據模板,如下所示:
<ListBox Height="150" MinHeight="100" HorizontalAlignment="Left" Name="myListBox"
VerticalAlignment="Top" Width="290"
ItemsSource="{Binding}" SelectionMode="Multiple" Margin="0,18,0,0">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical" >
<StackPanel Orientation="Horizontal">
<CheckBox Name="cbLista" Width="100" Content="{Binding Path=Nom_estudio}" IsChecked="{Binding IsChecked, Mode=TwoWay}"
Checked="cbLista_Checked" />
<TextBox Name="txbCantidad" Width="100" Margin="0,0,0,5" TextChanged="txbCantidad_TextChanged" />
</StackPanel>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
現在,我可以在復選框中選擇所有對象,但是如何獲取與每個復選框相關聯的texbox的text屬性呢?
將TextBox的“ Text”屬性綁定到數據對象上的某個屬性(例如MyTextProperty
)。 然后,當您獲得“ SelectedItems”列表時,您只需訪問此屬性。
即:
<TextBox Text="{Binding MyTextProperty}" ... />
在具有Nom_estudio
和IsChecked
屬性的類中的更多屬性上創建。 然后將該屬性綁定到TextBox.Text
屬性
<StackPanel Orientation="Horizontal">
<CheckBox Name="cbLista" Width="100" Content="{Binding Path=Nom_estudio}" IsChecked="{Binding IsChecked, Mode=TwoWay}"
Checked="cbLista_Checked" />
<TextBox Name="txbCantidad" Text="{Binding MYTEXTPROPERTY}" Width="100" Margin="0,0,0,5" TextChanged="txbCantidad_TextChanged" />
</StackPanel>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.