2011-08-29 44 views
1

我有一個<Style x:Key="ToggleButtonStyle" TargetType="ToggleButton">它有一個TextBlock在裏面。我希望ToggleButton能夠在引用樣式時更改TextBlockText屬性。我怎樣才能做到這一點?Silverlight從引用對象中改變樣式屬性

我的風格是如下

<Style x:Key="ReturnToggleButton" TargetType="ToggleButton"> 
    <Setter Property="BorderThickness" Value="1"/> 
    <Setter Property="BorderBrush" Value="#FF999999"/> 
    <Setter Property="Background" Value="#FFECECEB"/> 
    <Setter Property="Foreground" Value="#FF999999"/> 
    <Setter Property="HorizontalContentAlignment" Value="Center"/> 
    <Setter Property="VerticalContentAlignment" Value="Center"/> 
    <Setter Property="Padding" Value="3"/> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="ToggleButton"> 
       <StackPanel Orientation="Horizontal" HorizontalAlignment="Left" VerticalAlignment="Center" Background="Transparent"> 
        <VisualStateManager.VisualStateGroups> 
        ... 
        </VisualStateManager.VisualStateGroups> 
        <TextBlock x:Name="text1" TextWrapping="Wrap" Text="Open" Foreground="#FF35393D" HorizontalAlignment="Left" VerticalAlignment="Top" FontSize="10" FontFamily="Verdana"/> 
        <Border x:Name="border1" BorderBrush="#FF999999" BorderThickness="1" CornerRadius="1" Height="14" Width="36" Margin="6,0" Background="Transparent"> 
            <TextBlock x:Name="text2 TextWrapping="Wrap" Text="9:30" Foreground="#FF999999" HorizontalAlignment="Left" VerticalAlignment="Top" FontSize="10" FontFamily="Verdana"/> 
       </StackPanel> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
</Style> 

我需要能夠設置的text1text2

+0

這個問題需要更多的細節,顯示包含提到的TextBlock的xaml。 – AnthonyWJones

+0

已編輯原創帖子 – Aks

回答

0

這我會採取的方法的Text財產。

創建一個名爲「DualTextToggleButton」的新「Silverlight模板控件」,並將基類從「Control」更改爲「ToggleButton」。

添加兩個名爲「Text1」和「Text2」的DependencyProperties(您可能希望使用比我在此處選擇的名稱更好的名稱)。現在打開Themes/Generic.xaml文件,並用當前使用的樣式替換DualTextToggleButton的默認樣式。像這樣: -

<Style TargetType="local:DualTextToggleButton"> 
    <Setter Property="Text1" Value="Open" /> 
    <Setter Property="Text2" Value="9:30" /> 
    <Setter Property="BorderThickness" Value="1"/> 
    <Setter Property="BorderBrush" Value="#FF999999"/> 
    <Setter Property="Background" Value="#FFECECEB"/> 
    <Setter Property="Foreground" Value="#FF999999"/> 
    <Setter Property="HorizontalContentAlignment" Value="Center"/> 
    <Setter Property="VerticalContentAlignment" Value="Center"/> 
    <Setter Property="Padding" Value="3"/> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="ToggleButton"> 
       <StackPanel Orientation="Horizontal" HorizontalAlignment="Left" VerticalAlignment="Center" Background="Transparent"> 
        <VisualStateManager.VisualStateGroups> 
        ... 
        </VisualStateManager.VisualStateGroups> 
        <TextBlock x:Name="text1" TextWrapping="Wrap" Text="{TemplateBinding Text1}" Foreground="#FF35393D" HorizontalAlignment="Left" VerticalAlignment="Top" FontSize="10" FontFamily="Verdana"/> 
        <Border x:Name="border1" BorderBrush="#FF999999" BorderThickness="1" CornerRadius="1" Height="14" Width="36" Margin="6,0" Background="Transparent"> 
            <TextBlock x:Name="text2 TextWrapping="Wrap" Text="{TemplateBinding Text2}" Foreground="#FF999999" HorizontalAlignment="Left" VerticalAlignment="Top" FontSize="10" FontFamily="Verdana"/> 
       </StackPanel> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
</Style> 

注意使用TemplateBinding以及添加文本1和文本制定者。在適當的情況下,您的模板可能會使用更多的TemplateBinding用法,此時大多數設置程序完全沒有任何操作,因爲ControlTemplate會忽略它們。

現在你可以像這樣在XAML中創建這個控件的實例: -

<local:DualTextToggleButton Text1="Closed" Text2="17:00" /> 

或者你通過風格做到這一點: -

<Style x:Key="SomeDualTextToggleButtonStyle"> 
    <Setter Property="Text1" Value="Closed" /> 
    <Setter Property="Text2" Value="17:00" /> 
</Style> 

或者你可以使用數據綁定: -

<ListBox ItemsSource="{Binding ListOfModelItems}"> 
    <ListBox.ItemStyle> 
     <DataTemplate> 
      <local:DualTextToggleButton Text1="{Binding State}" Text2="{Binding TimeOfDay}" 
       IsChecked="{Binding Enabled, Mode=TwoWay}" /> 
     </DataTemplate> 
    </ListBox.ItemStyle> 
</ListBox ItemsSource="{Binding ListOfModelItems}">