Ebaad
Ebaad

Reputation: 21

Call GIF image source from the code behind in WPF c# for the tabcontrol's setter

i am using wpf tab control and setting the Icon and text in the tab header through style, i am able to set the text dynamically through getter and setter but can not able to set the image source. I tried to bind the image source through getter and setter but failed. Following is the style code in which i want to set the image source dynamically from the code behind. -->

        <Setter Property="HeaderTemplate" >

            <Setter.Value>

                <DataTemplate>
                    <StackPanel  Orientation="Horizontal">
                        <!--<Image   gif:ImageBehavior.AnimatedSource="{DynamicResource MyFillBrush}"  Width="20" />-->

                        <Image   gif:ImageBehavior.AnimatedSource="25.gif"  Width="20" />
                        <Label Content="{Binding }"/>

                    </StackPanel>

                </DataTemplate>

            </Setter.Value>

        </Setter>

Upvotes: 0

Views: 657

Answers (2)

Brian Dobony
Brian Dobony

Reputation: 229

I know this is a bit old thread, but this is how you update the AnimatedSource from code:

var image = new BitmapImage();
image.BeginInit();
image.UriSource = new Uri(value);
image.EndInit();
ImageBehavior.SetAnimatedSource(Imagereference, image);

The 'value' is the location of your gif file such as "25.gif". The Imagereference is the Image control you want to update. Also ensure you have the namespace referenced: using WpfAnimatedGif;

Upvotes: 0

Arthur Kazykhanov
Arthur Kazykhanov

Reputation: 325

Maybe you can use Binding, something like

private string _dynamicGif = "test.gif";
public string DynamicGif
{
    get { return _dynamicGif; }
    private set
    {
        _dynamicGif = value;
        RaisePropertyChanged("DynamicGif");
    }
}

and

<Image   gif:ImageBehavior.AnimatedSource="{Binding DynamicGif, UpdateSourceTrigger=PropertyChanged}"  Width="20" />

Or, if it doesn't work, you can use MediaElement instead of Image, it won't require WPF Animated GIF. It's also pretty simple to use:

private Uri _imgSource = new Uri("test.gif");
public Uri ImgSource
{
    get { return _imgSource; }
    private set
    {
        _imgSource = value;
        RaisePropertyChanged("ImgSource");
    }
}

and

<MediaElement x:Name="gifImg" LoadedBehavior="Play" Width="20" Source="{Binding ImgSource}"/>

Hope this helps.

Upvotes: -1

Related Questions