【问题标题】:Background image data binding from source not works来自源的背景图像数据绑定不起作用
【发布时间】:2017-08-06 11:55:10
【问题描述】:

在我的窗口内 xaml:

<Grid HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
    <Grid.Style>
        <Style TargetType="{x:Type Grid}">
            <Setter Property="Background">
                <Setter.Value>
                    <VisualBrush>
                        <VisualBrush.Visual>
                            <Image Source="{Binding Path=ImageSource,Converter={StaticResource imgconverter}}">
                                <Image.BitmapEffect>
                                    <BlurBitmapEffect KernelType="Box" />
                                </Image.BitmapEffect>
                            </Image>
                        </VisualBrush.Visual>
                    </VisualBrush>
                </Setter.Value>
            </Setter>
        </Style>
    </Grid.Style>
</Grid>

(我在窗口资源中添加了这个转换器)

我想向这个网格(使用 MVVM 模式)添加具有模糊效果的背景图像,但我的属性从未在我的视图模型中调用。如果我只使用带有“Path=”的转换器。转换器可以工作,但我必须在转换器中使用静态 ImageSource,因为如果我为 ImageSource(到路径)(BitmapImage、ImageSource、..等)添加任何对象类型,转换器将不会调用。 (我尝试将 UpdateSourceTrigger 与 PropertyChanged 值一起使用,但此解决方案对我没有帮助。)转换器只是一个不需要的解决方案,因为这是正确设置我的背景的唯一方法,因为我的 ImageSouce 属性需要值,但没有它就无法工作转换器,不幸的是,如果我添加任何绑定路径,转换器也将无法工作。

这是我在 ViewModel 中的属性:

    private ImageSource _imageSource;
    public ImageSource ImageSource
    {
        get
        {
            return _imageSource;
        }
        set
        {
            _imageSource = value;
            OnPropertyChanged();
        }
    }

任何想法使用 MVVM 模式正确设置具有模糊效果的背景图像并且不使用 uri 路径? (我不想将图像保存到物理存储)

【问题讨论】:

  • 我的网格的DataContext和我的窗口DataContext一样。我没有为此网格设置不同的数据上下文。此窗口中的其他功能与数据绑定一起正常工作。我什么都不能传递给转换器。如果我设置任何路径,转换器将不会调用。这就是我现在必须使用静态 ImageResource 来显示我的图像的原因。

标签: wpf mvvm


【解决方案1】:

VisualBrush 不是元素树的一部分,因此它不会继承 GridDataContext

但是您可以将Image 定义为资源并使用{x:Reference} 将其Source 属性绑定到父窗口的属性。这应该有效:

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApplication1"
        mc:Ignorable="d"
        Title="MainWindow" Height="300" Width="300" x:Name="win">
    <Window.Resources>
        <local:imgconverter x:Key="imgconverter" />
    </Window.Resources>
    <Grid>
        <Grid.Resources>
            <Image x:Key="img" Source="{Binding Path=DataContext.ImageSource,Converter={StaticResource imgconverter}, Source={x:Reference win}}">
                <Image.BitmapEffect>
                    <BlurBitmapEffect KernelType="Box" />
                </Image.BitmapEffect>
            </Image>
        </Grid.Resources>
        <Grid.Style>
            <Style TargetType="{x:Type Grid}">
                <Setter Property="Background">
                    <Setter.Value>
                        <VisualBrush Visual="{StaticResource img}" />
                    </Setter.Value>
                </Setter>
            </Style>
        </Grid.Style>
        <TextBlock Text="..." />
    </Grid>
</Window>

【讨论】:

  • 我将您的解决方案复制到我的视图 xaml 但结果是相同的问题(有和没有转换器)。在视图模型中,ImageSource 属性具有值,但从不从视图调用,如果我添加路径值,转换器将不会调用。
  • 如果 ImageSource 属性属于窗口的 DataContext(视图模型),您应该将绑定的 Path to 更改为 DataContext.ImageSource。我编辑了我的答案。
  • 现在它适用于您最后的更改!我为这个问题放弃了两天。 (现在转换器是多余的,因为它现在来自 viewmodel 属性:))非常感谢!
猜你喜欢
  • 2016-05-16
  • 1970-01-01
  • 2018-12-23
  • 1970-01-01
  • 2023-03-03
  • 2014-05-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多