2

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!

0

3 Answers 3

2

Disclaimer: I was away for a bit and you made an edit to your post, nothing wrong with that! :) I hope this information is still usefull to you. You may fetch your resource simpler.

If it's a shared resource you can do a (like in app.xaml, or a merged dict. there). I just made a small sample application and placed the style in app.xaml.

var style = (Style)Application.Current.TryFindResource("MetroCircleToggleButtonStyle");

In codebehind:

var style = (Style)TryFindResource("MetroCircleToggleButtonStyle");

Where to grab it sort of depends on where you have put your resource. I didn't find a scope link for you but you can dig a bit around here if you want to. I use TDD so I would make a service of this, particulary if you have to use the Applicationl.Current there, it's static etc. Actually I would have it as an interface in my viewmodelbase somewhere, anyways that's Offtopic. Hope this helps you! :)

FrameworkElement.TryFindResource

Application.TryfindResource

Cheers,

Stian

Sign up to request clarification or add additional context in comments.

1 Comment

Yes that would work - but the resource value will have to be passed in from a different view that why i had to create a dependency property and let the resource be set that way.
0

Ok i think i found a way to fix this.

I was able to pass in the resource by doing the following:

In the code behind ...

public Canvas IconResource
        {
            get { return (Canvas)GetValue(IconProperty); }
            set
            {
                SetValue(IconProperty, value);
            }
        }

        public static readonly DependencyProperty IconProperty =
            DependencyProperty.Register("IconResource", typeof(Canvas), typeof(ButtonWithText), null);

In XAML

 <ToggleButton x:Name="mainButton" Grid.Row="0" Style="{DynamicResource MetroCircleToggleButtonStyle}" Height="60" Width="60">
    <ToggleButton.Content>
        <Rectangle Height="30" Width="30">
                <Rectangle.Fill>
                    <VisualBrush Visual="{Binding IconResource}"></VisualBrush>
                </Rectangle.Fill>
            </Rectangle>
        </ToggleButton.Content>
</ToggleButton>

and ... when trying to use this control, i'll just pass in the entire dynamic resource

<k:ButtonWithText Caption="Hello" IconResource="{StaticResource appbar_magnify}"></k:ButtonWithText>

Comments

0

Model:

public class Status
{
    public string IconName{get;set;}
    public Canvas Icon 
    {
        get 
        {
            try
            {
                return Application.Current.FindResource(IconName) as Canvas;

            }                
            catch(System.Windows.ResourceReferenceKeyNotFoundException e)
            {
                return null;
            }
        }
    }
}

ViewModel:

public StatusViewModel
{
    public Status Status{get;set;}
    public StatusViewModel()
    {
        Status = new Status{IconName="appbar_database}"};
    }
}

Page .cs

DataContext = new StatusViewModel();

Page XAML

<ToggleButton x:Name="mainButton" Grid.Row="0" Style="{DynamicResource MetroCircleToggleButtonStyle}" Height="60" Width="60">
<ToggleButton.Content>
    <Rectangle Height="30" Width="30">
            <Rectangle.Fill>
                <VisualBrush Visual="{Binding Status.Icon}"></VisualBrush>
            </Rectangle.Fill>
        </Rectangle>
    </ToggleButton.Content>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.