简体   繁体   中英

Move text box control at place of selected item inside a list

I want to move a textbox1 control inside listbox1 control at selected listbox's item's place at runtime. Lets say the selected place inside a list is 1. I am working in c# wpf application inside a grid.
Anybody knows ?

I think what you really want to do is have the TextBox.Text bound to the currently selected list item. Here is an example:

    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto" />
            <ColumnDefinition />
        </Grid.ColumnDefinitions>
        <TextBox x:Name="MyTextbox" Width="100" Margin="2" VerticalAlignment="Top"                 
                 Text="{Binding Path=SelectedItem.Content, ElementName=MyListBox}"/>
        <ListBox x:Name="MyListBox" Margin="2" Grid.Column="1">
            <ListBox.Items>
                <ListBoxItem>Item1</ListBoxItem>
                <ListBoxItem>Item2</ListBoxItem>
                <ListBoxItem>Item3</ListBoxItem>
            </ListBox.Items>
        </ListBox>
    </Grid>

This will do:

Private Sub MoveTextBox(TxtBox as TextBox, LBox as listbox, index as integer)
            lBox.items.remove(lbox.indexof(TxtBox));
        Dim l as ListBoxItem = lBox.items(index);
        l.content = TxtBox;
End Sub

Just write MoveTextBox(Which TextBox you want to move,listbox to which the txtbox belongs and to which you want to move, the index where you want the textbox to be moved(Remember index starts from 0) Sorry, I don't know how to get this to run with C#.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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