2013-04-27 177 views
0

喜后更改图像我有那种按钮:自定义按钮,点击

XAML:

... 
<Window.Resources> 
    <Style x:Key="NoChromeButton" TargetType="{x:Type Button}"> 
     <Setter Property="Background" Value="Transparent"/> 
     <Setter Property="BorderThickness" Value="1"/> 
     <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/> 
     <Setter Property="HorizontalContentAlignment" Value="Center"/> 
     <Setter Property="VerticalContentAlignment" Value="Center"/> 
     <Setter Property="Padding" Value="1"/> 
     <Setter Property="Template"> 
      <Setter.Value> 
       <ControlTemplate TargetType="{x:Type Button}"> 
        <Grid x:Name="Chrome" Background="{TemplateBinding Background}" SnapsToDevicePixels="true"> 
         <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/> 
        </Grid> 
        <ControlTemplate.Triggers> 
         <Trigger Property="IsEnabled" Value="false"> 
          <Setter Property="Foreground" Value="#ADADAD"/> 
          <Setter Property="Opacity" TargetName="Chrome" Value="0.5"/> 
         </Trigger> 
        </ControlTemplate.Triggers> 
       </ControlTemplate> 
      </Setter.Value> 
     </Setter> 
    </Style> 
</Window.Resources> 
... 
<Button Name="aaa" Style="{DynamicResource NoChromeButton}" Margin="5" Height="30" Width="40" Click="Button_Click"> 
      <StackPanel> 
       <Image Name="bbb" Source="C:\Users\Daniel\Desktop\WPF\Paint\skew.png"/> 
      </StackPanel> 
</Button> 

C#:

private void Button_Click(object sender, RoutedEventArgs e) 
{ 
    BitmapImage bmi = new BitmapImage(new Uri("C:\\Users\\Daniel\\Desktop\\WPF\\Paint\\skew2.png", UriKind.Relative)); 
    bbb.Source = bmi ; 
} 

当我点击按钮图像只是消失,我尝试了其他几种方法来做到这一点,但我做不到。我搜索这个论坛,但我没有找到任何能够真正帮助我的东西。

回答

0

您需要使用UriKind.Absolute作为图像路径。

BitmapImage bmi = 
     new BitmapImage(new Uri("C:\\Users\\Daniel\\Desktop\\WPF\\Paint\\skew2.png", 
     UriKind.Absolute)); 
    bbb.Source = bmi ; 
+0

或者根本不设置UriKind。 – Clemens 2013-04-27 18:59:07

0

您尚未设置ContentPresenterContent属性。