简体   繁体   English

从 WPF c# 后面的代码中为 tabcontrol 的 setter 调用 GIF 图像源

[英]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.我正在使用 wpf 选项卡控件并通过样式设置选项卡标题中的图标和文本,我可以通过 getter 和 setter 动态设置文本,但无法设置图像源。 I tried to bind the image source through getter and setter but failed.我试图通过 getter 和 setter 绑定图像源但失败了。 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>

I know this is a bit old thread, but this is how you update the AnimatedSource from code:我知道这是一个有点旧的线程,但这是您从代码更新 AnimatedSource 的方式:

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". “值”是 gif 文件的位置,例如“25.gif”。 The Imagereference is the Image control you want to update. Imagereference 是您要更新的 Image 控件。 Also ensure you have the namespace referenced: using WpfAnimatedGif;还要确保您引用了命名空间: using WpfAnimatedGif;

Maybe you can use Binding, something like 也许您可以使用Binding之类的

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. 或者,如果不起作用,则可以使用MediaElement代替Image ,它将不需要WPF动画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. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM