WPF Combox.FindName() doesn't work
I've got two comboboxes. The items of the first one i'm filling in Xaml with:
<ComboBox Name="ddl_pageType" Width="200" BorderThickness="5">
开发者_开发问答 <ComboBoxItem Name="Website" Content="Webseite"/>
<ComboBoxItem Name="CNIProg" Content="Seite"/>
</ComboBox>
and the function ddl_pageType.FindName("Website"); works.
The second combobox i'm filling with:
ddl_cniProg.SetBinding(TextBlock.TextProperty, new Binding());
ddl_cniProg.ItemsSource = progList;
where proglist is List. Here the function ddl_cniProg.FindName(string) doesn't work.
What do i have to do to get an item from dd_cniprog?
FrameworkElement.FindName
searches for child elements via the Name
attribute. (http://msdn.microsoft.com/en-us/library/system.windows.frameworkelement.findname.aspx) Unless the ComboBoxItems you are generating from the databound list have the Name attribute set (which it doesn't look like from the small code snippet) then the function won't find them.
To find the element you're looking for using FindName
, you'll need to set the Name
attribute for each item, either via databinding or programmatically.
Since you don't specify any name for the items in the databound ComboBox
, you can't use FindName
...
If you want to retrieve the ComboBoxItem
for a specific data item, you can use the ItemContainerGenerator
:
ComboBoxItem comboItem = ddl_cniProg.ItemContainerGenerator.ContainerFromItem(item) as ComboBoxItem;
精彩评论