gpt4 book ai didi

c# - 属性绑定(bind)没有响应

转载 作者:太空宇宙 更新时间:2023-11-03 13:20:36 25 4
gpt4 key购买 nike

代码可能并不完美,甚至没有完全的意义,但我正在努力使其绑定(bind)正常工作。

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;

除非设置了其他东西来更新 Canvas 的 DataContext,否则当控件加载时绑定(bind)到它的 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;
}

关于c# - 属性绑定(bind)没有响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24418425/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com