2010-05-18 26 views
0
<sdk:DataGridTemplateColumn> 
       <sdk:DataGridTemplateColumn.CellTemplate> 
        <DataTemplate> 
         <StackPanel Loaded ="SPImage_Loaded" Orientation="Horizontal" Background="Transparent" > 
          <Button x:Name="myButton" 
         Click="Btn_Click"> 
          <Image x:Name="imgMarks" " Stretch="None"/> 
         </Button> 
         </StackPanel> 
        </DataTemplate> 
       </sdk:DataGridTemplateColumn.CellTemplate> 
      </sdk:DataGridTemplateColumn> 

頁我所定義的事件堆疊面板的StackPanel中的.cs不解僱in.cs

private void SPImage_Loaded(object sender, RoutedEventArgs e) 
    { 
     try 
     { 
      var TargetMarks = sender as StackPanel; 
      Image imgMarks= (Image)TargetScore.FindName("imgMarks"); 

      Marks obj = (Marks)TargetMarks.DataContext; 

// here marks oject would be containg the details 
// here if marks.score object value is 1 then bind the image 
//else 
// dnt bind the image . that is logic i am trying to do. 

      imgMarks.Source = new BitmapImage(new Uri("/Images/a1.png", UriKind.Relative)); 
     } 
     catch (Exception) 
     { 
      throw; 
     } 
    } 

有沒有更好的解決方案,以實現這一目標?

+0

您可以加入更多的XAML文件?沒有比DataTemplate更多的功能,我們可以做的不多。 – Stephan 2010-05-18 20:33:35

回答

0

沒有看到完整的xaml和.cs文件,您確定xaml中指定的用戶控件的x:Class與您的.cs文件中的類名相同嗎?