Ono
Ono

Reputation: 1357

Property binding not responding

The code might not be perfect or even does not make perfect sense, but what i am trying to get it the binding working.

C# code:

void PlayImages()
{
    string testImageFolder = "C:\\Development2012\\ThorImage\\TIS_Development\\GUI\\Controls\\OverlayManager\\TestImages";

    DirectoryInfo d = new DirectoryInfo(testImageFolder);//Assuming Test is your Folder
    FileInfo[] Files = d.GetFiles("*.tif"); //Getting Text files


    List<string> tiffImage = new List<string>();

    for (int n = 0; n < Files.Length; n++)
    {               
        tiffImage.Add(Files[n].Directory.ToString() + "\\" + Files[n].Name);   
    }


    OMTestViewModel vm = new OMTestViewModel();
    if (vm == null)
    {
        return;
    }

    vm.TiffFiles = tiffImage;
}

The code above will go to ViewModel:

public List<string> TiffFiles
{
    get 
    { 
        return _tiffFiles; 
    }
    set 
    { 
        _tiffFiles = value;
        OnPropertyChanged("Bitmap");
    }
}

But it does not go to following code, which is also in the same ViewModel C# files as above code:

public WriteableBitmap Bitmap
{
    get
    {
        switch (GetColorChannels())
        {
            case 1:
                {
                    var width  = 1024;
                    var height = 1024;

                    bitmap = new WriteableBitmap(width, height, 96, 96, PixelFormats.Gray16, null);
                    var pixels = new ushort[width * height];
                    for (var y = 0; y < height; ++y)
                        for (var x = 0; x < width; ++x)
                        {
                            var v = (0x10000 * 2 * x / width + 0x10000 * 3 * y / height);
                            var isMirror = (v / 0x10000) % 2 == 1;
                            v = v % 0xFFFF;
                            if (isMirror)
                                v = 0xFFFF - v;

                            pixels[y * width + x] = (ushort)v;
                        }

                    bitmap.WritePixels(new Int32Rect(0, 0, width, height), pixels, width * 2, 0);

                    var encoder = new PngBitmapEncoder();
                    encoder.Frames.Add(BitmapFrame.Create(bitmap));
                    using (var stream = System.IO.File.Create("gray16.png"))
                        encoder.Save(stream);
                }
                break;
            case 2:
                { }
                break;
            default:
                break;
        }

        return bitmap;
    }
}

xaml:

<Canvas x:Name="imageCanvas"
    RenderOptions.BitmapScalingMode="NearestNeighbor"
    RenderOptions.EdgeMode="Aliased" Margin="0,52,0,0">
    <Canvas.Background>
        <ImageBrush x:Name="image1"
                        Stretch="None"
                        AlignmentX="Left"
                        AlignmentY="Top"
                        ImageSource="{Binding Path=Bitmap, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}">
        </ImageBrush>
    </Canvas.Background>                    
</Canvas>

I am wondering why OnPropertyChanged("Bitmap") does invoke Bitmap property? How do I change to invoke Bitmap? Thanks.

Upvotes: 2

Views: 80

Answers (1)

Austin Mullins
Austin Mullins

Reputation: 7437

You can't do this:

OMTestViewModel vm = new OMTestViewModel();
if (vm == null)
{
    return;
}

vm.TiffFiles = tiffImage;

Unless something else is set up to update the DataContext of your Canvas, the ViewModel bound to it when the control loads is not the one you're creating here.

Instead, you should do something like this:

XAML

<Canvas xmlns:local="clr-namespace:MyNamespace"
        x:Name="imageCanvas"
        RenderOptions.BitmapScalingMode="NearestNeighbor"
        RenderOptions.EdgeMode="Aliased" Margin="0,52,0,0">
        <Canvas.Resources>
            <local:OMTestViewModel x:Key="vm"/>
        </Canvas.Resources>
        <Canvas.Background>
          <ImageBrush ...
             ImageSource="{Binding Source={StaticResource vm},
               Path=Bitmap, ...}" />
        </Canvas.Background>
        ...

C#

Declare this inside your class that owns imageCanvas:

private OMTestViewModel vm;

Inside Constructor:

vm = (OMTestViewModel)imageCanvas.Resources["vm"];

And then remove the part that instantiates a new ViewModel in PlayImages:

void PlayImages()
{
    string testImageFolder = "...";

    DirectoryInfo d = new DirectoryInfo(testImageFolder);
    FileInfo[] Files = d.GetFiles("*.tif");

    List<string> tiffImage = new List<string>();

    for (int n = 0; n < Files.Length; n++)
    {               
        tiffImage.Add(Files[n].Directory.ToString() + "\\" + Files[n].Name);   
    }

    vm.TiffFiles = tiffImage;
}

Upvotes: 1

Related Questions