2015-07-12 146 views
-1

我正在使用Telerik,HubSection就像使用WP8的Pivot一樣。如何使用DataTemplate訪問列表框中的特定項目

<HubSection x:Uid="Section4Header" Header="All note" > 
    <DataTemplate> 
     <ListBox x:Name="listBoxobj" Background="Transparent" Margin="6" Height="auto" BorderThickness="2" MaxHeight="580" Grid.Row="1" SelectionChanged="listBoxobj_SelectionChanged"> 
      <ListBox.ItemTemplate> 
       <DataTemplate> 
        <Grid Width="350" > 
         <Border Margin="5" BorderBrush="White" BorderThickness="1"> 
          <Grid Holding="Grid_Holding" VerticalAlignment="Stretch"> 
           <Grid.RowDefinitions> 
            <RowDefinition Height="Auto"/> 
            <RowDefinition Height="Auto"/> 
           </Grid.RowDefinitions> 

           <FlyoutBase.AttachedFlyout> 
            <MenuFlyout> 
             <MenuFlyoutItem x:Name="EditButton" 
              Text="Export To PDF" 
              Click="EditButton_Click" 
             /> 
             <MenuFlyoutItem x:Name="EditButton1" 
              Text="Export To PDF syncfu" 
              Click="EditButton1_Click" 
             /> 
            </MenuFlyout> 
           </FlyoutBase.AttachedFlyout> 
           <TextBlock Margin="5,0,0,0" Grid.Row="0" x:Name="NameTxt" TextWrapping="Wrap" Text="{Binding Name}" FontSize="28" Foreground="White"/> 
           <TextBlock HorizontalAlignment="Right" Margin="0,0,35,0" Grid.Row="3" x:Name="CreateddateTxt" Foreground="White" FontSize="18" TextWrapping="Wrap" Text="{Binding CreationDate}" /> 
          </Grid> 
         </Border> 
        </Grid> 
       </DataTemplate> 
      </ListBox.ItemTemplate> 
     </ListBox> 
    </DataTemplate> 
</HubSection> 

C#

private void ReadWritePadFileContentList_Loaded(object sender, RoutedEventArgs e) 
{ 
    ReadAllWritePadFileContent dbnote = new ReadAllWritePadFileContent(); 
    DB_ContactList = dbnote.GetAllContacts();//Get all DB contacts 
    if (DB_ContactList.Count > 0) 
     deleteAppBarButton.IsEnabled = true; 
    else 
     deleteAppBarButton.IsEnabled = false; 

    listBoxobj.ItemsSource = DB_ContactList.OrderByDescending(i => i.Id).ToList();//Binding DB data to LISTBOX and Latest contact ID can Display first. 

} 

listBoxobj是列表框 問題的名字就是那麼,如何可以存取權限的列表框?

「listBoxobj」這個名字不會在目前情況下

回答

1

正如邁克說,你不會成爲能夠直接從輪轂的代碼中訪問列表框中,作爲它在一個DataTemplate中。

但是,你可以創建一個UserControl例如MyUserControl1。 將您的ListBox放入MyUserControl1.xaml文件中,並將其相關的c#代碼放在MyUserControl1.cs文件中。 然後,將用戶控件添加到HubSection中。

<HubSection x:Uid="Section4Header" Header="All note" > 
    <DataTemplate> 
     <local:MyUserControl1 x:Name="ListControl"/> 
    </DataTemplate> 
</HubSection> 

編輯:把你的列表框代碼<Grid>你的用戶控件代碼中:

<ListBox x:Name="listBoxobj" Background="Transparent" Margin="6" Height="auto" BorderThickness="2" MaxHeight="580" Grid.Row="1" SelectionChanged="listBoxobj_SelectionChanged"> 
     <ListBox.ItemTemplate> 
      <DataTemplate> 
       <Grid Width="350" > 
        <Border Margin="5" BorderBrush="White" BorderThickness="1"> 
         <Grid Holding="Grid_Holding" VerticalAlignment="Stretch"> 
          <Grid.RowDefinitions> 
           <RowDefinition Height="Auto"/> 
           <RowDefinition Height="Auto"/> 
          </Grid.RowDefinitions> 

          <FlyoutBase.AttachedFlyout> 
           <MenuFlyout> 
            <MenuFlyoutItem x:Name="EditButton" 
             Text="Export To PDF" 
             Click="EditButton_Click" 
            /> 
            <MenuFlyoutItem x:Name="EditButton1" 
             Text="Export To PDF syncfu" 
             Click="EditButton1_Click" 
            /> 
           </MenuFlyout> 
          </FlyoutBase.AttachedFlyout> 
          <TextBlock Margin="5,0,0,0" Grid.Row="0" x:Name="NameTxt" TextWrapping="Wrap" Text="{Binding Name}" FontSize="28" Foreground="White"/> 
          <TextBlock HorizontalAlignment="Right" Margin="0,0,35,0" Grid.Row="3" x:Name="CreateddateTxt" Foreground="White" FontSize="18" TextWrapping="Wrap" Text="{Binding CreationDate}" /> 
         </Grid> 
        </Border> 
       </Grid> 
      </DataTemplate> 
     </ListBox.ItemTemplate> 
    </ListBox> 
+0

你可以把一個例子 – Amin

+0

它已經在那裏!閱讀後部分**然而**。現在我添加了完整的代碼。 要創建UserControl,請在解決方案資源管理器中右鍵單擊您的項目,選擇Add,選擇UserControl。 –

+0

thnx,它的工作原理:D – Amin

相關問題