简体   繁体   English

从DataTemplate(WPF)内的文本框中获取值

[英]Obtain the value from textbox inside of a DataTemplate (WPF)

I´ve just created a datatemplate for a ListBox like that: 我刚刚为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>

And now I can get every object selected in the checkbox, but how can I obtain the text property for the texbox asociated for every checkbox ? 现在,我可以在复选框中选择所有对象,但是如何获取与每个复选框相关联的texbox的text属性呢?

Bind the "Text" property of the TextBox to some property (say MyTextProperty ) on your data object. 将TextBox的“ Text”属性绑定到数据对象上的某个属性(例如MyTextProperty )。 Then when you get the "SelectedItems" list, you just access this property. 然后,当您获得“ SelectedItems”列表时,您只需访问此属性。

ie: 即:

<TextBox Text="{Binding MyTextProperty}" ... />

Create on more property in your class which has Nom_estudio and IsChecked properties. 在具有Nom_estudioIsChecked属性的类中的更多属性上创建。 Then bind that property to TextBox.Text property 然后将该属性绑定到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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM