【问题标题】:Resizing image bound to datasource with an IValueConverter使用 IValueConverter 调整绑定到数据源的图像大小
【发布时间】:2014-08-20 03:10:46
【问题描述】:

我有一个图像文件绑定到网格内的图像元素,如下所示

   <FlipView x:Name="FlipView" ItemsSource="{Binding Source={StaticResource ItemsViewSource}}" SelectionChanged="FlipView_SelectionChanged">
        <FlipView.ItemTemplate>
            <DataTemplate>
                <Grid SizeChanged="Grid_SizeChanged">
                    <Image Source="{Binding File, Converter={StaticResource ImageConverter}}" Stretch="None" />
                </Grid>
            </DataTemplate>
        </FlipView.ItemTemplate>
    </FlipView>

图像设置为不拉伸,因为我正在 IValueConverter 内部调整大小,以便小图像不会像素化,而大图像仍会缩小以适应屏幕。

当我的应用程序大小发生变化时,如何触发 IValueConverter 重新计算当前显示的 Image 的大小?

【问题讨论】:

    标签: c# data-binding winrt-xaml ivalueconverter


    【解决方案1】:

    实现INotifyPropertyChanged接口,当你的应用大小发生变化时,设置File属性值。

    public PropertyChangedEventHandler PropertyChanged;
    
    private void NotifyPropertyChanged(string propertyName)
    {
         PropertyChangedEventHandler handler = this.PropertyChanged;
         if (handler != null)
         {
              var e = new PropertyChangedEventArgs(propertyName);
              handler(this, e);
         }
    }
    
    private String file;
    
    public String File
    {
        get 
        { 
             return file; 
        }
        set
        {
             file = value;
             NotifyPropertyChanged("File");
        }
    }
    

    【讨论】:

    • 对不起,我不明白。我将使用什么来处理 PropertyChangedEvent?
    • 它是处理它的数据绑定框架的一部分
    • 那行得通。我还必须在绑定到 PropertyChanged 时设置 UpdateSource。
    • 是的,没错,你需要更新数据源。
    猜你喜欢
    • 1970-01-01
    • 2013-01-05
    • 2011-10-24
    • 2013-02-24
    • 2014-08-17
    • 1970-01-01
    • 1970-01-01
    • 2011-01-29
    • 2020-05-10
    相关资源
    最近更新 更多