开发者

Why can't I access AutoCompleteBox in any function other than Main Window in WPF?

I am trying to do a

codebox.ItemsSource = codesList;
codebox.PopulateComplete();

from the Populating event I have created and I get the error

"The name 'codebox' does not exist in the current context'

开发者_如何学C

This was working when I just populated it from the MainWindow. Anyone know what I am missing?

Thanks!


This looks like it's because codebox is not visible from the scope of where your Populating event handler is declared. When the Populating event handler is within MainWindow, the codebox control is "visible" to that code. See here for more info.

Where is your Populating event handler declared?

Also, note that the sender parameter in the Populating event should be a reference to codebox. You could simply cast this to an AutoCompleteBox, and it should work fine, e.g.:

private void Codebox_Populating(object sender, PopulatingEventArgs e)
{ 
    AutoCompleteBox _codebox = sender as AutoCompleteBox;

    // Use _codebox here instead of codebox
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜