【发布时间】:2014-09-15 21:39:48
【问题描述】:
我在使用从现有资源创建的视觉画笔填充矩形时遇到了以下问题。
如果我要在 XAML 中对其进行硬编码,它将看起来像这样......
<ToggleButton Style="{DynamicResource MetroCircleToggleButtonStyle}" Height="60" Width="60">
<ToggleButton.Content>
<StackPanel Orientation="Vertical">
<Rectangle Height="30" Width="30">
<Rectangle.Fill>
<VisualBrush Visual="{StaticResource appbar_database}" Stretch="Uniform" />
</Rectangle.Fill>
</Rectangle>
</StackPanel>
</ToggleButton.Content>
我试图在后面的代码中执行此操作...并在运行时将资源名称作为组合控件(按钮和标签)的一部分传递。 我为这个组合控件(标题和图标资源)创建了两个 DependencyProperty,并尝试在更新图标资源后更新按钮内容,但是这部分代码似乎从未被执行:( ...有什么想法吗?
[编辑] 我已经为图标资源路径创建了依赖属性...
public string Caption
{
get { return (string)GetValue(TextProperty); }
set
{
SetValue(TextProperty, value);
if (string.IsNullOrEmpty(value))
{
tbCaption.Visibility = System.Windows.Visibility.Collapsed;
}
}
}
public string IconResource
{
get { return (string)GetValue(IconProperty); }
set
{
SetValue(IconProperty, value);
object icon = TryFindResource(value);
if (icon != null)
{
Visual iconVisual = icon as Visual;
((Rectangle)mainButton.Content).Fill = new VisualBrush(iconVisual);
}
}
}
public static readonly DependencyProperty TextProperty =
DependencyProperty.Register("Caption", typeof(string), typeof(ButtonWithText), null);
public static readonly DependencyProperty IconProperty =
DependencyProperty.Register("IconResource", typeof(string), typeof(ButtonWithText), null)
;
或
有没有办法在 XAML 中绑定包含动态资源名称的变量?
谢谢!
【问题讨论】: