2011-12-01 23 views
1

我在Silverlight中使用切換按鈕控件。請告訴我如何使用不同的字體系列來處理Checked和Unchecked狀態。我曾嘗試使用Expression Blend編輯切換按鈕模板,但無法使其工作。請幫忙。針對不同按鈕狀態的不同字體

感謝, 奧馬爾賈韋德

回答

0

您可以使用轉換器和TextBlock的按鈕內的FontFamily屬性綁定到按鈕的狀態器isChecked。

<ToggleButton 
    x:Name="tb"> 
    <ToggleButton.Resources> 
     <converters:BooleanToFontFamily 
      x:Key="BooleanToFontFamily" /> 
    </ToggleButton.Resources> 
    <TextBlock 
     FontFamily="{Binding IsChecked, ElementName='tb', Converter={StaticResource BooleanToFontFamily}, ConverterParameter='Segoe WP Semibold,Segoe WP SemiLight'}" 
     Text="Some Text" /> 
</ToggleButton> 

而在C#中的轉換器看起來rouguhly這樣的:

public class BooleanToFontFamily : IValueConverter 
{ 
    public object Convert(
     object value, 
     Type targetType, 
     object parameter, 
     CultureInfo culture) 
    { 
     var b = (bool)value; 
     var families = ((string)parameter).Split(','); 
     return new FontFamily(b ? families[1] : families[0]); 
    } 

    public object ConvertBack(
     object value, 
     Type targetType, 
     object parameter, 
     CultureInfo culture) 
    { 
     throw new NotSupportedException(); 
    } 
} 

雖然個人 - 我會繼續努力,讓該模板的工作 - 可能是一些與VisualStateManager處理過渡到做「檢查」狀態。

0

簡單的方法是使用內置的行爲模式ChangePropertyAction。既然你已經提到你已經安裝了Expression Blend,只需進入資產面板並輸入'ChangePropertyAction',然後將這個行爲拖放到你的ToggleButton之上。

之後,您只需在之後指定哪個事件發生了您要更改的火災什麼。有關如何使用此行爲的更多信息,請參閱this post

一些示例代碼

<ToggleButton Content="ToggleButton" FontFamily="Arial" HorizontalAlignment="Center" VerticalAlignment="Center"> 
     <i:Interaction.Triggers> 
      <i:EventTrigger EventName="Checked"> 
       <ei:ChangePropertyAction PropertyName="FontFamily"> 
        <ei:ChangePropertyAction.Value> 
         <FontFamily>Arial Black</FontFamily> 
        </ei:ChangePropertyAction.Value> 
       </ei:ChangePropertyAction> 
      </i:EventTrigger> 
      <i:EventTrigger EventName="Unchecked"> 
       <ei:ChangePropertyAction PropertyName="FontFamily"> 
        <ei:ChangePropertyAction.Value> 
         <FontFamily>Arial</FontFamily> 
        </ei:ChangePropertyAction.Value> 
       </ei:ChangePropertyAction> 
      </i:EventTrigger> 
     </i:Interaction.Triggers> 
    </ToggleButton> 
相關問題