这是场景:
1)打开Visual Studio 2008,创建新项目......
2)选择WPF Custom Control Library作为项目类型
3)打开位于Themes文件夹中的Generic.xaml资源字典
4)添加一个简单的画笔,如:
<SolidColorBrush x:Key ="BackgroundBrush"Color ="Yellow"/>
5)从中更改嵌套Borer控件的Background属性
Background ="{TemplateBinding Background}"
至
Background ="{DynamicResource BackgroundBrush}"
现在,当您在任何项目中编译和使用此控件时,背景将不会为黄色.但是,如果您将background属性更改为StaticResource
Background ="{StaticResource BackgroundBrush}"
......它会起作用.为什么是这样?为什么StaticResource工作但不是DynamicResource?
我有一种情况,我试图解析资源字典中引用的数据模板中的可视化组件,引用app.xaml标记的静态资源,请参阅下面的示例.
/App.xaml
<Application
x:Class="App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:composition="clr-namespace:Composition"
ShutdownMode="OnMainWindowClose">
<Application.Resources>
<ResourceDictionary>
<composition:ApplicationContainer x:Key="ApplicationContainer"/>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/MyApp;component/Composition/DataTemplates.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
Run Code Online (Sandbox Code Playgroud)
/Composition/DataTemplates.xaml
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:projectvm="clr-namespace:ViewModels.ProjectManagement"
xmlns:unity="clr-namespace:Extensions">
<DataTemplate DataType="{x:Type projectvm:ProjectDocument}">
<ContentControl>
<!-- Custom Extension that resolves a component from the composition container -->
<unity:Resolve Container="{StaticResource ApplicationContainer}" TargetType="{x:Type projectvm:ProjectDocument}" ContractName="ProjectDocument"/>
</ContentControl>
</DataTemplate>
</ResourceDictionary>
Run Code Online (Sandbox Code Playgroud)
ERROR就在上面,统一:Resolve行找不到ApplicationContainer静态资源,我不明白为什么.
这是我的XAML代码
<Window x:Class="Q316995.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:dxg="http://schemas.devexpress.com/winfx/2008/xaml/grid"
xmlns:dxgt="http://schemas.devexpress.com/winfx/2008/xaml/grid/themekeys"
xmlns:local="clr-namespace:Q316995"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<ResourceDictionary>
<Style x:Key="LastRowHighlighted"
BasedOn="{StaticResource {dxgt:GridRowThemeKey ResourceKey=RowStyle}}"
TargetType="{x:Type dxg:GridRowContent}">
</Style>
</ResourceDictionary>
</Window.Resources>
</Window>
Run Code Online (Sandbox Code Playgroud)
它与c#代码背后的相似之处在于
Binding _Binding = new Binding();
_Binding.Converter = new LastRowHighlighter();
Setter _Setter = new Setter();
_Setter.Property = GridRowContent.FontWeightProperty;
_Setter.Value = _Binding;
Style _Style = new System.Windows.Style();
//_Style.BasedOn = new Style(typeof(GridRowContent));
_Style.TargetType = typeof(GridRowContent);
_Style.Setters.Add(_Setter);
grid.Resources.Add("LastRowHighlighted", _Style);
Run Code Online (Sandbox Code Playgroud)
我不知道如何更换
BasedOn="{StaticResource {dxgt:GridRowThemeKey ResourceKey=RowStyle}}"
Run Code Online (Sandbox Code Playgroud)
用c#代码.Grid是Devexpress的GridControl
我正在寻找一种定义WPF资源(目前用作静态资源)的方法,该资源可从我的应用程序中的任何位置访问。
我的资源在此资源字典中定义:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Style x:Key="flatButtonStyle" BasedOn="{StaticResource {x:Static ToolBar.ButtonStyleKey}}" TargetType="{x:Type Button}">
<Setter Property="BorderThickness" Value="4"/>
</Style>
</ResourceDictionary>
Run Code Online (Sandbox Code Playgroud)
选择此问题的答案表明,我必须将该资源字典合并到我的App.xaml文件中(名为的项目AppWideResources):
<Application x:Class="AppWideResources.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="Window1.xaml">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/AppWideResources;component/CommonResources.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
Run Code Online (Sandbox Code Playgroud)
起初,这似乎有效;该窗口中的按钮以适当的扁平样式设计,带有超厚边框:
<Window x:Class="AppWideResources.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="AppWideResources" Height="300" Width="300"
>
<StackPanel>
<Button Style="{StaticResource flatButtonStyle}" Content="Test" HorizontalAlignment="Stretch"/>
</StackPanel>
</Window>
Run Code Online (Sandbox Code Playgroud)
但是,一旦在控件模板中使用共享资源,此操作就会停止:
我(出于此问题的目的,对此进行了极其简化)控制:
using System;
using System.Windows;
using System.Windows.Controls;
namespace AppWideResources
{
public class MyControl : Control
{
static MyControl()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(MyControl), new FrameworkPropertyMetadata(typeof(MyControl)));
} …Run Code Online (Sandbox Code Playgroud) 我有一个项目控件模板,如下所示.我需要为模板中的每个项目单独设置colorProvider实例.items控件中的每个项目都需要Color Provider的单独实例,具体取决于它绑定的项目.如何创建staticresource的多个副本,以便staticresource仅适用于该项目.
<ItemsControl x:Name="itemsControl" ItemsSource="{Binding DataList}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Vertical"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid MinHeight="250">
<ContentPresenter Content="{Binding }" ContentTemplateSelector="{StaticResource chartSelector}">
<ContentPresenter.Resources>
<v:ColorProvider x:Key="colorProvider"/>
</ContentPresenter.Resources>
</ContentPresenter>
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
Run Code Online (Sandbox Code Playgroud) 我压缩了CSS和图像文件,以便CSS可以访问图像.
http://www.salesforce.com/us/developer/docs/pages/Content/pages_resources.htm
我已经按照文档代码,但它根本没有呈现.
<apex:stylesheet value="{!URLFOR($Resource.jquery_dialog_css_img,
'jquery_ui_one_dot_eight_dot_eleven.css')}" />
Run Code Online (Sandbox Code Playgroud)
压缩文件结构是:
[jquery_dialog_css_img] ---- jquery_ui_one_dot_eight_dot_eleven.css
|
|---images ----- someImage.jpg
Run Code Online (Sandbox Code Playgroud)
渲染visualforce页面时,导入标记如下所示:
<link class="user" href="/resource/1301916406000/
manenabi__jquery_dialog_css_img/
jquery_ui_one_dot_eight_dot_eleven.css"
rel="stylesheet"
type="text/css" />
Run Code Online (Sandbox Code Playgroud)
它看起来是正确的,我不知道为什么它不工作.当我点击生成的链接时:
/ resource/1301916406000/manenabi__jquery_dialog_css_img/jquery_ui_one_dot_eight_dot_eleven.css
页面全是空白的(我认为)意味着路径不正确.(如果是正确的,弹出的页面会显示像js或css这样的代码)
是否可以将null添加为标记元素的静态资源?我希望能够使用{StaticResource myKey}语法引用值.目前,我需要引用的值为null,但将来可能不会.我在标记的其余部分有多个引用值,我希望它们引用资源键而不是{x:Null}.
我希望这样做:
<Window.Resources>
<x:Null key="myKey" />
</Window.Resources>
Run Code Online (Sandbox Code Playgroud)
......但这不起作用.它编译但在运行时会引发XamlParseException,表示无法解析资源引用.
Apologies for the maybe hazy nature of this question, but I am fairly new to WPF and am consequently struggling with the issue of resources.
My problem is that I have a DataGrid that I wish to assign a style to that describes properties such as the FontSize and Background/Foreground colours (when the mouse hovers over the rows). I can do this successfully as follows:
<Window x:Class="WpfApplication11.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<DataGrid Name="DataGrid1" ItemsSource="{Binding Path=Fibers}">
<DataGrid.RowStyle> …Run Code Online (Sandbox Code Playgroud) 您好,我有一个单元测试,它附加到一个事件并根据处理程序中 eventArgs 的属性更新计数器,如下所示:
protected void UpdateCounts(object sender, EventArgs eventArgs)
{
lock (lockobject)
{
Counts[eventArgs.Target]++;
}
}
Run Code Online (Sandbox Code Playgroud)
Counts 是该类中所有单元测试共享的静态字典资源。我断言,在测试结束时,我断言对于特定的 Target 值(Target 是一个枚举),计数为 6。当我通过此测试进行调试时,它总是通过最终断言,但是,当我在没有任何断点的情况下运行它时,Target 值的计数可以是 7 或 8,但永远不会是 6。
我意识到许多尝试访问字典中条目的线程可能会出现竞争条件,这就是我在增量周围放置锁的原因。我还有一个 TestInitialize 方法,该方法在每个测试运行之前运行,如下所示:
[TestInitialize]
public void InitTest()
{
foreach (TargetType x in Enum.GetValues(typeof(Target)))
{
Counts[x] = 0;
}
}
Run Code Online (Sandbox Code Playgroud)
有人对这里发生的事情有任何了解吗?
我有一个简单的WPF XAML窗口,我需要在以下XAML中创建一个StaticResource密钥。
XAML源代码是
<Window x:Class="WpfApplication1.Trigger"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:super="clr-namespace:WpfApplication1"
Title="Trigger" Height="300" Width="300">
<Grid>
<Border x:Name="m_Border" Width="100" Height="30" HorizontalAlignment="Center" VerticalAlignment="Top" Background="#FFF2FFC6" Margin="0,20,0,0">
<Button x:Name="btn" Content="iApp" HorizontalAlignment="Center" VerticalAlignment="Center" Width="75" Visibility="{Binding IsMouseOver,ElementName=m_Border, Converter={StaticResource BooleanToVisibilityConverterKey}, ConverterParameter=Normal}"/>
</Border>
</Grid>
</Window>
Run Code Online (Sandbox Code Playgroud)
我的转换器C#源代码:
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Data;
namespace WpfApplication1
{
public enum BooleanToVisibilityConverterType
{
Normal = 1,
Reverse = 2
}
public class BooleanToVisibilityConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo …Run Code Online (Sandbox Code Playgroud) staticresource ×10
wpf ×8
xaml ×4
c# ×3
resources ×2
.net ×1
app.xaml ×1
datagrid ×1
mstest ×1
null ×1
salesforce ×1
settext ×1
styles ×1
unit-testing ×1
visualforce ×1