WPF Bug清单12中我们介绍过一个与不同WPF版本上XAML解析相关的Bug。今天要介绍另一个.NET Framework 4对于XAML解析上的改进。

 

我们知道Button属于ContentControlContentControl里的Content逻辑上可以是任何其它Control。所以我们可以把ButtonContent设置为一个Image。这也是一个很常见的功能。

 

代码如下所示:

<Button Height="28">
    
<Image Width="16" Height="16" Source="Green.PNG" Stretch="Fill"/> 
</Button>

 

 

那么我们可不可以在ButtonStyle里把Content属性就设置为Image呢?理论上应该没有什么问题。但是事实是在.NET Framework 4之前的版本的WPF中,像下面这样:

<Style x:Key="ImageButtonStyle" TargetType="{x:Type Button}">
    
<Setter Property="Content">
        
<Setter.Value>
            
<Image Width="16" Height="16" Source="Green.PNG" Stretch="Fill"/> 
        
</Setter.Value>
    
</Setter>
    
<Setter Property="ToolTip">
        
<Setter.Value>
            
<Image Width="16" Height="16" Source="Green.PNG" Stretch="Fill"/>
        
</Setter.Value>
    
</Setter>
</Style>

相关文章:

  • 2021-09-24
  • 2021-10-24
  • 2022-01-16
  • 2021-09-07
  • 2022-12-23
  • 2021-06-27
  • 2021-06-06
  • 2021-09-14
猜你喜欢
  • 2021-09-19
  • 2022-12-23
  • 2022-12-23
  • 2021-09-27
  • 2021-11-15
  • 2021-09-18
  • 2022-01-17
相关资源
相似解决方案