gpt4 book ai didi

.net - 如何在样式中使用附加属性?

转载 作者:行者123 更新时间:2023-12-03 07:50:29 24 4
gpt4 key购买 nike

我在 ButtonStyle 中创建了一个图像。现在我创建了一个附加属性,以便我可以为该图像设置源。应该直截了当,但我坚持下去。

这是我缩短的 ButtonStyle:

<Style x:Key="ToolBarButtonStyle"
TargetType="Button">
...
<Image x:Name="toolbarImage"
Source="{TemplateBinding PrismExt:ImageSourceAttachable:ImageSource}"
Width="48"
Height="48" />
...
</Style>

这是附加的属性定义,请注意,我不知道如何修复回调,因为依赖属性似乎是按钮而不是图像。并且 Button 不会在其样式中公开我的图像。这很棘手。
namespace SalesContactManagement.Infrastructure.PrismExt
{
public class ImgSourceAttachable
{
public static void SetImgSource(DependencyObject obj, string imgSource)
{
obj.SetValue(ImgSourceProperty, imgSource);
}

public static string GetImgSource(DependencyObject obj)
{
return obj.GetValue(ImgSourceProperty).ToString();
}

// Using a DependencyProperty as the backing store for MyProperty. This enables animation, styling, binding, etc...
public static readonly DependencyProperty ImgSourceProperty =
DependencyProperty.RegisterAttached("ImgSource", typeof(string), typeof(ImgSourceAttachable), new PropertyMetadata(Callback));

private static void Callback(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
//((Button)d).Source = new BitmapImage(new Uri(Application.Current.Host.Source, e.NewValue.ToString()));
}
}
}

这就是我在 XAML 中设置图像源的方式:
<Button PrismExt:ImgSourceAttachable.ImgSource="./Images/New.png"
Style="{StaticResource ToolBarButtonStyle}" />

请问有什么想法吗?
非常感谢,

最佳答案

以下是如何以样式设置附加属性

<Style x:Key="ToolBarButtonStyle" TargetType="Button">
<Setter Property="PrismExt:ImgSourceAttachable.ImgSource"
Value="./Images/New.png"/>
<!--...-->
</Style>

当绑定(bind)到附加属性时,路径应该在括号内,所以尝试使用 RelativeSourceTemplatedParent 绑定(bind)反而
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Image x:Name="toolbarImage"
Source="{Binding RelativeSource={RelativeSource TemplatedParent},
Path=(PrismExt:ImgSourceAttachable.ImgSource)}"
Width="48"
Height="48">
</Image>
</ControlTemplate>
</Setter.Value>
</Setter>

编辑:上面的代码在 WPF 中工作,在 Silverlight 中 Image在运行时显示,但在设计器中失败并出现异常。您可以在 PropertyChangedCallback 中使用以下代码来获取 Image作为一种解决方法
private static void Callback(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
Button button = d as Button;
Image image = GetVisualChild<Image>(button);
if (image == null)
{
RoutedEventHandler loadedEventHandler = null;
loadedEventHandler = (object sender, RoutedEventArgs ea) =>
{
button.Loaded -= loadedEventHandler;
button.ApplyTemplate();
image = GetVisualChild<Image>(button);
// Here you can use the image
};
button.Loaded += loadedEventHandler;
}
else
{
// Here you can use the image
}
}
private static T GetVisualChild<T>(DependencyObject parent) where T : DependencyObject
{
T child = default(T);

int numVisuals = VisualTreeHelper.GetChildrenCount(parent);
for (int i = 0; i < numVisuals; i++)
{
DependencyObject v = (DependencyObject)VisualTreeHelper.GetChild(parent, i);
child = v as T;
if (child == null)
{
child = GetVisualChild<T>(v);
}
if (child != null)
{
break;
}
}
return child;
}

关于.net - 如何在样式中使用附加属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7139641/

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