2013-12-22 122 views
0

我要訪問內部列表視圖命名控制datatemplet我也跟着這樣:How to Access a Named Control Inside a XAML DataTemplate (using CSharp訪問控制DataTemplete

itemlistview.ItemsSource=new List<MyObject>();  
foreach (var item in itemListView.Items) 
       { 
        var _Container = itemListView.ItemContainerGenerator 
         .ContainerFromItem(item); 
        var _Children = AllChildren(_Container); 

        var _FirstName = _Children.OfType<StackPanel>() 
         .First(x => x.Name.Equals("subjectListItem")); 

        _FirstName.Visibility = 
         Visibility.Collapsed; 
       } 

var item in itemListView.Items問題它retruns我傳遞給不ListView控件的ItemsSource的datatemplete內CONTROLES的爲MyObject 。 那麼如何返回控件?

回答

0

ItemContainerGenerator.ContainerFromItem(object)應返回ListViewItem的實例,其中包含item的數據。

但是,這種做法阻礙了WPF/WinRT框架。 「正確」的方法是控制數據並讓用戶界面對其作出反應。

喜歡的東西:

// in code 
var list = new List<MyObject>(); 
foreach (var item in list) 
{ 
    item.IsSomethingVisible = false; 
} 
itemlistview.ItemsSource = list; 

<!--in XAML--> 
<StackPanel Visibility="{Binding IsSomethingVisible, 
        Converter={StaticResource BooleanToVisibilityConverter}}".../> 

<!--The converter is defined in resources of some higher level--> 
<BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter"/>