- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在尝试构建一个继承自 Image 控件的自定义类,但我在绑定(bind)方面遇到了一些问题。
我在我的资源中使用 DrawingImage(这是为矢量图像提供的)
所以在我的 App.xaml 资源中我有一些 DrawingImages,其中一个示例:
<DrawingImage x:Key="Shutdown">
<DrawingImage.Drawing>
<DrawingGroup>
<DrawingGroup.Children>
<DrawingGroup>
<DrawingGroup.Children>
<GeometryDrawing Brush="{x:Null}">
<GeometryDrawing.Pen>
Here--> <Pen Brush="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type local:IconImage}}, Path=Colour, UpdateSourceTrigger=PropertyChanged}" Thickness="2" />
</GeometryDrawing.Pen>
<GeometryDrawing.Geometry>
<PathGeometry FillRule="Nonzero" Figures="M8.332,4.941C5.759,6.271 4,8.956 4,12.052 4,16.47 7.582,20.052 12,20.052 16.418,20.052 20,16.47 20,12.052 20,8.911 18.19,6.193 15.555,4.884" />
</GeometryDrawing.Geometry>
</GeometryDrawing>
Here--> <GeometryDrawing Brush="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type local:IconImage}}, Path=Colour, UpdateSourceTrigger=PropertyChanged}" Pen="{x:Null}">
<GeometryDrawing.Geometry>
<RectangleGeometry RadiusX="0" RadiusY="0" Rect="11,2,2,10" />
</GeometryDrawing.Geometry>
</GeometryDrawing>
</DrawingGroup.Children>
<DrawingGroup.ClipGeometry>
<RectangleGeometry Rect="0,0,24,24" />
</DrawingGroup.ClipGeometry>
</DrawingGroup>
</DrawingGroup.Children>
</DrawingGroup>
</DrawingImage.Drawing>
</DrawingImage>
我已经构建了我的自定义类如下:
public class IconImage : Image
{
public enum Icons
{
None,
Shutdown,
Minimize
}
private Icons _icon;
public Icons Icon
{
get { return _icon; }
set
{
_icon = value;
if (value == Icons.None)
{
Visibility = Visibility.Hidden;
}
else
{
Visibility = Visibility.Visible;
this.SetResourceReference(Image.SourceProperty, value.ToString());
}
}
}
public static readonly DependencyProperty ColourProperty = DependencyProperty.Register("Colour", typeof(Brush), typeof(IconImage), new PropertyMetadata(Brushes.Black));
public Brush Colour
{
get { return (Brush)GetValue(ColourProperty); }
set { SetValue(ColourProperty, value); }
}
}
用法:
<local:IconImage Icon="Shutdown" Colour="CornflowerBlue" />
问题是我在 DrawingImages 中的 Brush 绑定(bind)不会绑定(bind)到我在自定义类中的 Color DependancyProperty,我不知道如何完成这个,或者,如果它可能的话。如果我对画笔进行硬编码,它就可以完美地工作(很明显)。我只需要能够在设计时或在后面的代码中随时从控件更改它们的颜色。
提前致谢!
-肖恩-
最佳答案
你可以让它像这样工作。首先将资源标记为非共享,因为你想让你的 DrawingImage
根据它所属的 IconImage
改变外观,这对共享没有多大意义资源(在这种情况下共享意味着像单例 - 只有一个全局实例):
<DrawingImage x:Key="Shutdown" x:Shared="False">
<!-- the rest -->
</DrawingImage>
然后不使用 SetResourceReference
而是这样做:
var resource = FindResource(value.ToString());
// alternative
// var resource = Application.Current.Resources[value.ToString()];
this.Source = (ImageSource)resource;
这与将您的 DrawingImage
xaml 直接放在 IconImage
下大致相同,就像其他答案所建议的那样。
顺便说一句,您最好为您的资源设置更多唯一名称,以避免在树下意外使用具有相同名称的资源(特别是如果您打算使用 FindResource
)。
关于c# - 将应用程序资源中的 DependancyProperty 绑定(bind)到控件 DependancyProperty(正在使用该资源),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46944845/
我正在尝试构建一个继承自 Image 控件的自定义类,但我在绑定(bind)方面遇到了一些问题。 我在我的资源中使用 DrawingImage(这是为矢量图像提供的) 所以在我的 App.xaml 资
我正在尝试将双 handle slider 修改为通用双模型。 [更新]:XAML 代码: C# 代码隐藏: #region Dependency Property - Minimum
我发现了这个问题 MVVM and the TextBox's SelectedText property .但是,我无法让解决方案发挥作用。这是我的非工作代码,我试图在第二个文本框中显示第一个文本框
我是一名优秀的程序员,十分优秀!