.net WPF/XAML中的动态作用域资源?

.net WPF/XAML中的动态作用域资源?,.net,wpf,xaml,dynamic,resources,.net,Wpf,Xaml,Dynamic,Resources,我有两个Xaml文件,一个包含一个DataTemplate,它有一个图像画笔的资源定义,另一个包含一个显示此DataTemplate的内容控件。数据模板已绑定到视图模型类。除了ImageBrush资源外,一切似乎都正常,它只是显示为白色。。。有什么想法吗 文件1:ViewModel的数据模板 <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns

我有两个Xaml文件,一个包含一个DataTemplate,它有一个图像画笔的资源定义,另一个包含一个显示此DataTemplate的内容控件。数据模板已绑定到视图模型类。除了ImageBrush资源外,一切似乎都正常,它只是显示为白色。。。有什么想法吗

文件1: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>
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");
                }
            }
        }


    }
}

这是一种应对问题。如果您将资源移动到
网格
级别,而不是
数据模板
本身,它将工作:

<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
的内容在一个单独的名称范围内,我将在本周一尝试,如果是这样的话,那么你肯定会得到投票和接受:-p
<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>