2010-09-01 121 views
0

取數據我有這樣從點擊的ListView

<UserControl x:Class="UISkripsi3.Sidebar" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    xmlns:UISkripsi3="clr-namespace:UISkripsi3" 
    xmlns:local="clr-namespace:UISkripsi3"> 
<UserControl.Template> 

     <ControlTemplate TargetType="{x:Type UISkripsi3:Sidebar}"> 
      <DataTemplate x:Key="StatusAndOther" DataType="{x:Type local:NewsFeedResources}"> 
        <Grid> 
         <Grid.RowDefinitions> 
          <RowDefinition/> 
          <RowDefinition/> 
          <RowDefinition/> 
         </Grid.RowDefinitions> 
         <Grid.ColumnDefinitions> 
          <ColumnDefinition/> 
          <ColumnDefinition/> 
         </Grid.ColumnDefinitions> 
         <StackPanel Orientation="Vertical"> 
          <StackPanel Orientation="Horizontal" VerticalAlignment="Top" Margin="10"> 
           <Image x:Name="ProfilePicture" Source="{Binding Path=ProfilePict}" Stretch="Fill" Width="80" Height="80" VerticalAlignment="Top"></Image> 
           <StackPanel Orientation="Vertical" Margin="5"> 
            <TextBlock Text="{Binding Path=UserName}" Style="{StaticResource UserNameStyle}" VerticalAlignment="Top"/> 
            <TextBlock Text="{Binding Path=Message}" Foreground="White" TextWrapping="Wrap" Width="400" VerticalAlignment="Top" HorizontalAlignment="Left"/>          
            <StackPanel Orientation="Horizontal"> 
             <Image x:Name="Icon" Source="{Binding Path=icon}" Width="10" Height="10"></Image> 
             <TextBlock Text=" via " Foreground="White"></TextBlock> 
             <TextBlock Text="{Binding Path=attribution}" Foreground="White"></TextBlock> 
             <StackPanel Orientation="Vertical"> 
             <Button x:Name="LikeButton" VerticalAlignment="Top" Content="like" IsEnabled="{Binding canLike}" ToolTip="Like" Click="LikeButtonClick"> 
              <Button.Style> 
              <Style BasedOn="{StaticResource LikeButtonStyle}" TargetType="Button"> 
               <Setter Property="Visibility" Value="Visible" /> 
               <Style.Triggers> 
               <DataTrigger Binding="{Binding hasLiked}" Value="True"> 
                <Setter Property="Visibility" Value="Collapsed" /> 
               </DataTrigger> 
               </Style.Triggers> 
              </Style> 
              </Button.Style> 
             </Button> 
             <Button x:Name="RemoveLikeButton" VerticalAlignment="Center" Content="remove like" IsEnabled="{Binding hasLiked}" ToolTip="Remove Like"> 
              <Button.Style> 
              <Style BasedOn="{StaticResource RemoveLikeButtonStyle}" TargetType="Button"> 
               <Setter Property="Visibility" Value="Visible" /> 
               <Style.Triggers> 
               <DataTrigger Binding="{Binding hasLiked}" Value="False"> 
                <Setter Property="Visibility" Value="Collapsed" /> 
               </DataTrigger> 
               </Style.Triggers> 
              </Style> 
              </Button.Style> 
             </Button> 
            </StackPanel> 

            </StackPanel> 
           </StackPanel>        
          </StackPanel> 
        </StackPanel> 
          <StackPanel Orientation="Vertical"> 
         <ListView Name="NewsFeedList" Width="700" Height="500" Margin="10" 
            ItemsSource="{Binding Source={x:Static local:Sidebar.newsFeedResourcesCoba2}}" 
            Background="Black" 
            ItemTemplateSelector="{StaticResource myDataTemplateSelector}" Focusable="False">        
         </ListView> 
        </StackPanel>     
        </Grid>     
       </DataTemplate> 
     </ControlTemplate> 

</UserControlTemplate> 

</UserControl> 

XAML文件。然後,我有這樣的

public class NewsFeedResources : UserControl 

{  

     public string UserName { get; set; } 
     public string Id { get; set; } 
     public string Message { get; set; } 
     public string IdPost { get; set; } 
     public byte[] ProfilePict { get; set; } 
     public string DestinationName { get; set; } 
     public string to { get; set; } 
     public byte[] UploadedPict { get; set; } 
     public byte[] Attachment { get; set; } 
     public string Description { get; set; } 
     public string type { get; set; } 
     public string nameWallPost { get; set; } 
     public string captionWallPost { get; set; } 
     public string link { get; set; } 
     public byte[] icon { get; set; } 
     public string attribution { get; set; } 
     public bool canLike { get; set; } 
     public bool hasLiked { get; set; } 

} 

類允許添加一些數據上面

public static ObservableCollection <NewsFeedResources> _newsfeedcoba2 = new ObservableCollection <NewsFeedResources>(); 

public static ObservableCollection <NewsFeedResources> newsFeedResourcesCoba2 
     { get { return _newsfeedcoba2; } } 

_newsfeedcoba2.Add(new NewsFeedResources 

{ 

UserName = "abc", Id = "123", Message = "Hello World", IdPost = "12345-12345", ProfilePict = data, DestinationName = "Boulevard", to = "John", UploadedPict = data2, Attachment = data2, Description = "Bored", type = tipeStatus, nameWallPost = nameWallPost, captionWallPost = caption, link = linknya, icon = data3, attribution = attributenya, canLike = blumLike, hasLiked = sdhLike 

} 
); 

代碼只是我的程序的一部分,它工作正常。我想問一下ListView(x:name =「NewsFeedList」)。我想,如果我點擊LikeButton,一個消息框出現並顯示IdPost。我不知道如何將xaml的值傳遞給xaml.cs.誰能幫我?

回答

0
private void LikeButtonClick(object sender, RoutedEventArgs e) { 
    Button btn = (Button)sender; 
    NewsFeedResources obj = btn.DataContext as NewsFeedResources; 
    if(null != obj){ 
     MessageBox.Show(obj.IdPost); 
    } 
} 
+0

非常感謝!它的工作..現在,我有一個新的問題。我編輯了我以前的問題。 – 2010-09-02 11:37:55

+0

@Wandy Wujayanto:人們回答後不要改變你的問題。如果這個答案有幫助,請對其進行解釋併爲您的新請求提出一個新問題。這是SO的工作原理。您可以點擊點數顯示下方的複選標記來接受答案(如果它對您有幫助)。點擊顯示編輯帖子時間的鏈接,即可恢復到問題的前一版本。 – HCL 2010-09-02 11:46:00

+0

@ HCL:感謝您的建議。我是新來的,非常感謝。我點擊了你所說的鏈接。 – 2010-09-02 11:58:36