简体   繁体   中英

WPF cannot access nested control

Wondering if there's any trick to accessing a nested control in the code-behind ?

given some XAML along the lines of

<UserControl>    
    <textbox />
    <DataGrid Name="MyGrid">
        <Columns>
            <Column field=ABC>
                <EditType>
                    <ComboBox Name="myCombo1"/>

I can access this.MyGrid but cannot access this.myCombo1 !!

Everytime that I have run into this I have be able to resolve it by using x:Name instead of Name . This may not be the issue in your case but give it a try.

In simple cases you can access elements using their name, but looks like combobox is datatemplate part, you can work with visual tree at runtime using VisualTreeHelper , but if you use mvvm & code right usually you don't need to access elements directly. Can you provide more information on general problem or test project to reproduce issue?

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