调用GIF图像源代码在WPF c#为tabcontrol的设置器

问题描述:

我使用wpf选项卡控件,并通过样式设置标签标题中的图标和文本,我能够通过getter和setter动态设置文本,但无法设置图像源。我试图通过getter和setter绑定图像源,但失败了。以下是我想从后面的代码中动态设置图像源的样式代码。 - >调用GIF图像源代码在WPF c#为tabcontrol的设置器

 <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> 

也许你可以使用绑定,像

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

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

或者,如果它不工作,你可以使用MediaElement代替Image,它不需要WPF Animated GIF。这也是很简单的使用方法:

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

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

希望这有助于。

+0

我将无法调用RaisePropertyChanged,我该如何调用它? – Ebaad

+0

@Ebaad,不知道我是否理解你的问题。你的意思是你无法在视图模型或代码隐藏中设置这些属性?你如何更改Label的文本,是否绑定到某个属性? –

+0

是的,这正是我的意思,我无法在后面的代码中调用该图像标签,以便我可以在需要时更改图像运行时间或禁用可见性。 – Ebaad