2015-12-29 44 views
0

我有一個包含一個togglebutton的數據模板的gridview。 Gridview中的項目的數據模板中有一個網格,並帶有一個程式化的切換按鈕。 togglebutton的樣式位於Grid.Resources中。它工作正常,但是當我將樣式從Grid.Resources移動到Page.Resources或App.xaml時,樣式中定義的按鈕內的內容將從所有格中消失,除了gridview中第一次出現按鈕。這裏是風格:風格僅適用於Grid.Resources之外的第一次出現。

<Style TargetType="ToggleButton" x:Key="teststyle"> 
     <Setter Property="Padding" Value="0"/> 
     <Setter Property="Content"> 
      <Setter.Value> 
       <Path HorizontalAlignment="Center" Stroke="Black" StrokeThickness="1.25" VerticalAlignment="Center" Height="9" Width="9" Stretch="Uniform" Fill="Black" Data="M 0,0 -11.78,-11.779 0,-23.561 l 1.061,1.061 -9.97,9.971 21.064,0 0,1.5 -21.064,0 9.97,9.968 L 0,0 Z" RenderTransformOrigin="0.5,0.5" > 
        <Path.RenderTransform> 
         <CompositeTransform Rotation="-90"/> 
        </Path.RenderTransform> 
       </Path> 
      </Setter.Value> 
     </Setter> 
</Style> 

這裏是行爲的截圖:http://imgur.com/a/8iZaD頂部圖像是當風格定位於Grid.Resources原來,當風格被移到底部。

+0

您是否明確地將ToggleButton樣式設置爲此StaticResource項(在您的數據模板中)? –

+0

@igrali這是我的ToggleButton聲明在數據模板中的樣子:'' – justanotherxl

回答

1

你可以在你的風格而不是內容使用的ContentTemplate解決此問題。

<Page.Resources> 
    <Style TargetType="ToggleButton"> 
     <Setter Property="Padding" Value="0"/> 
     <Setter Property="ContentTemplate"> 
      <Setter.Value> 
       <DataTemplate> 
        <Path HorizontalAlignment="Center" Stroke="Black" StrokeThickness="1.25" VerticalAlignment="Center" Height="9" Width="9" Stretch="Uniform" Fill="Black" Data="M 0,0 -11.78,-11.779 0,-23.561 l 1.061,1.061 -9.97,9.971 21.064,0 0,1.5 -21.064,0 9.97,9.968 L 0,0 Z" RenderTransformOrigin="0.5,0.5" > 
         <Path.RenderTransform> 
          <CompositeTransform Rotation="-90"/> 
         </Path.RenderTransform> 
        </Path> 
       </DataTemplate> 
      </Setter.Value> 
     </Setter> 
    </Style> 
</Page.Resources> 
+0

這解決了這個問題,你知道爲什麼行爲不正確的內容屬性? – justanotherxl

+0

Honnestly否:)我想調查的根本原因,但沒有找到答案呢。 –

0

在XAML中移動樣式的最佳方法是使用Blend設計器。

您可以在Blend中的參考資料部分查看和組織您的樣式。之後,您可以將本地風格拖放到App.xaml或Resources.xaml中。所有參考文獻將自動更新,您可以從下面的gif圖像檢查。

enter image description here

+0

試過這個。還是同樣的問題:( – justanotherxl

1

Style可以在DataTemplate外出打工,這裏的問題是Path你已經設置爲您ToggleButtonContent

Style XAML資源必須共享(參見XAML resources must be shareable):

對於在ResourceDictionary存在一個對象,該對象必須是共享

可共享是必需的,因爲當應用程序的對象樹在運行時被構造和使用時,對象不能存在於樹中的多個位置。在內部,當請求每個XAML資源時,資源系統會創建資源值的副本以用於應用程序的對象圖中。

UIElement永遠不能共享,並從UIElementPath繼承所以Path不可共享。當在DataTemplate中設置Style並將Style設置在DataTemplate之外時,項目中的每個ToggleButton都應用Style,但Path只能顯示一次,因爲它不可共享。爲了測試它,你可以在你的Style像添加對象:

<Style x:Key="teststyle" TargetType="ToggleButton"> 
    <Setter Property="Padding" Value="0" /> 
    <Setter Property="Content"> 
     <Setter.Value> 
      <Path Width="9" 
        Height="9" 
        HorizontalAlignment="Center" 
        VerticalAlignment="Center" 
        Data="M 0,0 -11.78,-11.779 0,-23.561 l 1.061,1.061 -9.97,9.971 21.064,0 0,1.5 -21.064,0 9.97,9.968 L 0,0 Z" 
        Fill="Black" 
        RenderTransformOrigin="0.5,0.5" 
        Stretch="Uniform" 
        Stroke="Black" 
        StrokeThickness="1.25"> 
       <Path.RenderTransform> 
        <CompositeTransform Rotation="-90" /> 
       </Path.RenderTransform> 
      </Path> 
     </Setter.Value> 
    </Setter> 
    <Setter Property="Background" Value="Red" /> 
</Style> 

這時你會發現變成紅色您ToggleButton的所有背景。您也可以直接將兩個ToggleButtonStackPanel,如:

<StackPanel> 
    <ToggleButton Style="{StaticResource teststyle}" /> 
    <ToggleButton Style="{StaticResource teststyle}" /> 
</StackPanel> 

您還可以找到只有第一個ToggleButton有路徑。

而當你把Style放在DataTemplate裏面時,每個項目都會有自己的Resources,所以工作正常。

如果你使用ContentTemplate像@讓 - 塞巴斯蒂安·杜佩說,那麼Value成爲DataTemplate,因爲它是從FrameworkTemplate得出這是共享的,所以它也能正常工作。

相關問題