I'm running into the following issue on filling a rectangle with a visual brush created from an existing resource.
If I am to hard code it in XAML, it'll look like this ...
<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>
I'm trying to do this in code behind instead ... and to pass in the resource name in run-time as part of a combined control (a button and a label). I've created two DependencyProperty for this combined control (caption and icon resource) and trying to update the button content once the icon resource is updated, but that part of the code never seemed to be executed :( ... any thoughts?
[edit] I've created dependency properties for the icon resource path ...
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)
;
or
is there a way to bind a variable that contains the dynamic resource name in XAML?
Thanks!