我有一个配置文件,开发人员可以通过传入一个字符串来指定文本颜色:
<text value="Hello, World" color="Red"/>
Run Code Online (Sandbox Code Playgroud)
而不是有一个巨大的开关语句寻找所有可能的颜色,只是使用类System.Drawing.Brushes中的属性而不是内部我可以这样说:
Brush color = Brushes.Black; // Default
// later on...
this.color = (Brush)Enum.Parse(typeof(Brush), prasedValue("color"));
Run Code Online (Sandbox Code Playgroud)
除了刷子/画笔中的值不是枚举.所以Enum.Parse没有给我带来快乐.建议?
当绑定到DataTemplate内的 SolidColorBrush属性时,我在Visual Studio输出窗口中收到此警告:
System.Windows.Data错误:2:找不到目标元素的管理FrameworkElement或FrameworkContentElement.BindingExpression:路径= MyColor; 的DataItem = NULL; target元素是'SolidColorBrush'(HashCode = 22943289); 目标属性是'颜色'(类型'颜色')
如果我直接绑定在DataTemplate外部的矩形元素上,那么一切都很好.
任何人都可以从下面的示例代码中解释为什么这两个明显相似的用法存在差异:
我的看法:
<UserControl.Resources>
<vm:TestViewModel x:Key="_myTestVM"/>
<DataTemplate x:Key="testVMDataTemplate">
<Grid>
<Rectangle Height="30" Width="200" Margin="5">
<Rectangle.Fill>
<SolidColorBrush Color="{Binding Path=MyColor}" />
</Rectangle.Fill>
</Rectangle>
</Grid>
</DataTemplate>
</UserControl.Resources>
<Grid>
<StackPanel DataContext="{StaticResource _myTestVM}">
<!-- Binding *outside* the DataTemplate = works fine -->
<Rectangle Height="30" Width="200" Margin="5">
<Rectangle.Fill>
<SolidColorBrush Color="{Binding Path=MyColor}"/>
</Rectangle.Fill>
</Rectangle>
<!-- Binding *inside* the DataTemplate = output warning -->
<ContentControl Content="{Binding}" ContentTemplate="{StaticResource testVMDataTemplate}"/>
</StackPanel>
</Grid>
Run Code Online (Sandbox Code Playgroud)
我的ViewModel(TestViewModel):
public class TestViewModel …Run Code Online (Sandbox Code Playgroud) 有没有办法,使用XAML,根据其中一个单元格的内容动态设置行的背景?
谢谢,
菲尔
如何将设置中定义的Color Bkg(System.Drawing.Color)与XAML中的Style 绑定?
的xmlns:道具= "CLR的命名空间:App.Properties"
<Style TargetType="{x:Type StackPanel}" x:Key="_itemStyle">
<Setter Property="Background" Value="{Binding Path=Bkg, Source={x:Static props:Settings.Default}}"/>
Run Code Online (Sandbox Code Playgroud)
Background属性是System.Windows.Media.Color类型,所以它需要以某种方式转换?
如何在不牺牲性能的情况下在WPF(使用MVVM)中对图像进行着色/着色?纯粹的XAML解决方案将是理想的,因为修改代码中的位图将导致大量更改图像的性能损失.图像不仅仅是简单的形状,因此无法使用路径.
我们可以在ViewModel中使用针对UI/UI框架的程序集中的类吗?
今天我讨论了一个问题,其中一个人非常执着,不能在PresentationModel中使用来自PresentationCore.dll的类.(好像他以前没有使用过ICommand)但是这样对吗?
因为我理解MVVM只是一种解耦View&ViewModel的模式?它没有说明我可以在ViewModel中使用什么类型的类,只要它们不创建视图(ViewModel没有直接引用视图或任何有关视图的特定实现或类型的知识).
请不要回答这是一个好的做法,我只是想明确MVVM.
wpf ×5
c# ×4
colors ×2
xaml ×2
.net ×1
bind ×1
binding ×1
brush ×1
data-binding ×1
datatemplate ×1
graphics ×1
mvvm ×1
settings ×1
warnings ×1
wpfdatagrid ×1
wpftoolkit ×1