属性绑定没有响应

时间:2014-06-25 20:57:54

标签: c# wpf xaml mvvm binding

代码可能不完美,甚至没有完美的意义,但我试图让它具有约束力。

C#代码:

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

上面的代码将转到ViewModel:

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

但它没有转到下面的代码,它也与上面代码中的ViewModel C#文件相同:

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>

我想知道为什么OnPropertyChanged("Bitmap")会调用Bitmap属性?如何更改为调用Bitmap?感谢。

1 个答案:

答案 0 :(得分:1)

你不能这样做:

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

vm.TiffFiles = tiffImage;

除非设置其他内容来更新Canvas的DataContext,否则当控件加载时绑定到它的ViewModel不是您在此处创建的那个。

相反,你应该这样做:

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#

在拥有imageCanvas

的类中声明此内容
private OMTestViewModel vm;

Inside Constructor:

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

然后删除在PlayImages中实例化新ViewModel的部分:

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