2014-01-22 11 views
0

我需要自定义Microsoft.Phone.Controls.Rating控件。下面的代码创建了一个更接近模板,但我需要更改图标。自定义Microsoft.Phone.Controls.Rating

<Style x:Key="RatingStyle" TargetType="toolkit:Rating"> 
    <Setter Property="AllowHalfItemIncrement" Value="True" /> 
    <Setter Property="Width" Value="100" /> 
    <Setter Property="Height" Value="20" /> 
    <Setter Property="FilledItemStyle"> 
     <Setter.Value> 
      <Style TargetType="toolkit:RatingItem"> 
       <Setter Property="Background" 
         Value="#FF9900" /> 
      </Style> 
     </Setter.Value> 
    </Setter> 
    <Setter Property="UnfilledItemStyle"> 
     <Setter.Value> 
      <Style TargetType="toolkit:RatingItem"> 
       <Setter Property="Background" 
         Value="#E0E0E0" /> 
      </Style> 
     </Setter.Value> 
    </Setter> 
</Style> 

我的需求模板是2,地点评级为1,平均价格水平为1(显示$符号)。下面是显示我的要求的图像。

enter image description here

回答

3

我找到最快的方法是改变的两种风格Template来调整图像/外观:

<Style x:Name="UnfilledDollarStyle" TargetType="toolkit:RatingItem"> 
    <Setter Property="Template">     
     <Setter.Value> 
      <ControlTemplate TargetType="toolkit:RatingItem"> 
       <Border HorizontalAlignment="Center" VerticalAlignment="Center"> 
        <TextBlock FontSize="30" Foreground="Silver">$</TextBlock> 
       </Border> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 

</Style> 
<Style x:Name="FilledDollarStyle" TargetType="toolkit:RatingItem"> 
    <Setter Property="Template">     
     <Setter.Value> 
      <ControlTemplate TargetType="toolkit:RatingItem"> 
       <Border HorizontalAlignment="Center" VerticalAlignment="Center"> 
        <TextBlock FontSize="30" Foreground="Green">$</TextBlock> 
       </Border> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
</Style> 

在使用中:

<toolkit:Rating UnfilledItemStyle="{StaticResource UnfilledDollarStyle}" 
       FilledItemStyle="{StaticResource FilledDollarStyle}" Value="2" /> 

Show me the money (symbols that is)

通过编辑Template,你可以在每个RatingItem内使用图片或任何你想要的东西。

当然,您可以将未填充的样式保留为“空白”或完全透明,以便根本不显示任何符号。 (为了保持控件的一致间距,我建议使用完全透明的符号而不是内容)。在这里,我已经设置了Opacity0

<TextBlock FontSize="30" Opacity="0">$</TextBlock> 

Hide symbols, no gray

+0

真棒,我实际上寻找编辑模板,它删除我所有的痛苦。请让我知道如果我可以在样式中使用样式?意味着您创建了两种不同的样式,我们直接将其分配给控件。我创建了一个AveragePriceTemplate并将其填充和未填充分配给staticrecources。我们可以直接在我们的AveragePriceTemplate中放入两个填充/未填充样式而不定义它们的名称吗? – LojiSmith

+0

是的,您应该可以通过直接指定样式属性来实现这一点。虽然,我不是100%确定我理解你的问题。 – WiredPrairie