wpf - 来自源的背景图像数据绑定(bind)不起作用

标签 wpf mvvm

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

这是我在 ViewModel 中的属性:
    private ImageSource _imageSource;
    public ImageSource ImageSource
    {
        get
        {
            return _imageSource;
        }
        set
        {
            _imageSource = value;
            OnPropertyChanged();
        }
    }

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

最佳答案

一个 VisualBrush不是元素树的一部分,因此它不继承 DataContextGrid .

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

<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>

关于wpf - 来自源的背景图像数据绑定(bind)不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42814922/

相关文章:

c# - 在数据重新加载时保存 WPF TreeView 状态

wpf - 如何阻止在 DotNetBrowser 中加载远程内容?

c# - 无法在我的 WPF 网格中创建列

c# - 双向绑定(bind)不起作用

C# 通用用户控件 WPF

c# - WPF/Xaml 将 DataGrid 列标题和单元格绑定(bind)到不同的值

WPF,如何在观看 TypeDescriptor 时正确取消 Hook 处理程序

WPF MVVM ListBox SelectedItem 未突出显示

c# - 如何使用 ReactiveCommand?

c# - 如何从 ViewModel 获取集合到 View?