属性绑定没有响应

本文关键字:响应 绑定 属性 | 更新日期: 2023-09-27 18:10:22

代码可能不完美,甚至没有完美的意义,但我试图让它的绑定工作。

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 ?谢谢。

属性绑定没有响应

你不能这么做:

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

除非设置了其他东西来更新画布的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;

内部构造函数:

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