views:

987

answers:

2

I am currently working on a SilverLight 3 Application. I am using MVVM Pattern and Prism. I have everything working except the following item. On one of my views I have to use an OpenFileDialog. I attempted to do this in the ViewModel only to find out the security model of SilverLight prohibits it because it is only allowed to be user initiated. I've since moved the OpenFileDialog code to the code-behind of the View. Here is my problem though. Although I have binding to the source set to TwoWay it is not hitting the setter of the property in my ViewModel.

Example of Image control with binding:

<Image x:Name="imgCard" Height="283" Width="463" Canvas.Left="8" Canvas.Top="8" OpacityMask="White" Source="{Binding Path=CardImage, Mode=TwoWay}"/>

Button Used by user:

<Button x:Name="btnUpload" Height="20" Width="122" Canvas.Left="8" Canvas.Top="319" Content="Upload Image" Click="btnUpload_Click" />

Click Event:

private void btnUpload_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = "PNG Files(*.png)|*.png";

            ofd.ShowDialog();
            using (Stream stream = ofd.File.OpenRead())
            {
                BitmapImage image = new BitmapImage();
                image.SetSource(stream);
                imgCard.Source = image;
            }
        }

My ViewModel is implementing the INotifyPropertyChanged and has the following property.

BitmapSource CardImage
            {
                get
                {
                    return _imageSource;
                }
                set
                {
                    _imageSource = value;
                    NotifyPropertyChanged("CardImage");
                }
            }

If I put a break point on the Setter. It never hits it.

+1  A: 

At least in Silverlight 2, I think the following rule might explain why you are seeing this behavior. "If a Dependency Property is bound and in code the property is set to a value explicitly, the binding is removed." (source)

Maybe this has changed for Silverlight 3? In that case, I have no suggestions.

ckittel
A: 

Ok this is a hack but it works. Because I have to fire the OpenFileDialog from the UI I can instead of updating the control directly reverse tether to the DataContext to update the property. This works and still renders the UI the way I expect.

NOTE: HACK Until I find a better way.

private void btnUpload_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = "PNG Files(*.png)|*.png";

            ofd.ShowDialog();
            using (Stream stream = ofd.File.OpenRead())
            {
                BitmapImage image = new BitmapImage();
                image.SetSource(stream);
                BitmapSource b = image;

                //HACK: This works but now I'm tethered a bit.  This updates the context property CardImage.
                ((DesignerViewModel) this.DataContext).CardImage = b;
                //imgCard.Source = b;
            }
        }
cjibo