locked
Bug in dataform with images or my error? RRS feed

  • Question

  • Hi,

    I have a data form with an image and an upload button where the user can load an image on the server.

    My xaml:

                       <sdk:DataGrid AutoGenerateColumns="False"
                          Height="200" ItemsSource="{Binding ElementName=myDataSource, Path=Data}"
                          Name="dataGrid" RowDetailsVisibilityMode="VisibleWhenSelected" IsReadOnly="True" >
                            <sdk:DataGrid.Columns>
                               <sdk:DataGridTextColumn x:Name="FileName1Column" Binding="{Binding Path=FileName1}" Header="File Name 1" Width="SizeToHeader" />
                             </sdk:DataGrid.Columns>
                        </sdk:DataGrid>
                       <toolkit:DataForm Name="dataForm1"
                                       AutoGenerateFields="False"
                                       ItemsSource="{Binding Data, ElementName=myDataSource}"
                                       Margin="0,12,0,0" AutoEdit="False" AutoCommit="True">
                            <toolkit:DataForm.EditTemplate>
                                <DataTemplate>
                                    <StackPanel>
                                        <toolkit:DataField Label="ID">
                                            <TextBox IsReadOnly="True" Text="{Binding ID, Mode=OneWay}" />
                                        </toolkit:DataField>
                                              <toolkit:DataField Label="Image 1">
                                                    <StackPanel>                                                    
                                                        <Image x:Name="Image1" Source="{Binding FileName1, Converter={StaticResource ImageSourceConverter}}"  Width="100" Height="100"  HorizontalAlignment="Center" VerticalAlignment="Center"></Image>
                                                        <Button x:Name="UploadImage1" Click="UploadImage1_Click" Content="Upload"></Button>
                                                        <TextBlock Height="23" Name="StatusText1" Text="{Binding FileName1, Mode=TwoWay}" />
                                                    </StackPanel>
                                                </toolkit:DataField>
                                    </StackPanel>
                                </DataTemplate>
                            </toolkit:DataForm.EditTemplate>
                        </toolkit:DataForm>


    My converter:

        public class ImageSourceConverter : IValueConverter
        {
            #region IValueConverter Members
            public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
            {
                string host = Application.Current.Host.Source.AbsoluteUri;
                host = host.Remove(host.IndexOf("/ClientBin"));
    
                if (value != null && !String.Empty.Equals(value))
                {
                    return new BitmapImage(new Uri(host + value));
                }
                return new BitmapImage(new Uri(host + "/Images/no_image.jpg"));
            }
    
            public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
            {
                throw new NotImplementedException();
            }
            #endregion
        }
    


    My code behind where I upload the image on the server and I update the view in the form:


            private void UploadImage1_Click(object sender, RoutedEventArgs e)
            {
                imageName = "Image1";
    
                OpenFileDialog dlg = new OpenFileDialog();
                dlg.Multiselect = false;
                dlg.Filter = "JPG Images (*.jpg)|*.jpg|PNG Images (*.png)|*.png";
    
                bool? retval = dlg.ShowDialog();
    
                if (retval != null && retval == true)
                {
                    if (dlg.File.Length > 2000000)
                    {
                        MessageBox.Show("File is too big. Max file size is 2 MB.");
                        return;
                    }
                    UploadFile(dlg.File.Name, dlg.File.OpenRead());
                    TextBlock StatusText1 = (TextBlock)dataForm1.FindNameInContent("StatusText1");
                    StatusText1.Text = "/Images/"+dlg.File.Name;
    
                    var bi = new BitmapImage();
                    bi.SetSource(dlg.File.OpenRead());
    
                    //update view
                    var img1 = ((FrameworkElement)((Button)sender).Parent).FindName("Image1") as Image;
                    img1.Source = bi;
    
                }
                else
                {
                    //StatusText.Text = "No file selected...";
                }
            }
    
    




    The problem can be reproduced with the following steps:

    1. I select an item in the datagrid and then I click on the edit button of the dataform
    2. I edit the image of an item with the OpenFileDialog and then submit
    3. Select another item from the datagrid and I select again edit in the dataform
    4. PROBLEM: the old image of the previous item is shown in the dataform!
    5. Click cancel and then edit again in the dataform
    6. It works correctly! (it does not shown the old image but the correct one)


    Is it my error or a bug in the dataform?
    THANKS!

    Wednesday, March 16, 2011 5:48 PM

All replies