【问题标题】:ResourceDictionary shared variableResourceDictionary 共享变量
【发布时间】:2015-10-21 23:54:04
【问题描述】:

在我的 ResourceDictionary Resource_Color我已经定义了这一行:

    <Color x:Key="MainColor">Black</Color>

现在,我想在另一个资源文件中使用这个 MainColor,例如 Resource_DataGrid

        <Style.Triggers>
        <Trigger Property="IsSelected" Value="True">
            <Setter Property="Foreground" Value="{StaticResource MainColor}" />

。 . . 在这种模式下不起作用。 我该如何写这个声明?

【问题讨论】:

    标签: wpf xaml resourcedictionary


    【解决方案1】:

    使用ResourceDictionary.MergedDictionaries

    Window1.xaml

    <Window x:Class="Window1"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="Window1" Height="Auto" Width="Auto">
      <Window.Resources>
        <ResourceDictionary Source="Dictionary2.xaml" />
      </Window.Resources>
      <Grid>
        <TextBox Text="TESTING" FontWeight="Bold" Margin="30" />
      </Grid>
    </Window>
    

    Dictionary1.xaml

    <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
      <Color x:Key="MainColor">Blue</Color>
      <SolidColorBrush x:Key="MainBrush" Color="{StaticResource MainColor}" />
    </ResourceDictionary>
    

    Dictionary2.xaml

    <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
      <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="Dictionary1.xaml" />
      </ResourceDictionary.MergedDictionaries>
    
      <Style TargetType="TextBox">
        <Setter Property="Foreground" Value="{StaticResource MainBrush}" />
      </Style>
    </ResourceDictionary>
    

    此外,Foreground 属性通常是 Brush,而不是 Color。我的例子说明了这一点。

    【讨论】:

    • 非常感谢您的帮助 ;-)
    猜你喜欢
    • 1970-01-01
    • 2015-06-15
    • 2014-12-14
    • 2012-07-21
    • 2011-08-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多