开发者

StackPanel loaded is not getting fired in.cs page

开发者 https://www.devze.com 2022-12-31 10:14 出处:网络
<sdk:DataGridTemplateColumn> <sdk:DataGridTemplateColumn.CellTemplate> <DataTemplate>
 <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>

in .cs i have defined the event for stack panel

 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", Uri开发者_如何学编程Kind.Relative));
        }
        catch (Exception)
        {
            throw;
        }
    }

Is there any better solution to achieve this?


Without seeing the full xaml and .cs file, are you certain the x:Class for the user control specified in the xaml is the same as the class name in your .cs file?


I have used a Converter for similar things to this. Bind the Source of your image to your Marks object then decide what the Source is based on the score.

Hope it helps

Tim Heur article on Converters and DataBinding

0

精彩评论

暂无评论...
验证码 换一张
取 消