【发布时间】:2010-06-29 12:01:38
【问题描述】:
嗯,它并不是非常动态,至少它不会在运行时改变。
我的想法是我有按钮,每个按钮都有一个独特的图像(图标 32x32)。这些按钮都共享一种我与 ControlTemplate 混淆的样式。因此,当我将鼠标悬停时,每张图像也有两种颜色,一种是正常颜色,另一种是另一种颜色。
我注意到,当我声明图像的源路径时,它们几乎都是相同的,所以我认为 DRY(不要重复自己)。如果我可以使用按钮名称或其他属性作为源路径的一部分(即图像文件的名称)会怎样。那将是很好的编程。
问题是我是 XAML、WPF 和可能一起编程的新手,所以我不知道该怎么做。我想这需要代码或某种转换器(我想我会更多地了解转换器)。这是一些代码(这不起作用,但它给了你大致的想法(希望)):
<Style x:Key="ButtonPanelBigButton" TargetType="{x:Type Button}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Border Name="ButtonBorder"
Height="78"
MaxWidth="70"
MinWidth="50"
BorderThickness="0.5"
BorderBrush="Transparent"
CornerRadius="8" >
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="2*" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<!-- Here I wan't to put in the Name property of the button because there is a picture there to match -->
<Image x:Name="ButtonIcon" Source="..\Images\Icons\32x32\Blue\{Binding Name}.png"
Margin="4"
Height="32"
Width="32"
HorizontalAlignment="Center"
VerticalAlignment="Center" />
<TextBlock Grid.Row="1"
Padding="5,2,5,2"
TextWrapping="Wrap"
Style="{StaticResource MenuText}"
HorizontalAlignment="Center"
VerticalAlignment="Center">
<ContentPresenter ContentSource="Content" />
</TextBlock>
</Grid>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True" >
<Setter TargetName="ButtonIcon" Property="Source" Value="..\Images\Icons\32x32\Green\user.png" /> <!-- Same Here -->
<Setter TargetName="ButtonBorder" Property="BorderBrush" Value="{StaticResource SecondColorBrush}" />
<Setter TargetName="ButtonBorder" Property="Background">
<Setter.Value>
<LinearGradientBrush StartPoint="0.5,0" EndPoint="0.5,1" Opacity="0.5">
<GradientStop Color="{StaticResource MainColor}" Offset="1" />
<GradientStop Color="{StaticResource SecondColor}" Offset="0.5" />
<GradientStop Color="{StaticResource MainColor}" Offset="0" />
</LinearGradientBrush>
</Setter.Value>
</Setter>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
希望你能明白我的意思,有人可能会帮助我,所以我的代码很好而且很干(现在我在重复自己!!!)。
【问题讨论】: