Binding and DecodePixelWidth property
本文关键字:property DecodePixelWidth and Binding | 更新日期: 2023-09-27 18:10:12
有谁知道为什么绑定不能为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");
}
}
}
谢谢!
我有同样的问题,我已经找到了这个解决方案http://diegoparolin-codeplus.blogspot.it/2014/09/set-decodepixelheight-property-binding.html,它适用于我。
在我的MVVM WPF应用程序中,我想用存储在app.config中的值来调整位图图像的大小。因此,为了做到这一点,我尝试绑定图像。高度属性和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>
现在它工作了,我可以直接在配置文件中设置值。
问候!