2017-03-15 55 views
1

在我的窗口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模式),但我的屬性從未在我的viewmodel調用。如果我只用轉換器「Path =」。轉換器將工作,但我必須在轉換器中使用靜態ImageSource,因爲如果我添加任何對象類型的ImageSource(路徑)(BitmapImage,ImageSource,..等),轉換器將不會調用。 (我嘗試使用UpdateSourceTrigger與PropertyChanged值,但此解決方案對我沒有幫助。)轉換器只是一個不需要的解決方案,因爲這是正確設置我的背景的唯一方法,因爲我的ImageSouce屬性需要值,但它不工作沒有轉換器,不幸的是,如果我添加任何綁定路徑,轉換器將無法工作。

這裏是我的財產視圖模型裏面:

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

任何想法與MVVM模式和不使用URI路徑正確設置我的模糊效果的背景圖片? (我不想將圖像保存到物理存儲)

+0

我的網格的DataContext與我的窗口DataContext相同。我沒有爲這個網格設置不同的數據上下文。此窗口中的其他功能可以正確使用數據綁定。我無法傳遞給轉換器。如果我設置了任何路徑,轉換器將不會調用。這就是爲什麼我必須使用靜態ImageResource來顯示我的圖像。 –

回答

1

A 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> 
+0

我將您的解決方案複製到了我的視圖xaml中,但結果是相同的問題(帶和不帶轉換器)。在視圖模型中,ImageSource屬性具有值,但從不從視圖調用,如果添加路徑值,轉換器將不會調用。 –

+0

如果ImageSource屬性屬於窗口的DataContext(視圖模型),則應該將綁定的路徑更改爲DataContext.ImageSource。我編輯了我的答案。 – mm8

+0

現在它適用於您的最後更改!我爲此解決了兩天的問題。 (現在轉換器是多餘的,因爲現在它的作品來自viewmodel屬性:))非常感謝你! –