2012-06-20 116 views
2

我在WPF中工作,我試圖改變一個按鈕的圖像,我創建了一些觸發器和Setters,但它不工作。更改按鈕圖像問題

這裏是我的XAML代碼:

<Button BorderThickness="0" Height="23" HorizontalAlignment="Left" Margin="59,6,0,0" Name="topButton" Width="26" VerticalAlignment="Top" 
      Style="{StaticResource {x:Static ToolBar.ButtonStyleKey}}" IsEnabled="False" > 
     <StackPanel> 
      <Image> 
       <Image.Style> 
        <Style TargetType="Image"> 
         <Style.Triggers> 
          <DataTrigger Binding="{Binding ElementName=topButton, Path=Button.IsEnabled}" Value="True"> 
           <Setter Property="Image.Source" Value="Images/MoveFirst_Enabled.bmp" /> 
          </DataTrigger> 
          <DataTrigger Binding="{Binding ElementName=topButton, Path=Button.IsEnabled}" Value="False"> 
           <Setter Property="Image.Source" Value="Images/MoveFirst_Disabled.bmp" /> 
          </DataTrigger> 
         </Style.Triggers> 
        </Style> 
       </Image.Style> 
      </Image> 
     </StackPanel> 
    </Button> 

我不能明白我在做什麼錯。希望您能夠幫助我。先謝謝你。

+0

重複:http://stackoverflow.com/questions/2831359/wpf-image-change-source - 當按鈕被禁用 –

+0

@MortenFrederiksen我正在嘗試做與剛剛寫入的鏈接相同的內容。但它不起作用,因此我尋求一些幫助。 – Dante

回答

3

Path取出Button.。因此,而不是

Path=Button.IsEnabled 

你應該有

Path=IsEnabled 

<Button BorderThickness="0" Height="23" HorizontalAlignment="Left" Margin="59,6,0,0" Name="topButton" Width="26" VerticalAlignment="Top"    Style="{StaticResource {x:Static ToolBar.ButtonStyleKey}}" IsEnabled="False"> 
    <StackPanel> 
     <Image> 
      <Image.Style> 
       <Style TargetType="Image"> 
        <Style.Triggers> 
         <DataTrigger Binding="{Binding ElementName=topButton, Path=IsEnabled}" Value="True"> 
          <Setter Property="Source" Value="Images/MoveFirst_Enabled.bmp" /> 
         </DataTrigger> 
         <DataTrigger Binding="{Binding ElementName=topButton, Path=IsEnabled}" Value="False"> 
          <Setter Property="Source" Value="Images/MoveFirst_Disabled.bmp" /> 
         </DataTrigger> 
        </Style.Triggers> 
       </Style> 
      </Image.Style> 
     </Image> 
    </StackPanel> 
</Button> 
+0

它完美的工作!非常感謝你! – Dante

+0

@丹特:當然,很高興幫助:) –

1

我認爲你沒有使用正確的Uri字符串作爲圖像的來源。

<Button BorderThickness="0" Height="23" HorizontalAlignment="Left" Margin="59,6,0,0" Name="topButton" Width="26" VerticalAlignment="Top" 
       Style="{StaticResource {x:Static ToolBar.ButtonStyleKey}}" IsEnabled="False" > 
      <StackPanel> 
       <Image> 
        <Image.Style> 
         <Style TargetType="Image"> 

           <!-- If you are using the Image as a Resource, you should use the following format.--> 

          <Style.Triggers>  
           <DataTrigger Binding="{Binding ElementName=topButton, Path=Button.IsEnabled}" Value="True"> 
            <Setter Property="Image.Source" Value="assemblyname;component/Images/MoveFirst_Enabled.bmp" /> 
           </DataTrigger> 
           <DataTrigger Binding="{Binding ElementName=topButton, Path=Button.IsEnabled}" Value="False"> 
            <Setter Property="Image.Source" Value="assemblyname;component/Images/MoveFirst_Disabled.bmp" /> 
           </DataTrigger> 
          </Style.Triggers> 

          <!-- If you are using the Image as Content then you should use the below format.--> 

          <Style.Triggers>  
           <DataTrigger Binding="{Binding ElementName=topButton, Path=Button.IsEnabled}" Value="True"> 
            <Setter Property="Image.Source" Value="Images/MoveFirst_Enabled.bmp" /> 
           </DataTrigger> 
           <DataTrigger Binding="{Binding ElementName=topButton, Path=Button.IsEnabled}" Value="False"> 
            <Setter Property="Image.Source" Value="Images/MoveFirst_Disabled.bmp" /> 
           </DataTrigger> 
          </Style.Triggers> 

         </Style> 
        </Image.Style> 
       </Image> 
      </StackPanel> 
     </Button> 

我希望它可以幫助你......

+1

謝謝你的回覆,我接受了這個建議,但它仍然不能正常工作 – Dante