- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
例如,我有一些具有简单模型的 MVVM WPF 应用程序:
public class MyObject
{
public string F1 { get; set; }
public string F2 { get; set; }
}
和创建 3 行的简单 View 模型:
public class MyViewModel
{
public ObservableCollection<MyObject> Objects { get; set; }
public MyViewModel()
{
Objects = new ObservableCollection<MyObject>
{
new MyObject{F1 = "V1",F2 = "B1"},
new MyObject{F1 = "V2",F2 = "B2"},
new MyObject{F1 = "V3",F2 = "V3"}
};
}
}
在 View 中,我有一个包含手动定义列的 DataGrid
,并且我为每一列设置了 CellStyle
。两种样式都在 Window.Resources
block 中定义。但是对于第一列,我使用 StaticResource
,对于第二列,我使用 DynamicResource
查看 XAML:
<Window x:Class="WpfApplication12.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" x:Name="WholeWindow">
<Window.Resources>
<Style x:Key="BaseCellClass" TargetType="DataGridCell">
<Setter Property="Foreground" Value="Blue" />
</Style>
</Window.Resources>
<Grid>
<DataGrid AutoGenerateColumns="False" ItemsSource="{Binding ElementName=WholeWindow, Path=ViewModel.Objects}">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding F1}" Header="F1" CellStyle="{StaticResource BaseCellClass}" />
<DataGridTextColumn Binding="{Binding F2}" Header="F2" CellStyle="{DynamicResource BaseCellClass}" />
</DataGrid.Columns>
</DataGrid>
</Grid>
所以问题是:在第二列中,资源没有应用到该列。
最佳答案
您可以为 DataGridCell
中的属性创建资源Style
然后将它们引用为 DynamicResource
在 Style
内定义:
根据您的示例,它看起来像这样:
<Window.Resources>
<SolidColorBrush x:Key="ForegroundBrush" Color="Blue"/>
<Style x:Key="BaseCellClass" TargetType="DataGridCell">
<Setter Property="Foreground" Value="{DynamicResource ForegroundBrush}" />
</Style>
</Window.Resources>
<Grid>
<DataGrid AutoGenerateColumns="False" ItemsSource="{Binding ElementName=WholeWindow, Path=ViewModel.Objects}">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding F1}" Header="F1" CellStyle="{StaticResource BaseCellClass}" />
<DataGridTextColumn Binding="{Binding F2}" Header="F2" CellStyle="{StaticResource BaseCellClass}" />
</DataGrid.Columns>
</DataGrid>
</Grid>
资源当然会位于单独的资源文件中。
关于c# - 为什么我不能将 DynamicResource 与 DataGridColumn.CellStyle 一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17475083/
我对 DynamicResource 和 MergedDictionary 有疑问。我有一个可以用不同语言(英语、西类牙语、德语)显示的软件,并且我有很多 ResourceDictionaries。我
FrameworkElement 的 FindResource 方法允许获取在此框架元素中定义的资源。但是,我有一些动态资源位于运行时加载的另一个程序集中。从 XAML 中使用 DynamicReso
我有一个 WPF (3.5) 应用程序,它使用 Prism 以编程方式实例化多个 View ,然后将它们添加到区域中。我看到的问题是 View 中作为 DynamicResources 应用的样式在第
我的应用程序从单独的 XAML 文件中定义的资源字典加载所有字符串。 DynamicResource 键在运行时可能不存在。 在普通绑定(bind)中有一个 Fallback 值,可以像这样使用:
似乎使用 DynamicResource 引用应用程序级资源可能会导致发生内存泄漏。 请参阅此 WPF 论坛 post了解更多信息、如何重现它以及一些解决方法。 我的问题是:还有其他人遇到过吗?如果是
情况看起来是这样的: 我在应用程序中有很多图标,它们的大小不一。 例如,我使用图标作为 DynamicResource: 部分图标为.xaml格式,部分图标为.png格式 我添加了新的图标,例如:
如果 Resources 字典不可观察,DynamicResource 引用如何工作? 资源字典的添加/删除方法是否具有内部代码,可以“轮询”所有 DynamicResource 引用并在删除或添加它
如果没有,我怎样才能实现类似的行为?我想将某个控件的 Backgrond 属性绑定(bind)到 Brush 类型的属性。当该属性设置为 null 时,我想使用默认背景(使用资源键解析)。 最佳答案
我有一个带有该 XAML 的 UserControl:
我正在尝试将 DynamicResource 值绑定(bind)到位于 viewmodel 中的公共(public)属性,然后再对其进行操作。静态资源基本上显示的是图像。任何建议,谢谢。
这不是 MahApps.Metro 特定的,但这恰好是我正在使用的。我有一组 ViewModel,它们有一个 string 属性,表示要使用资源 XAML 文件中的图标。 public class C
我在使用 ComponentResourceKey 时发现一些意外行为。出现问题是因为我使用了 SystemColors 类中使用的名称。 考虑以下代码: 首先,我将 a 中的 ComponentRe
我正在使用 ResourceDictionary 在 .NET 4 中进行本地化。有没有人有使用字符串格式的值的解决方案? 例如,假设我有一个键为“SomeKey”的值: You ran {
以下代码不起作用。我如何使它工作? 最佳答案 这是 DynamicResource MarkupExtension 的错误用法.正确的是: 假设您有一个资源,其 key “VM.ImageKey”
视觉工作室 2010 | .NET/WPF 4.0 我认为这可能是 WPF 错误,但我似乎找不到关于它的错误报告。为了弥补我只是遗漏了一些明显的可能性,我转向 stackoverflow 寻求答案!
这是场景: 1)打开Visual Studio 2008,新建项目... 2)选择WPF自定义控件库作为项目类型 3) 打开位于 Themes 文件夹中的 Generic.xaml 资源字典 4)添加
我想知道是否有一种方法可以将 wpf 样式的 basedOn 属性与动态资源一起使用。例如 这例如抛出一个错误,表明动态资源与 BaseOn 样式结合使用是不可能的。 我想知道怎么会有人这
我正在尝试动态更改画笔的颜色。我写了一个非常简单的例子,但我不明白为什么它不起作用。 我已经在我的应用程序的 ResourceDictionary 中定义了前景颜色和使用该颜色作为 DynamicRe
我正在尝试根据状态和事件为边框着色。我有一个开启和关闭状态,带有“Poked”事件。 On = Green,Off = Red,当 Poked 时,我想从蓝色淡化回其原始颜色(基于其状态)。所有都在应
我程序的主菜单使用了一个由MenuItems组成的ContextMenu。在程序本地化期间(使用资源字典),我将 DynamicResource 设置为每个 MenuItems 的 Header。奇怪
我是一名优秀的程序员,十分优秀!