WPF / XAML中的动态范围资源?

时间:2009-08-15 20:46:22

标签: .net wpf xaml dynamic resources

我有2个Xaml文件,一个包含一个DataTemplate,它有一个Image画笔的资源定义,另一个包含一个显示这个DataTemplate的内容控件。数据模板绑定到视图模型类。一切似乎都有效,除了ImageBrush资源,它只显示白色...任何想法?

文件1:ViewModel的DataTemplate

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

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

为了完整起见! 视图模型的CodeBehind

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");
                }
            }
        }


    }
}

1 个答案:

答案 0 :(得分:2)

这是某种名称问题。如果您将资源移至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的内容位于不同的名称范围内。