gpt4 book ai didi

wpf - 如何使 WPF DrawingImage 元素的颜色动态化?

转载 作者:行者123 更新时间:2023-12-04 12:24:45 25 4
gpt4 key购买 nike

我们需要使某些矢量图形图像在运行时改变图形中某些元素的颜色。似乎基于静态或动态资源值设置这些颜色是行不通的。我们希望拥有同一个图形的多个版本,每个版本都能够将某些图形元素(路径、线条等)设置为不同的颜色,因此我认为动态资源方法行不通。剩下的数据绑定(bind)似乎是正确的方法。我们更新图形以使用数据绑定(bind) expr 而不是硬编码的画笔颜色,如下所示:

<DrawingImage x:Key="Graphic1">
<DrawingImage.Drawing>
<DrawingGroup>
<DrawingGroup.Children>
<GeometryDrawing Geometry="F1 M 8.4073,23.9233L">
<GeometryDrawing.Pen>
<Pen LineJoin="Round" Brush="{Binding Line1Brush, Mode=OneWay}"/>
</GeometryDrawing.Pen>
</GeometryDrawing>
<GeometryDrawing Geometry="F1 M 3.6875,2.56251L">
<GeometryDrawing.Pen>
<Pen LineJoin="Round" Brush="{Binding Line2Brush, Mode=OneWay}"/>
</GeometryDrawing.Pen>
</GeometryDrawing>
</DrawingGroup.Children>
</DrawingGroup>
</DrawingImage.Drawing>
</DrawingImage>

然后我们在这种情况下为 Graphic1 的每个实例创建一个 View 模型对象(支持 INotifyPropertyChanged)实例,并确保它同时具有 Line1BrushLine2Brush属性(property)。听起来不错,但我无法让它工作。我假设这个图形,它本身在资源字典中定义为 Image 对象,我尝试设置它们的 DataContext我在调试窗口中得到数据绑定(bind)错误输出。这是图像 XAML:
<Image x:Name="Pulse1" Grid.Column="0" Source="{StaticResource Graphic1}"/>
<Image x:Name="Pulse2" Grid.Column="1" Source="{StaticResource Graphic1}"/>

然后在Window的 Initialize方法我像这样设置他们的数据上下文:
 public MainWindow()
{
InitializeComponent();
this.DataContext = this;
this.PulseImage1 = new PulseImageViewModel();
this.PulseImage2 = new PulseImageViewModel();
this.PulseImage2.Line1Brush = Brushes.Green;
this.PulseImage2.Line2Brush = Brushes.Red;
this.Pulse1.DataContext = this.PulseImage1;
this.Pulse2.DataContext = this.PulseImage2;
}

在哪里 PulseImageViewModel (如下所示)定义了两个属性 Line1BrushLine2Brush ,每个都会触发 PropertyChanged 事件。
public class PulseImageViewModel : INotifyPropertyChanged
{
private Brush _line1Brush = Brushes.Yellow;
private Brush _line2Brush = Brushes.Black;

public event PropertyChangedEventHandler PropertyChanged;

public Brush Line1Brush
{
get { return _line1Brush; }
set
{
if (_line1Brush != value)
{
_line1Brush = value;
NotifyPropertyChanged("Line1Brush");
}
}
}
public Brush Line2Brush
{
get { return _line2Brush; }
set
{
if (_line2Brush != value)
{
_line2Brush = value;
NotifyPropertyChanged("Line2Brush");
}
}
}

private void NotifyPropertyChanged(string propertyName)
{
var del = PropertyChanged;
if (del != null)
{
del(this, new PropertyChangedEventArgs(propertyName));
}
}
}

但是我收到数据绑定(bind)错误,表明 WPF 正在寻找 Line1Brush在顶级 MainWindow 对象上,而不是在我的 PulseImageViewModel 对象上。关于我做错了什么的任何想法,或者是否有更好的方法来实现我在矢量图形上动态改变颜色的目标?此外,如果用户没有连接 View 模型对象,如果图形可以默认为一组漂亮的静态颜色,那就太好了。

最佳答案

StaticResource将有 DataContext来自 Window我相信。如果你使用 DynamicResource 会改变吗? ?

编辑 我得到和你一样的结果,Snoop 没有帮助。即使将 DataContext 移动到 XAML 也不会改变任何事情。为了好玩,我切换到将 TextBox 拉入标签的内容中,你瞧,这行得通……不知道有什么区别。

<Window.Resources>
<TextBlock x:Key="Text1"
Text="{Binding Line1Brush}" />
</Window.Resources>
<Grid>
<!-- Confusingly enough, this works -->
<Label Content="{StaticResource Text1}"
DataContext="{Binding PulseImage1}" />
</Grid>

编辑 2 以下作品:
<DataTemplate x:Key="Graphic1">
<Image>
<Image.Source>
<DrawingImage>
<DrawingImage.Drawing>
<DrawingGroup>
<DrawingGroup.Children>
<GeometryDrawing Geometry="F1 M 8.4073,23.9233L">
<GeometryDrawing.Pen>
<Pen LineJoin="Round"
Brush="{Binding Line1Brush, Mode=OneWay}" />
</GeometryDrawing.Pen>
</GeometryDrawing>
<GeometryDrawing Geometry="F1 M 3.6875,2.56251L">
<GeometryDrawing.Pen>
<Pen LineJoin="Round"
Brush="{Binding Line2Brush, Mode=OneWay}" />
</GeometryDrawing.Pen>
</GeometryDrawing>
</DrawingGroup.Children>
</DrawingGroup>
</DrawingImage.Drawing>
</DrawingImage>
</Image.Source>
</Image>
</DataTemplate>

XAML 看起来像:
<ContentPresenter ContentTemplate="{StaticResource Graphic1}"
Content="{Binding PulseImage1}"
Grid.Column="0" />
<ContentPresenter ContentTemplate="{StaticResource Graphic1}"
Content="{Binding PulseImage2}"
Grid.Column="1" />

关于wpf - 如何使 WPF DrawingImage 元素的颜色动态化?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4949131/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com