2016-09-29 108 views
0
<DataTemplate> 
       <Image Name="ServerImageSetter" Source="{svg2Xaml:SvgImage ServerAdministrator2;component/Images/test1.svg}"/> 
       <DataTemplate.Triggers> 
        <DataTrigger Binding="{Binding ServerKindTrigger}" Value="0"> 
         <Setter TargetName="ServerImageSetter" Property="Source" Value="{svg2Xaml:SvgImage ServerAdministrator2;component/Images/test2.svg}"/> 
        </DataTrigger> 
        <DataTrigger Binding="{Binding ServerKindTrigger}" Value="1"> 
         <Setter TargetName="ServerImageSetter" Property="Source" Value="{svg2Xaml:SvgImage ServerAdministrator2;component/Images/test3.svg}"/> 
        </DataTrigger> 
        <DataTrigger Binding="{Binding ServerKindTrigger}" Value="2"> 
         <Setter TargetName="ServerImageSetter" Property="Source" Value="{svg2Xaml:SvgImage ServerAdministrator2;component/Images/test3.svg}"/> 
        </DataTrigger> 
</DataTemplate   

在我的C#代碼中,我將我的值的類型切換爲0-2,並且應該更改圖像。但這種改變不起作用。DataTemplate不會改變圖片

public int ServerKindTrigger 
    { 
     get { return _serverKindTrigger; } 
     set { _serverKindTrigger = value; 
      RaisePropertyChanged(); 
     } 
    } 

//Here I switch my serverkind and wanna change the image value 

switch (_serverKind) 
     { 
      case Servers.test: _serverKindTrigger = 0; 
+1

如果分配新的值變量'_serverKindTrigger',不提高的通知。通過屬性來實現:'ServerKindTrigger = 0;'。 – ASh

回答

0

除了制定支持字段_serverKindTrigger,而不是財產ServerKindTrigger,因而沒有發射的PropertyChangedEvent,你直接設置Image控件的Source屬性:

<Image Source="{svg2Xaml:SvgImage ServerAdministrator2;component/Images/test1.svg}"/> 

這個所謂的地方值始終比DataTriggers中Setters的值具有更高的優先級。作爲參考,請參閱Dependency Property Value Precedence

您的XAML改成這樣:

<Image> 
    <Image.Style> 
     <Style TargetType="Image"> 
      <Setter Property="Source" Value="{svg2Xaml:SvgImage ServerAdministrator2;component/Images/test1.svg}"/> 
      <Style.Triggers> 
       <DataTrigger Binding="{Binding ServerKindTrigger}" Value="0"> 
        <Setter Property="Source" Value="{svg2Xaml:SvgImage ServerAdministrator2;component/Images/test2.svg}"/> 
       </DataTrigger> 
       <DataTrigger Binding="{Binding ServerKindTrigger}" Value="1"> 
        <Setter Property="Source" Value="{svg2Xaml:SvgImage ServerAdministrator2;component/Images/test3.svg}"/> 
       </DataTrigger> 
       <DataTrigger Binding="{Binding ServerKindTrigger}" Value="2"> 
        <Setter Property="Source" Value="{svg2Xaml:SvgImage ServerAdministrator2;component/Images/test3.svg}"/> 
       </DataTrigger> 
      </Style.Triggers> 
     </Style> 
    </Image.Style> 
</Image> 
+0

謝謝,這就是解決方案! (: – Pinotek

0

您正在更改本地字段而不是屬性,這就是爲什麼它不觸發綁定更新。

更改您的開關/外殼分配以分配ServerKindTrigger屬性。

switch (_serverKind) 
     { 
      case Servers.test: ServerKindTrigger = 0; 
+0

感謝您的回答,但我的問題仍然存在。在我的形象應該是,那裏只是站在「系統」。 – Pinotek