将该属性绑定到ResourceDictionary中的资源

时间:2014-08-20 07:49:20

标签: wpf xaml wpf-controls

我的应用程序有一些菜单按钮,按钮显示当前选择,例如,如果用户选择"红色",按钮图像是"红笔"。我为按钮图像准备了XAML文件,就像这样。

<?xml version="1.0" encoding="utf-8"?>
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 

                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:local="clr-namespace:SmartScreen">
    <DrawingBrush x:Key="sample_test" Stretch="Uniform">
        <DrawingBrush.Drawing>
            <DrawingGroup>
                <DrawingGroup.Children>
                    <GeometryDrawing Brush="#FF000000" Geometry="F1 M 87.7748,67.8528L 87.7748,54.9208C 87.7748,52.9101 86.1441,51.2781 84.1375,51.2781L 61.3575,51.2781L 61.3575,24.4635L 63.1001,24.4635C 64.7948,24.4635 66.1788,23.0808 66.1788,21.3861L 66.1788,8.06213C 66.1788,6.36613 64.7948,4.9888 63.1001,4.9888L 49.7748,4.9888C 48.0801,4.9888 46.6988,6.36613 46.6988,8.06213L 46.6988,21.3861C 46.6988,23.0808 48.0801,24.4635 49.7748,24.4635L 51.5095,24.4635L 51.5095,51.2781L 28.7348,51.2781C 27.2241,51.2781 25.9228,52.2061 25.3735,53.5115C 26.0281,53.4501 26.6948,53.4168 27.3601,53.4168C 37.6975,53.4168 46.2041,61.3808 47.0895,71.4915L 84.1375,71.4915C 86.1441,71.4915 87.7748,69.8595 87.7748,67.8528 Z "/>
                    <GeometryDrawing Brush="#FF000000" Geometry="F1 M 84.138,84.7019C 86.1447,84.7019 87.7753,83.0739 87.7753,81.0672L 87.7753,79.0565C 87.7753,77.0365 86.1447,75.4139 84.138,75.4139L 47.046,75.4139C 46.666,78.8512 45.402,82.0219 43.4873,84.7019L 84.138,84.7019 Z "/>
                    <GeometryDrawing Brush="#FF000000" Geometry="F1 M 27.3607,85.9973C 20.302,85.9973 14.5847,80.2747 14.5847,73.2253C 14.5847,66.1707 20.302,60.4467 27.3607,60.4467C 34.4193,60.4467 40.134,66.1707 40.134,73.2253C 40.134,80.2747 34.4193,85.9973 27.3607,85.9973 Z M 27.3607,56.9733C 18.386,56.9733 11.114,64.2467 11.114,73.2253C 11.114,82.1933 18.386,89.4693 27.3607,89.4693C 36.3313,89.4693 43.6113,82.1933 43.6113,73.2253C 43.6113,64.2467 36.3313,56.9733 27.3607,56.9733 Z "/>
                    <GeometryDrawing Brush="{Binding Path=SampleColor}" Geometry="F1 M 35.8893,71.2825L 29.3013,71.2825L 29.3013,63.6785C 29.3013,62.6105 28.4333,61.7465 27.3613,61.7465C 26.288,61.7465 25.4253,62.6105 25.4253,63.6785L 25.4253,72.2132C 25.4253,72.3905 25.4507,72.5559 25.4933,72.7199C 25.4507,72.8785 25.4253,73.0425 25.4253,73.2252C 25.4253,74.2932 26.288,75.1572 27.3613,75.1572L 35.8893,75.1572C 36.964,75.1572 37.8293,74.2932 37.8293,73.2252C 37.8293,72.1532 36.964,71.2825 35.8893,71.2825 Z "/>
                </DrawingGroup.Children>
            </DrawingGroup>
        </DrawingBrush.Drawing>
    </DrawingBrush>
</ResourceDictionary>

将此文件(名为&#39; sample_test.xaml&#39;)放入Applicaton.Resources的ResourceDictionary。

<Application x:Class="SmartScreen.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             StartupUri="MainWindow.xaml">
    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="sample_test.xaml"/>
           </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
</Application>

MainWindow.xaml正在关注,

<Window x:Class="SmartScreen.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
        xmlns:local="clr-namespace:Test">
<Grid Loaded="Grid_Loaded">

    <Label Name="label1" Width="100" Height="100" HorizontalAlignment="Center" VerticalAlignment="Center" 
                Background="{DynamicResource sample_test}"
                />
    <Label Name="label2" Width="100" Height="100" HorizontalAlignment="Right" VerticalAlignment="Center" Margin="100,0">
        <Label.Background>
            <DrawingBrush Stretch="Uniform">
                <DrawingBrush.Drawing>
                    <DrawingGroup>
                        <DrawingGroup.Children>
                            <GeometryDrawing Brush="#FF000000" Geometry="F1 M 87.7748,67.8528L 87.7748,54.9208C 87.7748,52.9101 86.1441,51.2781 84.1375,51.2781L 61.3575,51.2781L 61.3575,24.4635L 63.1001,24.4635C 64.7948,24.4635 66.1788,23.0808 66.1788,21.3861L 66.1788,8.06213C 66.1788,6.36613 64.7948,4.9888 63.1001,4.9888L 49.7748,4.9888C 48.0801,4.9888 46.6988,6.36613 46.6988,8.06213L 46.6988,21.3861C 46.6988,23.0808 48.0801,24.4635 49.7748,24.4635L 51.5095,24.4635L 51.5095,51.2781L 28.7348,51.2781C 27.2241,51.2781 25.9228,52.2061 25.3735,53.5115C 26.0281,53.4501 26.6948,53.4168 27.3601,53.4168C 37.6975,53.4168 46.2041,61.3808 47.0895,71.4915L 84.1375,71.4915C 86.1441,71.4915 87.7748,69.8595 87.7748,67.8528 Z "/>
                            <GeometryDrawing Brush="#FF000000" Geometry="F1 M 84.138,84.7019C 86.1447,84.7019 87.7753,83.0739 87.7753,81.0672L 87.7753,79.0565C 87.7753,77.0365 86.1447,75.4139 84.138,75.4139L 47.046,75.4139C 46.666,78.8512 45.402,82.0219 43.4873,84.7019L 84.138,84.7019 Z "/>
                            <GeometryDrawing Brush="#FF000000" Geometry="F1 M 27.3607,85.9973C 20.302,85.9973 14.5847,80.2747 14.5847,73.2253C 14.5847,66.1707 20.302,60.4467 27.3607,60.4467C 34.4193,60.4467 40.134,66.1707 40.134,73.2253C 40.134,80.2747 34.4193,85.9973 27.3607,85.9973 Z M 27.3607,56.9733C 18.386,56.9733 11.114,64.2467 11.114,73.2253C 11.114,82.1933 18.386,89.4693 27.3607,89.4693C 36.3313,89.4693 43.6113,82.1933 43.6113,73.2253C 43.6113,64.2467 36.3313,56.9733 27.3607,56.9733 Z "/>
                            <GeometryDrawing Brush="{Binding SampleColor}" Geometry="F1 M 35.8893,71.2825L 29.3013,71.2825L 29.3013,63.6785C 29.3013,62.6105 28.4333,61.7465 27.3613,61.7465C 26.288,61.7465 25.4253,62.6105 25.4253,63.6785L 25.4253,72.2132C 25.4253,72.3905 25.4507,72.5559 25.4933,72.7199C 25.4507,72.8785 25.4253,73.0425 25.4253,73.2252C 25.4253,74.2932 26.288,75.1572 27.3613,75.1572L 35.8893,75.1572C 36.964,75.1572 37.8293,74.2932 37.8293,73.2252C 37.8293,72.1532 36.964,71.2825 35.8893,71.2825 Z "/>
                        </DrawingGroup.Children>
                    </DrawingGroup>
                </DrawingBrush.Drawing>
            </DrawingBrush>

        </Label.Background>
    </Label>
</Grid>
</Window>

最后,MainWindow的视图模型应该具有名为&#34; SampleColor&#34;的属性。像这样;

namespace Sample
{
    ...
    public class MainWindowViewModel
    {
        public Brush SampleColor
        {
            get
            {
                return Brushes.Red;
            }
        }
    }
}

结果,标签名为&#34; label2&#34;显示红色部分,但&#34; label1&#34;没有按&#39;吨。 我不知道如何将DataContext告诉ResourceDictionary中的资源。

This非常类似,但DrawinBrush没有DataContext。

0 个答案:

没有答案