加载的StackPanel未在in.cs页面中被触发

时间:2010-05-18 15:46:02

标签: silverlight silverlight-4.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>

在.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;
        }
    }

有没有更好的解决方案来实现这一目标?

2 个答案:

答案 0 :(得分:0)

如果没有看到完整的xaml和.cs文件,您确定xaml中指定的用户控件的x:Class与.cs文件中的类名相同吗?

答案 1 :(得分:0)

我使用转换器来做类似的事情。将图像的来源绑定到Marks对象,然后根据得分确定Source的来源。

希望有所帮助

Tim Heur article on Converters and DataBinding