有没有人知道为什么绑定对DecodePixelWidth属性不起作用?当我通过xml设置它 - 一切正常 - 我看到缩放图像。当我使用绑定时 - 图像没有缩放。
<Image VerticalAlignment="Top" Width="2292" Stretch="None">
<Image.Source>
<BitmapImage UriSource="{Binding Src}" DecodePixelWidth="{Binding DecodePixelWidth}" />
</Image.Source>
</Image>
public int DecodePixelWidth
{
get
{
return _decodePixelWidth;
}
set
{
if (value != _decodePixelWidth)
{
_decodePixelWidth = value;
NotifyPropertyChanged("DecodePixelWidth");
}
}
}
谢谢!
答案 0 :(得分:0)
我遇到了同样的问题,我发现这个解决方案http://diegoparolin-codeplus.blogspot.it/2014/09/set-decodepixelheight-property-binding.html对我有用。
在我的MVVM WPF应用程序中,我想使用存储在app.config中的值调整位图图像的大小。因此,为此,我尝试将Image.Height属性和BitmapImage.DecodePixelHeight直接绑定到我的ViewModel,就像这样:
在app.config中
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<appSettings>
<add key="DocumentsToBeFiledImageHeight" value="240"/>
<add key="DocumentsToBeFiledImageDecodePixelHeight" value="240"/>
</appSettings>
</configuration>
然后在我的ViewModel中添加了这些属性
private int _documentsToBeFiledImageDecodePixelHeight;
public int DocumentsToBeFiledImageDecodePixelHeight
{
get { return _documentsToBeFiledImageDecodePixelHeight; }
set
{
if (value != _documentsToBeFiledImageDecodePixelHeight)
{
_documentsToBeFiledImageDecodePixelHeight = value;
RaisePropertyChanged(DocumentsToBeFiledImageDecodePixelHeightPropertyName);
}
}
}
private double _documentsToBeFiledImageHeight;
public double DocumentsToBeFiledImageHeight
{
get { return _documentsToBeFiledImageHeight; }
set
{
if (value != _documentsToBeFiledImageHeight)
{
_documentsToBeFiledImageHeight = value;
RaisePropertyChanged(DocumentsToBeFiledImageHeightPropertyName);
}
}
}
和此代码
DocumentsToBeFiledImageHeight = Convert.ToDouble(ConfigurationManager.AppSettings [“DocumentsToBeFiledImageHeight”]);
DocumentsToBeFiledImageDecodePixelHeight = Convert.ToInt32(ConfigurationManager.AppSettings [“DocumentsToBeFiledImageDecodePixelHeight”]);
然后在XAML中我添加了这段代码
<Image Grid.Row="0" Height="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}, Path=DataContext.DocumentsToBeFiledImageHeight, UpdateSourceTrigger=PropertyChanged}">
<Image.Source>
<BitmapImage DecodePixelHeight="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}, Path=DataContext.DocumentsToBeFiledImageDecodePixelHeight, UpdateSourceTrigger=PropertyChanged}"
UriSource="{Binding Path=FullName}" />
</Image.Source>
</Image>
不幸的是它不起作用,似乎没有正确设置DecodePixelHeight属性。 因此,我试图直接从app.config恢复该值。我在app.config文件的用户设置部分添加了这些参数
<userSettings>
<Agest.AO.Client.Properties.Settings>
<setting name="DocumentsToBeFiledImageDecodePixelHeight" serializeAs="String">
<value>240</value>
</setting>
<setting name="DocumentsToBeFiledImageHeight" serializeAs="String">
<value>240</value>
</setting>
</Agest.AO.Client.Properties.Settings>
</userSettings>
我以这种方式更改了XAML代码
xmlns:localme="clr-namespace:Agest.AO.Client.Properties"
<Image Grid.Row="0" Height="{Binding Source={x:Static localme:Settings.Default}, Path=DocumentsToBeFiledImageHeight, Mode=OneWay}">
<Image.Source>
<BitmapImage DecodePixelHeight="{Binding Source={x:Static localme:Settings.Default}, Path=DocumentsToBeFiledImageDecodePixelHeight, Mode=OneWay}"
UriSource="{Binding Path=FullName}" />
</Image.Source>
</Image>
现在它可以正常工作,我可以直接在配置文件中设置值。
问候!