简体   繁体   中英

Accessing a nested XAML control within C#

I'm creating an application whereby there is a TextBox element within a DataTemplate, within a ListBox in WPF. I want to access the TextBox element by name in order to edit, or read the value directly from C# code, please help. There are multiple TextBoxes added within the WPF application, so how may I sort these out by index as well?

<ListBox x:Name="listBox" HorizontalAlignment="Left" Height="336" Margin="276,69,0,0" VerticalAlignment="Top" Width="242" SelectionChanged="listBox_SelectionChanged">
        Grid.IsSharedSizeScope="True"
        HorizontalContentAlignment="Stretch">
        <ListBox.ItemTemplate>
            <DataTemplate x:Name="D_Template">
                <Grid Margin="4" Width="222">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="Auto" SharedSizeGroup="Key" />
                        <ColumnDefinition Width="*" />
                    </Grid.ColumnDefinitions>
                    <TextBox x:Name ="TextValue" Grid.Column="1" Text="{Binding Value}" />
                </Grid>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>

As you can see, the ListBox has the variable name "listBox", the DataTemplate has been assigned to "D_Template" and the TextBox is assigned to the name "TextValue". Any help is appreciated, thank you for your time. :)

You should really bind the listbox itemssource to a collection of objects and read/change it instead of access directly to the textbox.But if for some reason you can't or don't want to do that, this code could help you. (Please note that I really discourage this solution)

    private void Button_Click(object sender, RoutedEventArgs e)
    {   //pretend you want to access the second item
        object myItem = myListbox.Items[1];
        ListBoxItem container = myListbox.ItemContainerGenerator.ContainerFromItem(myItem) as ListBoxItem;
        ContentPresenter contentPresenter = FindVisualChild<ContentPresenter>(container);
        DataTemplate myDataTemplate = contentPresenter.ContentTemplate;
        TextBox myTextbox = myDataTemplate.FindName("myTextbox", contentPresenter)as TextBox;
        if (myTextbox != null)
        {
            myTextbox.Text = "text changed!";
        }
    }

    public static IEnumerable<T> FindVisualChildren<T>(DependencyObject depObj) where T : DependencyObject
    {
        if (depObj != null)
        {
            for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
            {
                DependencyObject child = VisualTreeHelper.GetChild(depObj, i);
                if (child != null && child is T)
                {
                    yield return (T)child;
                }

                foreach (T childOfChild in FindVisualChildren<T>(child))
                {
                    yield return childOfChild;
                }
            }
        }
    }

    public static childItem FindVisualChild<childItem>(DependencyObject obj)
        where childItem : DependencyObject
    {
        foreach (childItem child in FindVisualChildren<childItem>(obj))
        {
            return child;
        }

        return null;
    }

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