2009-08-15 16 views
1

2つのXamlファイルがあります.1つはImageブラシのリソース定義を持つDataTemplateを含み、もう1つはこのDataTemplateを示すコンテンツコントロールを含んでいます。データテンプレートは、ビューモデルクラスにバインドされています。すべては、ImageBrushリソースを除いて動作するようです。WPF/XAMLの動的スコープリソース

ファイル1:DataTemplateをViewModelに

<ResourceDictionary 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:vm="clr-namespace:SEL.MfgTestDev.ESS.ViewModel" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    mc:Ignorable="d"> 

    <DataTemplate DataType="{x:Type vm:PresenterViewModel}"> 
     <DataTemplate.Resources> 
      <ImageBrush x:Key="PresenterTitleBarFillBrush" 
      TileMode="Tile" 
      Viewbox="{Binding Path=FillBrushDimensions, Mode=Default}" 
      ViewboxUnits="Absolute" 
      Viewport="{Binding Path=FillBrushPatternSize, Mode=Default}" 
      ViewportUnits="Absolute" 
      ImageSource="{Binding Path=FillImage, Mode=Default}"/> 
     </DataTemplate.Resources> 
     <Grid d:DesignWidth="1440" d:DesignHeight="900"> 
      <Grid.ColumnDefinitions> 
       <ColumnDefinition Width="*"/> 
       <ColumnDefinition Width="192"/> 
      </Grid.ColumnDefinitions> 
      <Grid.RowDefinitions> 
       <RowDefinition Height="120"/> 
       <RowDefinition Height="*"/> 
      </Grid.RowDefinitions> 
      <DockPanel HorizontalAlignment="Stretch" Width="Auto" LastChildFill="True" Background="{x:Null}" Grid.ColumnSpan="2"> 
       <Image Source="{Binding Path=ImageSource, Mode=Default}"/> 
       <Rectangle Fill="{DynamicResource PresenterTitleBarFillBrush}"/> 
      </DockPanel> 
     </Grid> 
    </DataTemplate> 

</ResourceDictionary> 

ファイル2:メインウィンドウクラスのDataTemplate経由でinstanciatesビューモデルです。

<Window x:Class="SEL.MfgTestDev.ESS.ESSMainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:vm="clr-namespace:SEL.MfgTestDev.ESS.ViewModel" 
    Title="ESS Control Window" 
    Height="900" 
    Width="1440" 
    WindowState="Maximized" 
    WindowStyle="None" 
    ResizeMode="NoResize" 
    DataContext="{Binding}"> 

    <Window.Resources> 
     <ResourceDictionary Source="PresenterViewModel.xaml" /> 
    </Window.Resources> 

    <ContentControl> 
     <ContentControl.Content> 
      <vm:PresenterViewModel ImageSource="XAMLResources\SEL25YearsTitleBar.bmp" FillImage="XAMLResources\SEL25YearsFillPattern.bmp" FillBrushDimensions="0,0,5,110" FillBrushPatternSize="0,0,5,120"/> 
     </ContentControl.Content> 
    </ContentControl> 

</Window> 

そして、完全性のために!ビューモデルの 分離コード

using System; 
using System.Collections.Generic; 
using System.Text; 
using System.Windows; 
using System.Windows.Controls; 
using System.Windows.Data; 
using System.Windows.Documents; 
using System.Windows.Input; 
using System.Windows.Media; 
using System.Windows.Media.Imaging; 
using System.Windows.Shapes; 

namespace SEL.MfgTestDev.ESS.ViewModel 
{ 
    public class PresenterViewModel : ViewModelBase 
    { 
     public PresenterViewModel() 
     { 

     } 

     //DataBindings 
     private ImageSource _imageSource; 

     public ImageSource ImageSource 
     { 
      get 
      { 
       return _imageSource; 
      } 
      set 
      { 
       if (_imageSource != value) 
       { 

        _imageSource = value; 
        OnPropertyChanged("ImageSource"); 
       } 
      } 
     } 

     private Rect _fillBrushPatternSize; 

     public Rect FillBrushPatternSize 
     { 
      get 
      { 
       return _fillBrushPatternSize; 
      } 
      set 
      { 
       if (_fillBrushPatternSize != value) 
       { 
        _fillBrushPatternSize = value; 
        OnPropertyChanged("FillBrushPatternSize"); 
       } 
      } 
     } 

     private Rect _fillBrushDimensions; 

     public Rect FillBrushDimensions 
     { 
      get 
      { 
       return _fillBrushDimensions; 
      } 
      set 
      { 
       if (_fillBrushDimensions != value) 
       { 
        _fillBrushDimensions = value; 
        OnPropertyChanged("FillBrushDimensions"); 
       } 
      } 
     } 

     private ImageSource _fillImage; 

     public ImageSource FillImage 
     { 
      get 
      { 
       return _fillImage; 
      } 
      set 
      { 
       if (_fillImage != value) 
       { 
        _fillImage = value; 
        OnPropertyChanged("FillImage"); 
       } 
      } 
     } 


    } 
} 

答えて

2

これはnamescopingの問題のいくつかの種類です。あなたはGridではなくDataTemplate自体のレベルにあなたのリソースを移動した場合、それは動作します:

<DataTemplate DataType="{x:Type vm:PresenterViewModel}"> 
    <Grid> 
     <Grid.Resources> 
      <ImageBrush x:Key="PresenterTitleBarFillBrush" 
       TileMode="Tile" 
       Viewbox="{Binding Path=FillBrushDimensions, Mode=Default}" 
       ViewboxUnits="Absolute" 
       Viewport="{Binding Path=FillBrushPatternSize, Mode=Default}" 
       ViewportUnits="Absolute" 
       ImageSource="{Binding Path=FillImage, Mode=Default}"/> 
     </Grid.Resources> 
     <Grid.ColumnDefinitions> 
      <ColumnDefinition Width="*"/> 
      <ColumnDefinition Width="192"/> 
     </Grid.ColumnDefinitions> 
     <Grid.RowDefinitions> 
      <RowDefinition Height="120"/> 
      <RowDefinition Height="*"/> 
     </Grid.RowDefinitions> 
     <DockPanel HorizontalAlignment="Stretch" Width="Auto" LastChildFill="True" Background="{x:Null}" Grid.ColumnSpan="2"> 
      <Image Source="{Binding Path=ImageSource, Mode=Default}"/> 
      <Rectangle Fill="{DynamicResource PresenterTitleBarFillBrush}"/> 
     </DockPanel> 
    </Grid> 
</DataTemplate> 

を私は何が起こっていることはDataTemplateのリソースがDataTemplateの内容とは別の名前スコープにあることだと思います。

+0

私はこのmondayを試してみるつもりなら、あなたは必ず投票して受け入れられます: - P – Firoso